如何从XML获取所有叶元素的xpath?

the*_*ric 11 xml xslt xpath

我想知道是否可以创建一个XSLT样式表,它将为给定XML文件中的所有叶元素提取XPATH.例如

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item1>value1</item1>
    <subitem>
        <item2>value2</item2>
    </subitem>
</root>
Run Code Online (Sandbox Code Playgroud)

输出将是

/root/item1
/root/subitem/item2
Run Code Online (Sandbox Code Playgroud)

Kir*_*huk 17

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

    <xsl:output method="text" indent="no" />

    <xsl:template match="*[not(*)]">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat('/', name())"/>

            <xsl:if test="count(preceding-sibling::*[name() = name(current())]) != 0">
                <xsl:value-of select="concat('[', count(preceding-sibling::*[name() = name(current())]) + 1, ']')"/>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates select="*"/>
    </xsl:template>

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

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

输出:

/root/item1
/root/subitem/item2
Run Code Online (Sandbox Code Playgroud)


Dim*_*hev 9

这种转变:

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

        <xsl:variable name="vApos">'</xsl:variable>

        <xsl:template match="*[@* or not(*)] ">
          <xsl:if test="not(*)">
             <xsl:apply-templates select="ancestor-or-self::*" mode="path"/>
             <xsl:text>&#xA;</xsl:text>
            </xsl:if>
            <xsl:apply-templates select="@*|*"/>
        </xsl:template>

        <xsl:template match="*" mode="path">
            <xsl:value-of select="concat('/',name())"/>
            <xsl:variable name="vnumSiblings" select=
             "count(../*[name()=name(current())])"/>
            <xsl:if test="$vnumSiblings > 1">
                <xsl:value-of select=
                 "concat('[',
                         count(preceding-sibling::*
                                [name()=name(current())]) +1,
                         ']')"/>
            </xsl:if>
        </xsl:template>

        <xsl:template match="@*">
            <xsl:apply-templates select="../ancestor-or-self::*" mode="path"/>
            <xsl:value-of select="concat('[@',name(), '=',$vApos,.,$vApos,']')"/>
            <xsl:text>&#xA;</xsl:text>
        </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当应用于提供的XML文档时:

<root>
    <item1>value1</item1>
    <subitem>
        <item2>value2</item2>
    </subitem>
</root>
Run Code Online (Sandbox Code Playgroud)

产生想要的,正确的结果:

/root/item1
/root/subitem/item2
Run Code Online (Sandbox Code Playgroud)

使用此XML文档:

<root>
    <item1>value1</item1>
    <subitem>
        <item>value2</item>
        <item>value3</item>
    </subitem>
</root>
Run Code Online (Sandbox Code Playgroud)

它正确地产生:

/root/item1
/root/subitem/item[1]
/root/subitem/item[2]
Run Code Online (Sandbox Code Playgroud)

另请参阅此相关答案:https://stackoverflow.com/a/4747858/36305

  • @DimitreNovatchev,无论如何+1 (5认同)