[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: RE: FW: Path Reversal

Subject: Re: RE: FW: Path Reversal
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 29 Jan 2002 00:44:10 -0800 (PST)
veryfunnyjoke
For easy extraction of all the "steps" in an XPath expression with
unknown in advance number of "steps" one can re-use an existing
template -- the Functional Tokenizer:

http://aspn.activestate.com/ASPN/Mail/Message/xsl-list/914654

Cheers,
Dimitre Novatchev.


Jarno.Elovirta@xxxxxxxxx  wrote:

Hip hei,

> Suppose I have the following piece of XML:
> 
> <node name="funstuff">
>   <node name="jokes">
>     <node name="veryfunnyjoke"/>
>   </node>
> </node>
> 
> I have a path (as a string) which is composed of the @names of the
> nodes. It looks like /funstuff/jokes/veryfunnyjoke. How can I write a
> template (or an EXSLT function) that returns the node the 
> path points to
> (e.g. veryfunnyjoke)?

With XSLT 1.0 you can't return the node, but you can write a
named/moded template which is called for the node you want to return.
David McNally already posted a solution, but here's another one with
separate templates to evaluate the step and process the return value -
basically they're the same solution, really.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="/">
	<xsl:text>
/funstuff/jokes2/veryfunnyjoke  = </xsl:text>
  <xsl:call-template name="parser">
    <xsl:with-param name="path"
select="'/funstuff/jokes2/veryfunnyjoke'" />
  </xsl:call-template>
  <xsl:text>
/funstuff/jokes/veryfunnyjoke2  = </xsl:text>
  <xsl:call-template name="parser">
    <xsl:with-param name="path"
select="'/funstuff/jokes/veryfunnyjoke2'" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="parser">
  <xsl:param name="path" />

  <xsl:choose>
    <xsl:when test="contains($path, '/')">
      <xsl:choose>
        <xsl:when test="starts-with($path, '/')">
          <xsl:for-each select="/">
            <xsl:call-template name="step">
              <xsl:with-param name="test"
select="substring-before(substring-after($path, '/'), '/')" />
              <xsl:with-param name="path"
select="substring-after(substring-after($path, '/'), '/')" />
            </xsl:call-template>            
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="step">
            <xsl:with-param name="test" select="substring-before($path,
'/')" />
            <xsl:with-param name="path" select="substring-after($path,
'/')" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$path != ''">
      <xsl:call-template name="step">
        <xsl:with-param name="test" select="$path" />
        <xsl:with-param name="path" select="''" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="return" />        
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- here always forward $path to parser -->
<xsl:template name="step" >
  <xsl:param name="test" />
  <xsl:param name="path" />

  <xsl:for-each select="node[@name = $test]">
    <xsl:call-template name="parser">
      <xsl:with-param name="path" select="$path" />
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template name="return">
  <xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>

Hope this helps in writing the stylesheet you need,

Santtu




__________________________________________________
Do You Yahoo!?
Great stuff seeking new owners in Yahoo! Auctions! 
http://auctions.yahoo.com

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.