Getting XSLT Current Node formatted as an XPath request?

I have the following code block that gets the name of the nodes down the tree, for example:

section/page/subPage

But I would like to get it before the following (only to create it):

section[@id='someId']/page/subPage[@user='UserA']/@title

I found the following code from one of these StackOverflow posts:


<xsl:attribute name="path">
  <xsl:for-each select="ancestor-or-self::*">
    <xsl:if test="name() != 'root'">
      <xsl:value-of select="name()">
        <xsl:if test="not(position()=last())">
          <xsl:text>/</xsl:text>
        </xsl:if>
      </xsl:value-of>
    </xsl:if>
  </xsl:for-each>
</xsl:attribute>

Which gives me a direct path, but I would like to use more logic for this, so that it includes @id (or the corresponding attribute) and maybe something else that I canโ€™t think of right now.

What is the best way to do this?

I checked the EXSLT functions that might work, but maybe you guys already solved this problem better.

Any ideas?

I use ruby โ€‹โ€‹nokogiri to parse xml / xslt if that helps.

Thank you very much spear

+3
2

, :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" />

  <xsl:template match="/">
    <root>
      <xsl:attribute name="path">
        <xsl:apply-templates select="(//@title)[1]" mode="make-path" />
      </xsl:attribute>
    </root>
  </xsl:template>

  <xsl:template match="*|@*" mode="make-path">
    <xsl:apply-templates select="parent::*" mode="make-path" />
    <xsl:text>/</xsl:text>
    <xsl:apply-templates select="." mode="make-name" />
    <xsl:choose>
      <xsl:when test="self::section">
        <xsl:apply-templates select="@id" mode="make-predicate" />
      </xsl:when>
      <xsl:when test="self::subPage">
        <xsl:apply-templates select="@user" mode="make-predicate" />
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*|@*" mode="make-predicate">
    <xsl:text>[</xsl:text>
    <xsl:apply-templates select="." mode="make-name" />
    <xsl:text> = '</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>']</xsl:text>
  </xsl:template>

  <xsl:template match="*" mode="make-name">
    <xsl:value-of select="name()" />
  </xsl:template>

  <xsl:template match="@*" mode="make-name">
    <xsl:text>@</xsl:text>
    <xsl:value-of select="name()" />
  </xsl:template>

</xsl:stylesheet>

<section id="someId">
  <page>
    <subPage user="UserA" title="test" />
    <subPage user="UserB" title="blah" />
  </page>
  <page>
    <subPage user="UserC" title="fooh" />
  </page>
</section>

:

<root path="/section[@id = 'someId']/page/subPage[@user = 'UserA']/@title" />

<xsl:choose> - ( <xsl:when>):

<!-- test for element name -->
<xsl:when test="self::section">
  <!-- make predicates out of selected attributes -->
  <xsl:apply-templates select="@id" mode="make-predicate" />
</xsl:when>

:

<xsl:when test="self::section">
  <xsl:apply-templates select="@name|@category|subElement" mode="make-predicate" />
</xsl:when>

<root path="/section[@name = 'someName'][@category = 'somecat'][subElement = 'xyz']/..." />

, , - , . XPath.

+4

node xsl: for-each

<xsl:for-each select="@*">

XPath

<xsl:for-each select="ancestor-or-self::*">
   <xsl:if test="name() != 'root'">
      <xsl:value-of select="name()"/>
      <xsl:if test="@*">
         <xsl:text>[</xsl:text>
         <xsl:for-each select="@*">
            <xsl:text>@</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text>='</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>'</xsl:text>
            <xsl:if test="not(position()=last())">
               <xsl:text> and </xsl:text>
            </xsl:if>
         </xsl:for-each>
         <xsl:text>]</xsl:text>
      </xsl:if>
      <xsl:if test="not(position()=last())">
         <xsl:text>/</xsl:text>
      </xsl:if>
   </xsl:if>
</xsl:for-each>
+1

Source: https://habr.com/ru/post/1718874/


All Articles