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

Re: variables

Subject: Re: variables
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 31 May 2001 12:13:05 +0100
evaluate xsl variable
Hi André,

> I want to create one variable that contains all nodes of the
> source-document specified by the loction-paths in the path-elements
> (path1,path2,...).
>
> I know how to do this with one path-element (e.g. path1), the
> solution would be
>
> <xsl:variable name="doc2" select="document('doc2.xml)">
> <xsl:variable name="var1" select="xalan:evaluate(string($doc2/path1))">.
>
> But I don't know how to do this with more than one path-element. A
> restriction is that the number of path elements could vary. Any help
> would be welcome. Thanks in advance

Well, you can build a recursive template that takes two arguments -
the paths that you want to evaluate and the current result of
evaluating the paths:

<xsl:template name="evaluate-paths">
   <xsl:param name="paths" select="document('doc2.xml')/paths/*" />
   <xsl:param name="result" select="/.." />
   ...
</xsl:template>

[Note: it's not clear what the structure of 'doc2.xml' is, so I've
assumed that it's got 'paths' as a document element and that the paths
are specified in elements of various names underneath that - you may
want to change that location path.]

Within the template, if you have no more paths to evaluate, then
you're done, and you can set the variable to the result, and use that
variable in whatever way you need to use it:

<xsl:template name="evaluate-paths">
   <xsl:param name="paths" select="document('doc2.xml')/paths/*" />
   <xsl:param name="result" select="/.." />
   <xsl:choose>
      <xsl:when test="not($paths)">
         <xsl:variable name="var1" select="$result" />
         ...
      </xsl:when>
      <xsl:otherwise>
         ...
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

Otherwise, you want to move on to the next recursion by unioning the
result of evaluating the first path in the set to the result that you
have so far, and removing that path from the set of paths left to
evaluate:

<xsl:template name="evaluate-paths">
   <xsl:param name="paths" select="document('doc2.xml')/paths/*" />
   <xsl:param name="result" select="/.." />
   <xsl:choose>
      <xsl:when test="not($paths)">
         <xsl:variable name="var1" select="$result" />
         ...
      </xsl:when>
      <xsl:otherwise>
         <xsl:call-template name="evaluate-paths">
            <xsl:with-param name="paths"
                            select="$paths[position() > 1]" />
            <xsl:with-param name="result"
                            select="$result |
                                    xalan:evaluate($paths[1])" />
         </xsl:call-template>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

Note that you have to use the value of the $var1 variable (holding the
result of evaluating all the paths) within the template itself -
there's no way of returning this value to have it held within a
variable as a node set unless:

  (a) you don't care about the context those nodes are in, in which
      case you could have the template return a copy of the result
      nodes:

      <xsl:when test="not($paths)">
         <xsl:copy-of select="$result" />
      </xsl:when>

      and have the $var1 variable be set to the result of calling the
      template and converting the result to a node set:

      <xsl:variable name="var1-rtf">
         <xsl:call-template name="evaluate-paths" />
      </xsl:variable>
      <xsl:variable name="var1" select="xalan:nodeSet($var1-rtf)" />

  (b) you're prepared to go to extraordinary lengths to reaccess the
      actual nodes - you can return an RTF of node elements that use
      the unique generated ID of the nodes to identify the nodes that
      you actually want, and then resolve those generated IDs to find
      the nodes you're after.
      
  (b) you can use a processor that supports EXSLT - Functions, in
      which case you can use the same kind of code to define a
      function and return the result of calling that function as a
      node set for the $var1 variable.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.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.