You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Axel Weiß <aw...@informatik.hu-berlin.de> on 2005/07/17 13:41:22 UTC

Evaluation of xsl:attribute unexpectedly fails

Hi all,

before I'm going to feed jira with a possibly invalid bugreport, I'd like 
to ask for the developer's opinions here.

What I want to do is wrapping a long sequence of template calls into a 
loop, like in the following example.

Beginning with a stylesheet of the form:

<xsl:stylesheet...>
  <xsl:template name="T1"> ...
    <xsl:param name="P1" .../>
    <xsl:param name="P2" .../>
    ... <!-- maybe a dozen params -->
  </xsl:template>
  <xsl:template match=...>
    <xsl:call-template name="T1">
        <xsl:with-param name="P1" select="value1_p1"/>
        <xsl:with-param name="P2" select="value1_p2"/>
        ...
    </xsl:call-template>
    <xsl:call-template name="T1">
        <xsl:with-param name="P1" select="value2_p1"/>
        <xsl:with-param name="P2" select="value2_p2"/>
        ...
    </xsl:call-template>
    ... <!-- maybe some dozens template calls -->
  </xsl:template>
</xsl:stylesheet>

I'd like to reduce the code length by one magnitude:

<xsl:stylesheet...>
  <xsl:variable name="globalSettings">
    <elem attr1="value1_p1" attr2="value1_p2" .../>
    <elem attr1="value2_p1" attr2="value2_p2" .../>
    ... <!-- maybe some dozens entries here -->
  </xsl:variable>
  <xsl:template name="T1"> ...
    <xsl:param name="P1" .../>
    <xsl:param name="P2" .../>
    ... <!-- maybe a dozen params -->
  </xsl:template>
  <xsl:template match=...>
    <xsl:for-each select="$globalSettings/elem">
      <xsl:call-template name="T1">
        <xsl:with-param name="P1" select="@attr1"/>
        <xsl:with-param name="P2" select="@attr2"/>
        ...
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Unfortunately, Xalan doesn't like the for-each evaluation of my 
globalSettings variable. The error message is like:
XSLT Error: The expression does not evaluate to a node-set.

Now, I'm wondering, if I'm doing a mistake here. Reading e.g. 
http://www.w3.org/TR/xslt.html#variables, I can't find any hint that 
this construct would be forbidden (but I can't find any 'positive' 
example, either).

The main reason why I want to do 'loop rolling' here is that I plan to 
have a couple of different templates that would make use of this 
construct with all the same global variable. If I want to make some 
(consistent) changes in the parameter values of the template calls, I 
only need to make the required changes with the global variable, instead 
of querying hundreds of lines of code.

My questions here are
- is 'loop rolling' a legal concept with XSLT?
- is the reaction of Xalan (The expression does not evaluate to a 
node-set.) a bug - or a feature?

Thanks in advance,

cheers,
			Axel

-- 
Humboldt-Universität zu Berlin
Institut für Informatik
Signalverarbeitung und Mustererkennung
Dipl.-Inf. Axel Weiß
Rudower Chaussee 25
12489 Berlin-Adlershof
+49-30-2093-3050
** www.freesp.de **

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: Evaluation of xsl:attribute unexpectedly fails

Posted by Lonnie VanZandt <lo...@ngc.com>.
And the other option is to use an XSLT 2.0 processor which supports such 
operations on result tree fragments...

On Sunday 17 July 2005 12:50 pm, david_n_bertoni@us.ibm.com wrote:
> > The main reason why I want to do 'loop rolling' here is that I plan to
> > have a couple of different templates that would make use of this
> > construct with all the same global variable. If I want to make some
> > (consistent) changes in the parameter values of the template calls, I
> > only need to make the required changes with the global variable, instead
> >
> > of querying hundreds of lines of code.
> >
> > My questions here are
> > - is 'loop rolling' a legal concept with XSLT?
> > - is the reaction of Xalan (The expression does not evaluate to a
> > node-set.) a bug - or a feature?
>
> Whenever you use xsl:variable with content, you are creating a result tree
> fragment (RTF).  In XSLT 1.0, the only thing you can do with an RTF is
> copy it to the result tree, or use it like a string.  In your case, you
> are trying to treat it like a node-set, which is explicitly forbidden:
>
> http://www.w3.org/TR/xslt#section-Result-Tree-Fragments
>
> There are two ways around this.  You can avoid use constructing new things
> by using the select attribute of xsl:variable.  This also has the
> advantage of being more efficient, since you are not creating new nodes.
> The other is you can use the EXSLT node-set function to "convert" the RTF
> to a node-set:
>
> http://exslt.org/exsl/functions/node-set/index.html
>
> both the Java and C++ processors support this function.
>
> You can consult the XSL FAQ for more information:
>
> http://www.dpawson.co.uk/xsl/index.html
>
> Also, the Mulberry XSL list is by far the best source of information and
> insight into the sorts of issues:
>
> http://www.mulberrytech.com/xsl/xsl-list/index.html
>
> Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xalan-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: Evaluation of xsl:attribute unexpectedly fails

