Get full XPath from partial

时光怂恿深爱的人放手 提交于 2019-12-05 18:32:25

looking for perl solution or any other working things

This XPath 2.0 expression:

string-join(for $node in ancestor-or-self::node()
            return concat(('@')[$node/self::attribute()],
                          $node/name(),
                          (concat('[',
                                  count($node/preceding-sibling::node()
                                           [name()=$node/name()]) + 1,
                                  ']'))[$node/../node()
                                           [name()=$node/name()][2]]),
            '/')

Edit: Shorter expression.

This XSLT 1.0 transformation produces an XPath expression for every node contained in the $pNode parameter:

<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:template match="node()|@*">
  <path>
    <xsl:call-template name="buildPath"/>
  </path>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:template>


<xsl:template name="buildPath">
 <xsl:variable name="pNode" select="."/>
  <xsl:variable name="theResult">
    <xsl:for-each select="$pNode">
    <xsl:variable name="theNode" select="."/>
    <xsl:for-each select=
    "$theNode
    |
     $theNode/ancestor-or-self::node()[..]">
      <xsl:element name="slash">/</xsl:element>
      <xsl:choose>
        <xsl:when test="self::*">
          <xsl:element name="nodeName">
            <xsl:value-of select="name()"/>
            <xsl:variable name="thisPosition" select=
            "count(preceding-sibling::*
                   [name(current())
                   =
                    name()])"/>
            <xsl:variable name="numFollowing" select=
             "count(following-sibling::
                     *[name(current())
                     =
                       name()])"/>
            <xsl:if test="$thisPosition + $numFollowing > 0">
              <xsl:value-of select=
              "concat('[', $thisPosition +1, ']')"/>
            </xsl:if>
          </xsl:element>
        </xsl:when>
        <xsl:otherwise> <!-- This node is not an element -->
          <xsl:choose>
            <xsl:when test="count(. | ../@*) = count(../@*)">
            <!-- Attribute -->
              <xsl:element name="nodeName">
                <xsl:value-of select="concat('@',name())"/>
              </xsl:element>
            </xsl:when>
            <xsl:when test="self::text()">  <!-- Text -->
              <xsl:element name="nodeName">
                <xsl:value-of select="'text()'"/>
                <xsl:variable name="thisPosition"
                          select="count(preceding-sibling::text())"/>
                <xsl:variable name="numFollowing"
                          select="count(following-sibling::text())"/>
                <xsl:if test="$thisPosition + $numFollowing > 0">
                  <xsl:value-of select=
                  "concat('[', $thisPosition +1, ']')"/>
                </xsl:if>
              </xsl:element>
            </xsl:when>
            <xsl:when test="self::processing-instruction()">
            <!-- Processing Instruction -->
              <xsl:element name="nodeName">
                <xsl:value-of select="'processing-instruction()'"/>
                <xsl:variable name="thisPosition"
                   select="count(preceding-sibling::processing-instruction())"/>
                <xsl:variable name="numFollowing"
                    select="count(following-sibling::processing-instruction())"/>
                <xsl:if test="$thisPosition + $numFollowing > 0">
                  <xsl:value-of select=
                  "concat('[', $thisPosition +1, ']')"/>
                </xsl:if>
              </xsl:element>
            </xsl:when>
            <xsl:when test="self::comment()">   <!-- Comment -->
              <xsl:element name="nodeName">
                <xsl:value-of select="'comment()'"/>
                <xsl:variable name="thisPosition"
                         select="count(preceding-sibling::comment())"/>
                <xsl:variable name="numFollowing"
                         select="count(following-sibling::comment())"/>
                <xsl:if test="$thisPosition + $numFollowing > 0">
                  <xsl:value-of select=
                  "concat('[', $thisPosition +1, ']')"/>
                </xsl:if>
              </xsl:element>
            </xsl:when>
            <!-- Namespace: -->
            <xsl:when test=
              "count(. | ../namespace::*)
              =
               count(../namespace::*)">

              <xsl:variable name="apos">'</xsl:variable>
              <xsl:element name="nodeName">
                <xsl:value-of select="concat('namespace::*',
                '[local-name() = ', $apos, local-name(), $apos, ']')"/>

              </xsl:element>
            </xsl:when>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <!-- <xsl:text>&#xA;</xsl:text> -->
  </xsl:for-each>
 </xsl:variable>
 <xsl:value-of select="$theResult"/>
</xsl:template>
</xsl:stylesheet>

when applied on the following XML document:

<div id="entry-1" class="item-asset asset hentry">
  <div class="asset-header">
    <h2 class="asset-name entry-title">
      <a rel="bookmark" href="http://blahblah.com/paper-scissors">Paper Scissors</a>
    </h2>
  </div>
  <div class="asset-content entry-content">
    <div class="asset-body">
     <p>Paper and scissors</p>
    </div>
  </div>
</div>

the result is a list of the XPath expressions for every node in the document:

<path>/div</path>
<path>/div/@id</path>
<path>/div/@class</path>
<path>/div/div[1]</path>
<path>/div/div[1]/@class</path>
<path>/div/div[1]/h2</path>
<path>/div/div[1]/h2/@class</path>
<path>/div/div[1]/h2/a</path>
<path>/div/div[1]/h2/a/@rel</path>
<path>/div/div[1]/h2/a/@href</path>
<path>/div/div[1]/h2/a/text()</path>
<path>/div/div[2]</path>
<path>/div/div[2]/@class</path>
<path>/div/div[2]/div</path>
<path>/div/div[2]/div/@class</path>
<path>/div/div[2]/div/p</path>
<path>/div/div[2]/div/p/text()</path>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!