我在使用xsl:apply-template时遇到了一个问题。我正在尝试将模板应用于一个特定的标签,但我看到来自其他标签的文本。一个简单的xml文件:
<?xml version="1.0"?>
<!-- execute with xsltproc foo.xsl foo.xml -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" />
<xsl:template match="/foo">
<xsl:for-each select="bar">
<xsl:value-of select="grill"/>
<xsl:apply-templates match="baz"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="foo">[<xsl:value-of select="." />|http://example.com/<xsl:value-of select="." />]</xsl:template>
</xsl:stylesheet> 输入为:
<?xml version="1.0"?>
<foo>
<bar>
<baz>a <foo>b</foo> c</baz>
<grill>grill</grill>
</bar>
</foo>输出为:
grill
a [b|http://example.com/b] c
grill我希望输出结果是
grill
a [b|http://example.com/b] c(我现在不关心间距问题)
我可以解决用xsl:for-each包装xsl:apply-template的问题:
<xsl:for-each select="grill">
<xsl:apply-templates match="grill"/>
</xsl:for-each>但我真的不喜欢这个解决方案。有没有更好的方法?
发布于 2011-11-02 01:55:41
不允许在xsl:apply-templates元素上使用属性match。在xsl:apply-templates中将match更改为select,然后重试。
https://stackoverflow.com/questions/7970529
复制相似问题