;) Was: Re: Accessing the element stack generically

Subject: ;) Was: Re: Accessing the element stack generically
From: "Clark C. Evans" <clark.evans@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 15 Oct 1999 13:09:01 -0400 (EDT)
I'm happy.  XSL is slick.

Problem:
~~~~~~~

For particular nodes visited, I'd like to print
out their location.

Example:
~~~~~~~

With a given document:

     <a><b x="y"><c/></b><b x="z" p="q"><c/></b></a>

Produce output:

    <c-node location="/a/b[@x='y']/c" />
    <c-node location="/a/b[@x='z'][@p='q']/c" />

Solution:
~~~~~~~~

<xsl:template match="c">
  <xsl:element name="c-node" >
    <xsl:attribute name="location">
      <xsl:for-each select="ancestor-or-self::*">
        <xsl:value-of select="concat('/',name())"/>
         <xsl:for-each select="attribute::*">
             <xsl:text>[@</xsl:text><xsl:value-of select="name()" />
             <xsl:text>='</xsl:text><xsl:value-of select="."      />
             <xsl:text>']</xsl:text>
         </xsl:for-each>
      </xsl:for-each>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

The default sort order is "document order", thus there
is no problem with portability.

Right?

Clark   





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread