Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 用xslt生成xpath_Xml_Xslt_Xpath - Fatal编程技术网

Xml 用xslt生成xpath

Xml 用xslt生成xpath,xml,xslt,xpath,Xml,Xslt,Xpath,我正在尝试为xml文档中的元素生成XPath。我发现这个解决方案非常接近我所需要的,只是我希望它列出一条路径中的所有元素属性,而不是重新生成同一条路径。比如说 /root/elemA[2][@attribute1='first'][@attribute2='second'] 而不是 /root/elemA[2][@attribute1='first'] /root/elemA[2][@attribute2='second'] 我是xslt新手,一直在使用这个模板,但我似乎不知道如何更改输出。

我正在尝试为xml文档中的元素生成XPath。我发现这个解决方案非常接近我所需要的,只是我希望它列出一条路径中的所有元素属性,而不是重新生成同一条路径。比如说

/root/elemA[2][@attribute1='first'][@attribute2='second']
而不是

/root/elemA[2][@attribute1='first']
/root/elemA[2][@attribute2='second']
我是xslt新手,一直在使用这个模板,但我似乎不知道如何更改输出。

试试这个

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="text()"/>

    <xsl:template match="*">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat('/',local-name())"/>
            <!--Predicate is only output when needed.-->
            <xsl:if test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
                <xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/>
            </xsl:if>
            <!--Output attributes.-->
            <xsl:if test="@*">
                <xsl:text>[</xsl:text>
                <xsl:apply-templates select="@*"/>
                <xsl:text>]</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates select="node()"/>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:if test="position() != 1">
            <xsl:text>][</xsl:text>
        </xsl:if>
        <xsl:value-of select="concat('@',local-name(),'=&quot;',.,'&quot;')"/>
    </xsl:template>

</xsl:stylesheet>
编辑

这是一个基于注释的更新XSLT,如果它是子对象,是否仍要使其仅输出属性?例如
/body/text[@attr='1']
,然后
/body/text/h1[@attr='1']


[
]

;
][

这非常接近!!如果它是子对象,是否有办法只输出属性?例如/body/text[@attr='1']和/body/text/h1[@attr='1']@Sean-我添加了一个更新的XSLT,只输出原始上下文的属性。
<root>
    <elemA>one</elemA>
    <elemA attribute1='first' attribute2='second'>two</elemA>
    <elemB>three</elemB>
    <elemA>four</elemA>
    <elemC>
        <elemB>five</elemB>
    </elemC>
</root>
/root
/root/elemA[1]
/root/elemA[2][@attribute1="first"][@attribute2="second"]
/root/elemB
/root/elemA[3]
/root/elemC
/root/elemC/elemB
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="text()"/>

    <xsl:template match="*">
        <xsl:variable name="id" select="generate-id()"/>
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat('/',local-name())"/>
            <!--Predicate is only output when needed.-->
            <xsl:if test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
                <xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/>
            </xsl:if>
            <!--Output attributes.-->
            <xsl:if test="@* and generate-id() = $id">
                <xsl:text>[</xsl:text>
                <xsl:apply-templates select="@*"/>
                <xsl:text>]</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates select="node()"/>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:if test="position() != 1">
            <xsl:text>][</xsl:text>
        </xsl:if>
        <xsl:value-of select="concat('@',local-name(),'=&quot;',.,'&quot;')"/>
    </xsl:template>

</xsl:stylesheet>