Conditionally assigning nodesets to a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans-Michael Rupp

    #1

    Conditionally assigning nodesets to a variable

    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

    #2
    Re: Conditionally assigning nodesets to a variable


    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:n ode-set($foo/bar/x)">

    and declare a namespace dfor the extension

    Colin


    "Hans-Michael Rupp" <hans.rupp@dane t.de> wrote in message
    news:3F0A7CBC.3 050208@danet.de ...[color=blue]
    > 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[/color]
    exist.[color=blue]
    >
    > I would be gratefull for any suggestions.
    >
    > Greetings,
    >
    > Hans
    >[/color]


    Comment

    • Marrow

      #3
      Re: Conditionally assigning nodesets to a variable

      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" <hans.rupp@dane t.de> wrote in message
      news:3F0A7CBC.3 050208@danet.de ...[color=blue]
      > 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[/color]
      exist.[color=blue]
      >
      > I would be gratefull for any suggestions.
      >
      > Greetings,
      >
      > Hans
      >[/color]


      Comment

      Working...