[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: How to resolve the absolute location of a node


Subject: Re: How to resolve the absolute location of a node
From: "Clark C. Evans" <clark.evans@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 17 Oct 1999 23:49:15 -0400 (EDT)

Jarno,

I'm using this "absolute" location to name HTML 
form elements; and then using a servlet to 
transform a POST into an update on the given 
XML file.  Your suggestion to use "absolute"
entries is perfect.

A new-improved version is below (that also handles
attributes, thanks to Ken Holman.  

On Sun, 17 Oct 1999, Jarno Elovirta wrote:
> I'm trying to write a template that would return the absolute location path
> of the node i'm processing. e.g. with processing the c elements of
> >      <a><b x="y"><c/></b><b x="z" p="q"><c/></b></a>
> the templete would return
> /a/b[1]/c[1] and /a/b[2]/c[1]

This template returns:

/a[0001]/b[0001]/c[0001] and /a[0001]/b[0002]/c[0001]

I'm using a four digit pad so that I can sort the
key=value pairs in the HTTP/POST and make one pass
(using a SAX processor) at the XML document to update it.
I can have up to 99 children element pretty easily, so
I figured allowing for 9999 before the sort fails would
work just fine (and then I can throw an error)

...

<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nl "&#xd;&#xa;">]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">
<xsl:output method="text"/>

<xsl:template match="*">
<xsl:call-template name="resolver" />
<xsl:apply-templates/>
</xsl:template>

<xsl:template name="resolver" >
  <xsl:for-each select="ancestor-or-self::*">
    <xsl:variable name="id"   select="generate-id(.)" />
    <xsl:variable name="name" select="name()" />
    <xsl:value-of select="concat('/',name())"/>
    <xsl:for-each select="../*[name()=$name]" >
      <xsl:if test="generate-id(.)=$id">
        <xsl:text>[</xsl:text>
        <xsl:value-of select="format-number(position(),'0000')"/>
        <xsl:text>]</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
  <xsl:if test="not(self::*)">
    <xsl:value-of select="concat('/@',name())" />
  </xsl:if> 
</xsl:template>

</xsl:stylesheet>





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



Current Thread
Keywords
xml