xslt - 检查 XSL 模板中节点的类型

标签 xslt xpath-1.0

是否可以检查我与同一模板内的模板匹配的节点的类型?如果是,我该怎么做?例如,我想做这样的事情:

<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>

最佳答案

看看这里的答案,因为这应该为您提供所需的信息:

Difference between: child::node() and child::*

这给出了以下 xsl:选择 测试所有节点,包括文档节点。

<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>

关于xslt - 检查 XSL 模板中节点的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118670/

相关文章:

asp.net - Web.config转换作为第一个子项插入

xml - Schematron 条件验证

xml - XSLT 中的除法和乘法选择值

.net - 如何使用 XPath 选择最浅的匹配元素?

xml - XPath 1.0:列出不同的值及其出现的次数

xpath - XPath 1.0 中的总结

xml - XSL : Search for node name

selenium - XPath:多个谓词与逻辑 And 运算符

google-chrome - XPath 1.0 中括号的作用是什么?

xml - 在自闭合元素之间插入元素