Subject: RE: [xsl] FW: Path Reversal From: Jarno.Elovirta@xxxxxxxxx Date: Tue, 29 Jan 2002 09:34:48 +0200 |
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 XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] FW: Path Reversal, McNally, David | Thread | [xsl]: Context inside nested for-ea, TSchutzerWeissmann |
RE: [xsl] Simple attribute value re, Edward . Middleton | Date | Re: Re: [xsl] How to select element, Rogier Hofboer |
Month |