【问题标题】:Generate xpath of XML using XSLT for all Nodes使用 XSLT 为所有节点生成 XML 的 xpath
【发布时间】:2015-08-15 21:10:33
【问题描述】:

我试图找到一种方法将 XML 标记转换为它们各自的唯一地址,例如 xPath。 Nd 我找到了一个 XSLT,其中处理了 XML,并且仅为没有子元素和属性的节点创建了唯一地址。 [链接]:Generate/get xpath from XML node java

XSLT:

<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:value-of select="concat('=',$vApos,.,$vApos)"/>
         <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="vnumPrecSiblings" select=
         "count(preceding-sibling::*[name()=name(current())])"/>
        <xsl:if test="$vnumPrecSiblings">
            <xsl:value-of select="concat('[', $vnumPrecSiblings +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>

当传递给这个时

    <?xml version="1.0" encoding="UTF-8"?>
<root>
    <main>
        <tag1>001</tag1>
        <tag2>002</tag2>
        <tag3>
        <tag4>004</tag4>
        </tag3>
        <tag2>002</tag2>
        <tag5>005</tag5>
    </main>
</root>

生产

/root/main/tag1='001' /root/main/tag2='002' /root/main/tag3/tag4='004' /root/main/tag2[2]='002' /root/main/tag5='005'

所以我需要xslt按以下方式生成

/root
/root/main
/root/main/tag1
/root/main/tag2
/root/main/tag3
/root/main/tag3/tag4
/root/main/tag2[2]
/root/main/tag5

我也不需要价值观。所以请帮我解决这个问题

【问题讨论】:

    标签: java xml xslt xpath xml-parsing


    【解决方案1】:

    您的结果可以很简单地通过以下方式产生:

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    
    <xsl:template match="*">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="name()" />
            <xsl:variable name="i" select="count(preceding-sibling::*[name()=name(current())])"/>
            <xsl:if test="$i">
                <xsl:value-of select="concat('[', $i + 1, ']')"/>
            </xsl:if>
            <xsl:if test="position()!=last()">
                <xsl:text>/</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
        <xsl:apply-templates select="*"/>
    </xsl:template>
    
    </xsl:stylesheet>
    

    请注意,这不会处理属性(或元素以外的任何其他类型的节点)。

    【讨论】:

    • @DilipNs 如果您的问题得到解答,请通过接受答案将其关闭。
    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2011-06-12
    • 2019-11-06
    • 2014-08-17
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多