获取元素的XPath列表

时间:2015-05-07 23:53:01

标签: java xml xslt xpath

我有具有特定名称的元素的NodeList,我想拥有所有theese节点的XPath。

我找不到办法如何做到这一点。

NodeList nList = doc.getElementsByTagName("definition");
 for (int i = 0; i < nList.getLength(); i++) {
 Node node = nList.item(i);
 System.out.println(node.GET_XPATH());
}

我正在寻找像GET_XPATH()

这样的方法

有人知道怎么做吗?或者甚至可能吗?

如果可以使用XSLT,也可以使用它,但更喜欢有人在Java中了解这种可能性。

原因: 我需要一组指向XML库的指针。用于定义元素的指针。

实施例 输入:

<odML>
    <section>
        <type>experiment</type>
        <name>Experiment</name>
        <definition></definition>
        <property>
            <definition></definition>
        </property>
        <property>
            <definition></definition>
            <value>
                <definition></definition>
            </value>
        </property>
    </section>
    <definition></definition>
</odML>

输出:

/odML/section/definition

/odML/section/property[1]/definition

/odML/section/property[2]/definition

/odML/section/property[2]/value/definition

/odML/definition

2 个答案:

答案 0 :(得分: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:strip-space elements="*"/>

<xsl:template match="/"> 
    <xsl:for-each select="//definition">
        <xsl:for-each select="ancestor::*">
            <xsl:text>/</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:if test="(preceding-sibling::*|following-sibling::*)[name()=name(current())]">
                <xsl:text>[</xsl:text>  
                <xsl:value-of select="count(preceding-sibling::*[name()=name(current())]) + 1"/>
                <xsl:text>]</xsl:text>  
            </xsl:if>
        </xsl:for-each>
        <xsl:text>/definition</xsl:text>    
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>  
        </xsl:if>
    </xsl:for-each>
</xsl:template>  

</xsl:stylesheet>

应用于您的示例输入时,将返回:

/odML/section/definition
/odML/section/property[1]/definition
/odML/section/property[2]/definition
/odML/section/property[2]/value/definition
/odML/definition

答案 1 :(得分:1)

此转化

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

  <xsl:template match="definition">
     <xsl:apply-templates select="ancestor-or-self::*" mode="path"/>
     <xsl:text>&#xA;</xsl:text>
  </xsl:template>

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

应用于提供的源XML文档

<odML>
    <section>
        <type>experiment</type>
        <name>Experiment</name>
        <definition></definition>
        <property>
            <definition></definition>
        </property>
        <property>
            <definition></definition>
            <value>
                <definition></definition>
            </value>
        </property>
    </section>
    <definition></definition>
</odML>

生成想要的正确结果

/odML/section/definition
/odML/section/property[1]/definition
/odML/section/property[2]/definition
/odML/section/property[2]/value/definition
/odML/definition
相关问题