首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >node()的显式版本是什么

node()的显式版本是什么
EN

Stack Overflow用户
提问于 2013-04-23 04:16:40
回答 1查看 64关注 0票数 1

是大家熟知的XSLT 1.0身份模板

代码语言:javascript
复制
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

的同义词

代码语言:javascript
复制
<xsl:template match="/|@*|*|processing-instruction()|comment()|text()">
  <xsl:copy>
    <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()"/>
  </xsl:copy>
</xsl:template>

也就是说,node()在match语句中包含/而在select语句中不包含/是正确的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-23 05:33:15

根据是在match属性中还是在select属性中,node()节点测试没有不同的行为。身份模板的扩展版本如下所示:

代码语言:javascript
复制
<xsl:template match="@*|*|processing-instruction()|comment()|text()">
  <xsl:copy>
    <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()"/>
  </xsl:copy>
</xsl:template>

node()节点测试可以匹配任何节点,但是当没有给它一个显式的轴时,它默认位于child::轴上。因此,模式match="node()"与文档根或属性不匹配,因为它们不在任何节点的子轴上。

您可以观察到身份模板与根节点不匹配,因为这没有输出:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="@* | node()">
      <xsl:if test="count(. | /) = 1">
        <xsl:text>Root Matched!</xsl:text>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>

并输出"Root Matched!":

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="@* | node() | /">
      <xsl:if test="count(. | /) = 1">
        <xsl:text>Root Matched!</xsl:text>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>

通过在任何具有属性的文档上运行以下代码,可以验证node()测试是否应用于根节点和属性:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="node()">
    <xsl:apply-templates select="@* | node()" />
  </xsl:template>

  <xsl:template match="/">
    <xsl:if test="self::node()">
      node() matches the root!
    </xsl:if>
    <xsl:apply-templates select="@* | node()" />
  </xsl:template>

  <xsl:template match="@*">
    <xsl:if test="self::node()">
      node() matches an attribute!
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

下面是观察node()测试是否应用于根节点的另一种方法:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/*">
    <xsl:value-of select="concat('The root element has ', count(ancestor::node()), 
                                 ' ancestor node, which is the root node.')"/>
  </xsl:template>
</xsl:stylesheet>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16155940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档