Basic XSLT/XSLTC question

J

Justine Hlista

I'm using xalan-j_2_6_0 and trying to get an example from Michael
Kay's book to work:

<xsl:template match="/">
<xsl:variable name="rainbow">
<color>red</color>
<color>blue</color>
<color>green</color>
</xsl:variable>

<xsl:for-each select="$rainbow/color">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>

Both XSLTC and XSLT generate errors when they encounter the XPath
expression in the select attribute. I cannot get XSLTC or XSLT to
accept any expression that qualifies a variable in a select statement.

Am I trying to do something that is strictly forbidden here? Am I
misunderstanding what Michael Kay's example was supposed to do?

Any help is greatly appreciated!!

Justine
 
D

Dimitre Novatchev [MVP XML]

I guess that you read the second edition of Mike's book, in which he covered
XSLT 1.1.

XSLT 1.1 was never developed by the W3C into a final status and is not a
standard. The current standard of XSLT 1.0 has the concept of RTF (Result
Tree Fragment).

An RTF cannot be treated like a node-set in XPath -- any such attempt
results in raising an error -- this is exactly what you described.

In order to process an RTF as a node-set, one must use an
implementation-defined extension function with the usual name of node-set(),
belonging to an implementation-dependent namespace, or use the
common:node-set() function as defined by EXSLT.

These functions take an RTF and convert it to a regular node-set. Then their
result is a regular tree (a node-set having just one root node) and can be
navigated using XPath.


Therefore, you also have to use whatever implementation-defined
xxx:node-set() extension function is available for your XSLT processor.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 
P

Philippe Poulard

Hi,

It is also possible to store "XML constants" inside your stylesheet and
refer to them with the document() function, that will be adressable with
XPath (no extension required) :

<xsl:stylesheet ...
xmlns:xsl="..."
xmlns:data="...">

<data:structures>
<rainbow>
<color>red</color>
<color>blue</color>
<color>green</color>
</rainbow>
</data:structures>

<xsl:template match="/">
<xsl:variable name="rainbow"
select="document('')/*/data:structures/rainbow"/>
<xsl:for-each select="$rainbow/color">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
I guess that you read the second edition of Mike's book, in which he covered
XSLT 1.1.

XSLT 1.1 was never developed by the W3C into a final status and is not a
standard. The current standard of XSLT 1.0 has the concept of RTF (Result
Tree Fragment).

An RTF cannot be treated like a node-set in XPath -- any such attempt
results in raising an error -- this is exactly what you described.

In order to process an RTF as a node-set, one must use an
implementation-defined extension function with the usual name of node-set(),
belonging to an implementation-dependent namespace, or use the
common:node-set() function as defined by EXSLT.

These functions take an RTF and convert it to a regular node-set. Then their
result is a regular tree (a node-set having just one root node) and can be
navigated using XPath.


Therefore, you also have to use whatever implementation-defined
xxx:node-set() extension function is available for your XSLT processor.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


I'm using xalan-j_2_6_0 and trying to get an example from Michael
Kay's book to work:

<xsl:template match="/">
<xsl:variable name="rainbow">
<color>red</color>
<color>blue</color>
<color>green</color>
</xsl:variable>

<xsl:for-each select="$rainbow/color">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>

Both XSLTC and XSLT generate errors when they encounter the XPath
expression in the select attribute. I cannot get XSLTC or XSLT to
accept any expression that qualifies a variable in a select statement.

Am I trying to do something that is strictly forbidden here? Am I
misunderstanding what Michael Kay's example was supposed to do?

Any help is greatly appreciated!!

Justine


--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 
D

Dimitre Novatchev [MVP XML]

Philippe Poulard said:
Hi,

It is also possible to store "XML constants" inside your stylesheet and
refer to them with the document() function, that will be adressable with
XPath (no extension required) :

Yes, and this is a really valuable technique for creating data structures to
be used by the XSLT application.

The example given by the OP can be solved completely that way.

What cannot be done in this way is to access dynamically created nodes.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top