是元素节点的属性节点子节点

Bre*_*min 2 xml xslt xpath

考虑以下示例:

XML:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="03-01.xsl"?>

<ancient_wonders>

    <wonder myattribute = "Green">
        <name language="English">Colossus of Rhodes1</name>
    </wonder>

    <wonder myattribute = "Red">
        <name language="English">Colossus of Rhode2s</name>
    </wonder>

</ancient_wonders>
Run Code Online (Sandbox Code Playgroud)

XSL:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <!-- Output Method -->
    <xsl:output method="html"/>

    <!-- Root Template -->
    <xsl:template match="/">

        <html>
            <body>

                <p>Output 1 </p>
                <xsl:for-each select="//name//*">
                    <xsl:value-of select = "."/>
                </xsl:for-each>

                <p>Output 2</p>
                 <xsl:for-each select="//name/@*">
                    <xsl:value-of select = "."/>
                </xsl:for-each>

                <p>Output 3</p>
                 <xsl:for-each select="//name/*">
                    <xsl:value-of select = "."/>
                </xsl:for-each>

            </body>
        </html>

    </xsl:template>


</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

(您可以在此处看到输出)

现在,我们看到name具有属性。language 在这种情况下,languagenode的子节点node是name吗?如果是,为什么我在输出中看不到它(上面的链接)?

mic*_*57k 5

该表达式elem/*是以下内容的缩写:

elem/child::*
Run Code Online (Sandbox Code Playgroud)

它选择所有子元素elem。为什么只有元素?因为:

对于主体节点类型的任何节点,节点测试*为true。

和:

如果轴可以包含元素,则主要节点类型为element;

https://www.w3.org/TR/1999/REC-xpath-19991116/#node-tests


属性仅在attribute轴上可用,而在子轴上不可用:

每个元素节点都有一组关联的属性节点;元素是每个这些属性节点的父级;但是,属性节点不是其父元素的子元素
(添加了重点)

https://www.w3.org/TR/1999/REC-xpath-19991116/#attribute-nodes


在属性轴上,主要节点类型是属性,因此:

elem/attribute::*
Run Code Online (Sandbox Code Playgroud)

(可以缩写为elem/@*)选择的所有属性elem