Posted by da...@us.ibm.com.
> The main reason why I want to do 'loop rolling' here is that I plan to 
> have a couple of different templates that would make use of this 
> construct with all the same global variable. If I want to make some 
> (consistent) changes in the parameter values of the template calls, I 
> only need to make the required changes with the global variable, instead 

> of querying hundreds of lines of code.
> 
> My questions here are
> - is 'loop rolling' a legal concept with XSLT?
> - is the reaction of Xalan (The expression does not evaluate to a 
> node-set.) a bug - or a feature?

Whenever you use xsl:variable with content, you are creating a result tree 
fragment (RTF).  In XSLT 1.0, the only thing you can do with an RTF is 
copy it to the result tree, or use it like a string.  In your case, you 
are trying to treat it like a node-set, which is explicitly forbidden:

http://www.w3.org/TR/xslt#section-Result-Tree-Fragments

There are two ways around this.  You can avoid use constructing new things 
by using the select attribute of xsl:variable.  This also has the 
advantage of being more efficient, since you are not creating new nodes. 
The other is you can use the EXSLT node-set function to "convert" the RTF 
to a node-set:

http://exslt.org/exsl/functions/node-set/index.html

both the Java and C++ processors support this function.

You can consult the XSL FAQ for more information:

http://www.dpawson.co.uk/xsl/index.html

Also, the Mulberry XSL list is by far the best source of information and 
insight into the sorts of issues:

http://www.mulberrytech.com/xsl/xsl-list/index.html

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: Evaluation of xsl:attribute unexpectedly fails

Posted by Axel Weiß <aw...@informatik.hu-berlin.de>.
David Marston wrote:
> The question was about referring to bundles of parameters when calling
> a named template repeatedly.
>
> You could do what you want if you used the node-set() extension
> function. The problem comes when you attempt
> <xsl:for-each select="$globalSettings/elem">
> where $globalSettings is a Result Tree Fragment, which cannot be used
> in a path expression.
>
> But there are a couple other questions to ask yourself:
> 1. Is the template you're calling doing extensive manipulations on its
> parameters to generate its final output? If not, think about breaking
> apart the named template into several, skipping the named template
> completely, using attribute-sets if the template emits attributes,
> etc. 2. (If #1 above is not feasible) Is there an advantage if you
> pass one parameter to the named template and have the template then
> step through a collection of values? There could be possibilities for
> xsl:key or packing values into strings, in addition to your approach
> built on global variables.

Thanks David, for your instant response. You got me onto the right path.

I've solved this issue by an elegant solution: Put my global definitions 
(former globalSettings variable) into a separate xml file and read this 
one by the document(.) function. Now I have a clear separation of 
transform structure and transform symbol values, as well as nice and 
short stylesheets. Thanks.

			Axel

-- 
Humboldt-Universität zu Berlin
Institut für Informatik
Signalverarbeitung und Mustererkennung
Dipl.-Inf. Axel Weiß
Rudower Chaussee 25
12489 Berlin-Adlershof
+49-30-2093-3050
** www.freesp.de **

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: Evaluation of xsl:attribute unexpectedly fails

Posted by da...@us.ibm.com.
The question was about referring to bundles of parameters when calling a 
named template repeatedly.

You could do what you want if you used the node-set() extension function. 
The problem comes when you attempt
<xsl:for-each select="$globalSettings/elem">
where $globalSettings is a Result Tree Fragment, which cannot be used in a 
path expression.

But there are a couple other questions to ask yourself:
1. Is the template you're calling doing extensive manipulations on its 
parameters to generate its final output? If not, think about breaking 
apart the named template into several, skipping the named template 
completely, using attribute-sets if the template emits attributes, etc.
2. (If #1 above is not feasible) Is there an advantage if you pass one 
parameter to the named template and have the template then step through a 
collection of values? There could be possibilities for xsl:key or packing 
values into strings, in addition to your approach built on global 
variables.
.................David Marston

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org