【发布时间】:2012-08-17 18:35:58
【问题描述】:
这两个模板有什么区别?
<xsl:template match="node()">
<xsl:template match="*">
【问题讨论】:
这两个模板有什么区别?
<xsl:template match="node()">
<xsl:template match="*">
【问题讨论】:
另请参阅XSL xsl:template match="/" 其他匹配模式。
【讨论】:
只是为了说明其中一个区别,即* 与text 不匹配:
给定 xml:
<A>
Text1
<B/>
Text2
</A>
匹配node()
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<!--Suppress unmatched text-->
<xsl:template match="text()" />
<xsl:template match="/">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="node()">
<node>
<xsl:copy />
</node>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
给予:
<root>
<node>
<A />
</node>
<node>
Text1
</node>
<node>
<B />
</node>
<node>
Text2
</node>
</root>
而在* 上匹配:
<xsl:template match="*">
<star>
<xsl:copy />
</star>
<xsl:apply-templates />
</xsl:template>
与文本节点不匹配。
<root>
<star>
<A />
</star>
<star>
<B />
</star>
</root>
【讨论】:
* 也不匹配注释节点、处理指令节点、属性节点、命名空间节点和文档节点... 模式或表达式 *(单独为 @987654332 的缩写) @) 只匹配元素节点和元素节点。当使用@*(attribute::* 的缩写)时,星号仅匹配属性轴上的属性节点。
<xsl:template match="node()">
是以下的缩写:
<xsl:template match="child::node()">
这匹配任何可以通过the child::轴选择的节点类型:
元素
文本节点
处理指令 (PI) 节点
评论节点。
另一边:
<xsl:template match="*">
是以下的缩写:
<xsl:template match="child::*">
这匹配任何元素。
XPath 表达式:someAxis::* 匹配给定轴的主节点类型的任何节点。
对于child:: 轴,主要节点类型是元素。
【讨论】: