This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: variables


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]