views:

447

answers:

2

Hi there,

I have the following block of code that gets the name of the nodes down the tree, like this:

section/page/subPage

But I would like to be able to get it down to the following (just making it up):

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 the straight path, but I would like to run more logic on it to make it so it included the @id (or relevant attribute), and maybe some more stuff I can't think of at the moment.

What is the best way to do this?

I've checked into EXSLT functions, which may work, but maybe you guys have already solved this problem a better way.

Any ideas?

I'm using ruby's nokogiri to parse the xml/xslt if that helps.

Thanks a lot, Lance

+1  A: 

For each ancestor node you can loop over all attributes with simple xsl:for-each

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

You could then use the attributes to build up the XPath string

<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>
Tim C
+2  A: 

This solution does what you want:

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

  <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>

When applied to

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

you get:

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

The <xsl:choose> is the configurable spot (add as many <xsl:when>s as you like):

<!-- 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>

also possible:

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

which would lead to

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

The only issue I see is with predicate values that contain single quotes. They would break the XPath.

Tomalak
Nice use of mode
Teun D
Hardcore man, thanks for this.
viatropos