根节点的 xslt 默认模板混淆
xslt default template confusion for root node
xslt 中的一个默认模板如下:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
为什么匹配模式包含根节点“/”的表达式?星号“*”不是已经捕获了文档中的所有节点吗?
我试着把它去掉,但没有区别。正在关注 xslt
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" indent="yes"/>
<xsl:template match="*">
<xsl:value-of select="./name()"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
及以下 xml
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
</b>
</a>
产生输出:
<?xml version="1.0" encoding="UTF-8"?>a
b
所以根节点a被捕获了。
由于位置路径的默认轴是 child
,*|/
只是 child::*|/
的缩写。并且 child::*
不匹配文档的根节点,只匹配它的子节点。
xslt 中的一个默认模板如下:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
为什么匹配模式包含根节点“/”的表达式?星号“*”不是已经捕获了文档中的所有节点吗?
我试着把它去掉,但没有区别。正在关注 xslt
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" indent="yes"/>
<xsl:template match="*">
<xsl:value-of select="./name()"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
及以下 xml
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
</b>
</a>
产生输出:
<?xml version="1.0" encoding="UTF-8"?>a
b
所以根节点a被捕获了。
由于位置路径的默认轴是 child
,*|/
只是 child::*|/
的缩写。并且 child::*
不匹配文档的根节点,只匹配它的子节点。