XSLT显式节点选择不返回任何内容
我很难让XSLT只返回XML中的类别值。为什么要返回上次更新和路径。。。我怎样才能阻止这一切?提前谢谢 XML文档XSLT显式节点选择不返回任何内容,xslt,Xslt,我很难让XSLT只返回XML中的类别值。为什么要返回上次更新和路径。。。我怎样才能阻止这一切?提前谢谢 XML文档 <?xml version="1.0"?> <categories count="3"> <lastupdate>08/12/2010 12:27</lastupdate> <path>C:\</path> <category>Music</category>
<?xml version="1.0"?>
<categories count="3">
<lastupdate>08/12/2010 12:27</lastupdate>
<path>C:\</path>
<category>Music</category>
<category>News</category>
<category>Sport</category>
</categories>
08/12/2010 12:27
C:\
音乐
消息
运动
我的XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="categories">
<html>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<xsl:apply-templates/>
</td>
</tr>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="category">
<a>
<xsl:value-of select="." />
</a>
</xsl:template>
</xsl:stylesheet>
输出HTML
<html>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>08/12/2010 12:27C:\
<a>Music</a>
<a>News</a>
<a>Sport</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
2010年12月8日12:27C:\
音乐
消息
运动
将所有匹配模板应用于所有子节点
因为您没有为lastupdate和path定义匹配模板,所以XSLT应用了它的默认模板,在本例中,它复制文本内容
如果要禁用此功能,则必须覆盖默认模板(通常不太好),或者将模板应用程序限制在要处理的节点上。在您的示例中,将“应用模板”展开到
<xsl:apply-templates select="./category"/>
是否在xslt的底部有类似的内容:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<>这将解释为什么你会在输出的中间某处结束这些值。
为什么lastupdate和path被删除
回来了
因为正是这两个:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
意味着没有文本节点输出。或者使用推式方法,如
<xsl:template match="categories">
<html>
<body>
<xsl:apply-templates select="category"/>
</body>
</html>
</xsl:template>
在您的代码中,您正在为所有
类别
子元素节点应用模板。看
因此,您需要以下代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="categories">
<html>
<body>
<xsl:apply-templates select="category"/>
</body>
</html>
</xsl:template>
<xsl:template match="category">
<a>
<xsl:value-of select="." />
</a>
</xsl:template>
</xsl:stylesheet>
要获得所需输出:
<html>
<body>
<a>Music</a>
<a>News</a>
<a>Sport</a>
</body>
</html>
音乐
消息
运动
您的输出表明这不是您正在运行的完整xslt。我怀疑您的xslt中还有其他东西将lastupdate
和path
复制到您的输出中。我同意。这张桌子是从哪里来的?从您发布的代码中不清楚从他的示例中的缩进判断,他遗漏了正文和应用模板之间的表格,这也解释了输出。我添加了完整的XSLT文档,将其删减以使问题更具可读性,产生了相反的效果。抱歉,XSLT为节点、属性和文本节点定义了默认模板,这些模板通过常规调用在这里生效。在被推翻之前,它们始终存在。如果您将他的示例中的XSLT应用于XML,您将得到所描述的输出(除了缺少的表标记),您编写的输出将覆盖默认模板(通常不太好)。这是错误的:大多数情况下,每个转换都会覆盖一些内置规则。。。另外,启动/
步骤是冗长的,不需要。@Alejandro:普通模板使用仅适用于特定节点子集的特定规则覆盖常规(默认)规则。那完全可以。根据我的经验,用适用于相同范围的规则替换默认规则几乎没有必要。我加入了“/”以表明它是一个xpath语句,因为OP在XSLT方面似乎不是很有经验。如果原始案例比示例更复杂,他可能需要该提示:-)一致性规则(经常使用)覆盖内置规则的属性。
<html>
<body>
<a>Music</a>
<a>News</a>
<a>Sport</a>
</body>
</html>