检查 XSL 模板中节点的类型 [英] Check type of node in XSL template
本文介绍了检查 XSL 模板中节点的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以检查我与同一模板内的模板匹配的节点的类型?如果是,我该怎么做?例如我想做这样的事情:
Is it possible to check the type of a node I matched with a template inside the same template? In case it is, how can I do it? For example I would like to do something like this:
<xsl:template match="@*|node()">
<xsl:choose>
<xsl:when test="current() is an attribute">
<!-- ... -->
</xsl:when>
<xsl:when test="current() is an element">
<!-- ... -->
</xsl:when>
<xsl:otherwise>
<!-- ... -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
推荐答案
看看这里的答案,因为它应该给你你需要的信息:
Take a look at this answer here, as this should give you the information you need:
这给出了以下 xsl:choose 来测试所有节点,包括文档节点.
This gives the following xsl:choose to test all the nodes, including the document node.
<xsl:choose>
<xsl:when test="count(.|/)=1">
<xsl:text>Root</xsl:text>
</xsl:when>
<xsl:when test="self::*">
<xsl:text>Element </xsl:text>
<xsl:value-of select="name()"/>
</xsl:when>
<xsl:when test="self::text()">
<xsl:text>Text</xsl:text>
</xsl:when>
<xsl:when test="self::comment()">
<xsl:text>Comment</xsl:text>
</xsl:when>
<xsl:when test="self::processing-instruction()">
<xsl:text>PI</xsl:text>
</xsl:when>
<xsl:when test="count(.|../@*)=count(../@*)">
<xsl:text>Attribute</xsl:text>
</xsl:when>
</xsl:choose>
这篇关于检查 XSL 模板中节点的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文