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

Discussion in 'XML' started by David Blickstein, Aug 24, 2005.

  1. 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.
    David Blickstein, Aug 24, 2005
    #1
    1. Advertising

  2. 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.

    "David Blickstein" <> wrote in message
    news:430cb15e$...
    >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.
    >
    >
    >
    >
    Dimitre Novatchev, Aug 24, 2005
    #2
    1. Advertising

  3. "Dimitre Novatchev" <> wrote in message
    news:430ccff3$0$17939$...
    > 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

    >
    > Cheers,
    > Dimitre Novatchev.
    >
    > "David Blickstein" <> wrote in message
    > news:430cb15e$...
    > >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.
    > >
    > >
    > >
    > >

    >
    >
    David Blickstein, Aug 24, 2005
    #3
  4. 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.

    "David Blickstein" <> wrote in message
    news:430cd4d3$...
    >
    > "Dimitre Novatchev" <> wrote in message
    > news:430ccff3$0$17939$...
    >> 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
    >
    >>
    >> Cheers,
    >> Dimitre Novatchev.
    >>
    >> "David Blickstein" <> wrote in message
    >> news:430cb15e$...
    >> >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.
    >> >
    >> >
    >> >
    >> >

    >>
    >>

    >
    >
    Dimitre Novatchev, Aug 25, 2005
    #4
  5. David Blickstein

    pantagruel Guest

    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>
    pantagruel, Aug 26, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    0
    Views:
    562
    KathyB
    Jul 25, 2003
  2. Kathy Burke
    Replies:
    5
    Views:
    360
    Kathy Burke
    Aug 26, 2003
  3. Xeon

    Intersecting Nodeset

    Xeon, Jul 21, 2003, in forum: XML
    Replies:
    1
    Views:
    399
    Dimitre Novatchev
    Jul 21, 2003
  4. Bryan Galvin
    Replies:
    2
    Views:
    417
    Bryan Galvin
    Sep 29, 2004
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page