如何在xslt中检查当前节点的父节点是否是根节点?

时间:2011-09-07 05:40:41

标签: xml xslt xpath xpath-2.0

我想在Xslt中检查当前节点的父节点是否是根节点。我这样做了吗? 请指导我摆脱这个问题...

谢谢&问候, P.SARAVANAN

2 个答案:

答案 0 :(得分:8)

您可以使用not(ancestor::*)

用法示例:

  <xsl:template match="node()|@*">
    <xsl:if test="not(ancestor::*)">
      <xsl:message>The root element is "<xsl:value-of select="name()"/>".</xsl:message>
    </xsl:if>
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

答案 1 :(得分:7)

在XPath 1.0(XSLT 1.0)

not(parent::*)

或者您可以使用:

generate-id(..) = generate-id(/)

在XPath 2.0(XSLT 2.0)中

.. is root()