How do I share code that selects a nodeset in XSLT?

D

David Blickstein

I have an application where I have a shared "method" of selecting a nodeset.
The method currently is duplicated everywhere it is needed but I'd like that
code to be shared. The method also includes a conditional test (FWIW, the
test is whether a stylesheet parameter has a certain string value.)

<xsl:choose
<xsl:when test='$Generate='Y'">
select node set one way
<xsl:eek:therwise>
select node set another way

It's important to note that while the "method" of selection is common in
each occurence, I can't use a variable to store the nodeset once and reuse
for a number of reasons including the fact that there is no common scope
between the uses.


Is there a way to do this in XSLT V1? If there's a way to do it in V2, I'm
interested in hearing about it but I won't be able to use it.
 
D

Dimitre Novatchev

In XSLT 1.0 one either selects an expression into a global xsl:variable, or
passess it with an xsl:param to an applied/called template.

In XSLT 2.0 one will use xsl:function that returns a reference
(xsl:sequence) to the node-set.

Cheers,
Dimitre Novatchev.
 
D

David Blickstein

Dimitre Novatchev said:
In XSLT 1.0 one either selects an expression into a global xsl:variable, or
passess it with an xsl:param to an applied/called template.

I don't think that works for my situation. Can you assign a global variable
in any context? I thought you could only assign it in the direct context of
the xsl:stylesheet element. And if so, that means it can only have one
value that applies everywhere. As I mentioned, the process is shared but
it is used in different contexts and uses different results.
In XSLT 2.0 one will use xsl:function that returns a reference
(xsl:sequence) to the node-set.

Oh well... nice to know they've solved this problem in the next version.
But I can't depend on my clients having 2.0... yet.

db
 
D

Dimitre Novatchev

XSLT is a functional programming language. It requires a different mindset
from the imperative programming one that crippled so many programmers.

Any problem, that can be solved with "assignment" can be solved in a
functional way.

In case you specify (the smallest possible) problem it is most likely that
many people will offer solutions.

Cheers,
Dimitre Novatchev.
 
P

pantagruel

Is this what you're asking about? The description is sort of vague.

<xsl:param name="Generate" select="/doc/@Generate"/>
<xsl:param name="mynodeset">
<nodes>
<hi/>
</nodes>
</xsl:param>

<xsl:template match="someElement">
<xsl:call-template name="selector"/>
</xsl:template>
<xsl:template name="selector">
<xsl:choose
<xsl:when test='$Generate='Y'">
<xsl:apply-templates
select="exsl:node-set($mynodeset)/nodes/hi" mode="modeY"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-templates mode="modeNotY"
select="exsl:node-set($mynodeset)/nodes/hi" />
</xsl:eek:therwise>
</xsl:choose>


</xsl:template>

<xsl:template match="hi" mode="modeY">
Y said hi!
</xsl:template>
<xsl:template match="hi" mode="modeNotY">
NotY said hi!
</xsl:template>
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top