XSLT中*和node()之间的区别

Svi*_*ish 24 xslt templates matching

这两个模板之间有什么区别?

<xsl:template match="node()">

<xsl:template match="*">
Run Code Online (Sandbox Code Playgroud)

Dim*_*hev 38

<xsl:template match="node()">
Run Code Online (Sandbox Code Playgroud)

是以下内容的缩写:

<xsl:template match="child::node()">
Run Code Online (Sandbox Code Playgroud)

这匹配可通过the child::轴选择的任何节点类型:

  • 元件

  • 文本节点

  • 处理指令(PI)节点

  • 评论节点.

在另一边:

<xsl:template match="*">
Run Code Online (Sandbox Code Playgroud)

是以下内容的缩写:

<xsl:template match="child::*">
Run Code Online (Sandbox Code Playgroud)

这匹配任何元素.

XPath表达式:someAxis ::*匹配给定轴的主节点类型的任何节点.

对于child::轴,主节点类型是元素.


Stu*_*tLC 14

只是为了说明其中一个差异,即*不符合text:

给定xml:

<A>
    Text1
    <B/>
    Text2
</A>
Run Code Online (Sandbox Code Playgroud)

匹配 node()

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <!--Suppress unmatched text-->
    <xsl:template match="text()" />

    <xsl:template match="/">
        <root>
            <xsl:apply-templates />
        </root>
    </xsl:template>

    <xsl:template match="node()">
        <node>
            <xsl:copy />
        </node>
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

得到:

<root>
    <node>
        <A />
    </node>
    <node>
        Text1
    </node>
    <node>
        <B />
    </node>
    <node>
        Text2
    </node>
</root>
Run Code Online (Sandbox Code Playgroud)

匹配*:

<xsl:template match="*">
    <star>
        <xsl:copy />
    </star>
    <xsl:apply-templates />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

与文本节点不匹​​配.

<root>
  <star>
    <A />
  </star>
  <star>
    <B />
  </star>
</root>
Run Code Online (Sandbox Code Playgroud)

  • `*`也不匹配注释节点,处理指令节点,属性节点,命名空间节点和文档节点......模式或表达式`*`(就其本身而言,仅作为`child ::*`的缩写)**永远匹配元素节点和元素节点**.当使用`@*`,`attribute ::*`的缩写时,星号匹配属性轴上的_only_属性节点. (2认同)