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

Recursive evaluation of elements

Subject: Recursive evaluation of elements
From: "Rowan Sylvester-Bradley" <rowan@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 12 Dec 2009 00:08:18 -0000
 Recursive evaluation of elements
I have a stylesheet that now successfully expands sub-trees using multiple
"rows" from "tables" (the rows and tables are all part of the input XML
file). I can have one expand inside another giving a two-dimensional

Another feature of the system that I'm building is that in the contents of
an element of the input file I can include an XPath expression surrounded by
braces. My stylesheet recognises the braces using analyze-string, and
evaluates the expression within them using saxon:eval. Parameters $p1 and
$p2 allow me to refer in these expressions to elements in the source tree
and in the current table row. 

This all works fine unless I have an expression in an inner expansion that
refers to the result of an expression in an outer expansion. In this case
what I need to happen is that the inner expression uses as its parameter the
result of the outer expression. What actually happens is the inner
expression uses the literal outer expression, without evaluation. I can see
that this is because the $source variable is set to a subtree from the
source document. But I can't see how to get it to do what I want. I tried
putting an apply-templates in several places, and immediately got a stack
overflow error, even on a small test file.

Here's a simplified version of my stylesheet:

 <xsl:stylesheet version="2.0" 
   exclude-result-prefixes="xs saxon">

   <xsl:strip-space elements="*"/>
   <xsl:output indent="yes"/>
   <xsl:key name="t" match="table" use="id"/>
   <xsl:template match="*" mode="#all">
   <xsl:template match="tables" mode="#all"/>
   <xsl:template match="item[expand]" mode="#all">
      <xsl:variable name="here" select="."/>
      <xsl:for-each select="key('t',expand)/row">  
            <!-- <xsl:apply-templates select="$here/(* except
item),*,$here/item" mode="expand"> -->
            <xsl:apply-templates select="$here/*" mode="expand">  
               <xsl:with-param name="source" select="$here" tunnel="yes"/>
               <xsl:with-param name="tablerow" select="." tunnel="yes"/>   
   <xsl:template match="expand" mode="expand"/>
   <xsl:template match="text()" priority="2" mode="#all">
      <xsl:param name="source" tunnel="yes"/>
      <xsl:param name="tablerow" tunnel="yes"/>
      <xsl:analyze-string select="." regex="\{{([^\}}]+)\}}">
select="saxon:eval(saxon:expression(regex-group(1)), $tablerow, $source)"/>
            <xsl:value-of select="."/>


And here's a sample input file:

         <name>{concat('Item A', ' result')} of expression</name>
         <price>{$p1/price * 2}</price>
         <name>Item B</name>
         <price>{$p1/price * 2}</price>
            <name>{concat($p2/../name, ' delivered by ', $p1/name)}</name>
            <price>{$p1/price * 3}</price>
         <name>Item C ({$p1/name})</name>
         <price>{$p1/price * 2}</price>
            <name>{concat($p2/../name, ' delivered by ', $p1/name)}</name>
            <!-- <price>{$p2/../price + $p1/price}</price> -->
            <name>A nasty cheap one</name>
            <name>This one's gold plated</name>
            <name>Second Class</name>
            <name>First Class</name>
Items 'a' and 'b' expand fine, and the braces expressions evaluate

Item 'c' expands OK, but the value of the name element in item c1 is:
<name>Item C ({$p1/name}) delivered by Second Class</name>

What I need is:
<name>Item C (A nasty cheap one) delivered by Second Class</name>

Can anyone suggest how to do this?

Many thanks - Rowan

Current Thread


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.
First Name
Last Name
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.