Conditionally assigning nodesets to a variable

Discussion in 'XML' started by Hans-Michael Rupp, Jul 8, 2003.

  1. Hello!

    I need to a assign a node(-set) conditionally to a varialbe like:

    <xsl:variable name="foo">
    <xsl:if test="$bar=''">
    <!-- how can I access a node here ?-->
    </xsl:if>
    <xsl:if test="not($bar='')">
    <!-- how can I access a node here ?-->
    </xsl:if>
    </xsl:variable>

    but how can I do it? I would need a <xsl:select> tag, which does not exist.

    I would be gratefull for any suggestions.

    Greetings,

    Hans
    Hans-Michael Rupp, Jul 8, 2003
    #1
    1. Advertising

  2. you use any xslt that returns a value for example
    <xsl:varibale name-"foo">
    <xsl:if test="$bar=''">
    <xsl:value-of select="/foo/bar">
    </xsl:if>

    or

    <xsl:if test="$bar=''">
    <xsl:apply-templates select="/foo/bar/x">
    </xsl:if>

    etc

    if you only need to use the result of the apply templates as a string/number
    etc then you access it using <xsl:value-of select="$foo"/>

    but if it returns a node-set (e.g. foo/bar/x) then you must use a processor
    specifc extension e.g.

    <xsl:for-each select="msxsl:node-set($foo/bar/x)">

    and declare a namespace dfor the extension

    Colin


    "Hans-Michael Rupp" <> wrote in message
    news:...
    > Hello!
    >
    > I need to a assign a node(-set) conditionally to a varialbe like:
    >
    > <xsl:variable name="foo">
    > <xsl:if test="$bar=''">
    > <!-- how can I access a node here ?-->
    > </xsl:if>
    > <xsl:if test="not($bar='')">
    > <!-- how can I access a node here ?-->
    > </xsl:if>
    > </xsl:variable>
    >
    > but how can I do it? I would need a <xsl:select> tag, which does not

    exist.
    >
    > I would be gratefull for any suggestions.
    >
    > Greetings,
    >
    > Hans
    >
    Colin Mackenzie, Jul 8, 2003
    #2
    1. Advertising

  3. Hans-Michael Rupp

    Marrow Guest

    Hi Hans,

    If you need the variable to contain references to the original nodes (rather
    than copies of the original nodes placed into a result tree fragment) then
    you can usually accomplish this using XPath predicates. For example, taking
    your original code and modifying it a little...

    <xsl:variable name="foo">
    <xsl:if test="$bar=''">
    <xsl:copy-of select="//a"/>
    </xsl:if>
    <xsl:if test="not($bar='')">
    <xsl:copy-of select="//b"/>
    </xsl:if>
    </xsl:variable>

    then using XPath predicates you could do...
    <xsl:variable name="foo" select="//a[$bar=''] | //b[not($bar='')]"/>

    Hope this helps
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator

    "Hans-Michael Rupp" <> wrote in message
    news:...
    > Hello!
    >
    > I need to a assign a node(-set) conditionally to a varialbe like:
    >
    > <xsl:variable name="foo">
    > <xsl:if test="$bar=''">
    > <!-- how can I access a node here ?-->
    > </xsl:if>
    > <xsl:if test="not($bar='')">
    > <!-- how can I access a node here ?-->
    > </xsl:if>
    > </xsl:variable>
    >
    > but how can I do it? I would need a <xsl:select> tag, which does not

    exist.
    >
    > I would be gratefull for any suggestions.
    >
    > Greetings,
    >
    > Hans
    >
    Marrow, Jul 8, 2003
    #3
    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. Ken Cox [Microsoft MVP]

    Re: Assigning contents within HTML tag to string variable.

    Ken Cox [Microsoft MVP], Aug 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    895
    tonymjohn
    Aug 7, 2004
  2. RA
    Replies:
    4
    Views:
    4,639
  3. will

    xsl subsets of nodesets

    will, Jul 7, 2003, in forum: XML
    Replies:
    3
    Views:
    857
    Marrow
    Jul 8, 2003
  4. johkar
    Replies:
    2
    Views:
    610
    Peter Flynn
    Apr 12, 2009
  5. weston
    Replies:
    1
    Views:
    251
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page