【问题标题】:Get list of XPaths of elements [duplicate]获取元素的 XPath 列表 [重复]
【发布时间】:2015-05-07 23:53:00
【问题描述】:

我有具有特定名称的元素的 NodeList,并且我想要所有这些节点的 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

【问题讨论】:

  • 对不起什么? xPath 是用于对文档或节点执行查询的查询语言...我不确定您希望实现什么
  • 我需要逆向处理。我有一组 XML,我需要每个 XML 都有一个用于定义元素的 XPath 列表......定义元素应该几乎在文档中的任何地方。而且我需要知道,在 XML XYZ 中,例如 /odML/section/property[1]/definition 上有这个元素...
  • 在某些编辑器中,您可以右键单击任何元素,您可以获得该元素的 XPath。如果第一条评论不清楚,可以说我需要做类似的功能。
  • 是的,我很确定你需要自己构建这个(或者可能有一个可以提供帮助的库),但我很确定它不在默认库中
  • 这在 XSLT 中可能是可能的。当您发布输入 XML 和预期输出的示例时,我们会确定。

标签: java xml xslt xpath


【解决方案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

【讨论】:

    【解决方案2】:

    这种转变

    <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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-09
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      相关资源
      最近更新 更多