problem with reading in a variable from xml-file

Discussion in 'XML' started by Dennis Westermann, Jul 8, 2003.

  1. I have the following problem

    XML-File:
    <SEARCH>
    <TAG>'x' or 'y'</TAG>
    </SEARCH>

    <CHARLIST>
    <CHAR>a</CHAR>
    <CHAR>x</CHAR>
    <CHAR>f</CHAR>
    <CHAR>z</CHAR>
    <CHAR>r</CHAR>
    <CHAR>y</CHAR>
    <CHAR>u</CHAR>
    </CHARLIST>

    Stylesheet:
    this works:
    <xsl:variable name="var" select="'x' or 'y'"/>
    <xsl:for-each select="//CHARLIST[CHAR=$var]">
    <xsl:value-of select="$var"/>
    </xsl:for-each>
    => output= x y

    this doesn't work -> why? what can I do that this works too?:
    <xsl:variable name="var" select="//SEARCH/TAG"/>
    <xsl:for-each select="//CHARLIST[CHAR=$var]">
    <xsl:value-of select="$var"/>
    </xsl:for-each>
    => no output
     
    Dennis Westermann, Jul 8, 2003
    #1
    1. Advertising

  2. Dennis Westermann

    Marrow Guest

    Hi Dennis,

    Your first variable...

    <xsl:variable name="var" select="'x' or 'y'"/>

    The variable $var now contains a boolean value of true - because a string
    (e.g. 'x') if evaluated as a boolean always evaluates to true. So the OR of
    two true's is going to be true. And hence the use of that variable in a
    predicate like...
    //CHARLIST[CHAR=$var]
    is synonymous with...
    //CHARLIST[CHAR]
    i.e. select all CHARLIST elements that have a child of CHAR

    In the second variable...

    <xsl:variable name="var" select="//SEARCH/TAG"/>

    The variable $var now contains a node - and that node has a value of "'x' or
    'y'". So now when you do...

    <xsl:for-each select="//CHARLIST[CHAR=$var]">

    you are looking for a CHARLIST element which has a child CHAR element that
    has a value of "'x' or 'y'".

    It seems you are trying to macro evaluate some string that contains "'x' or
    'y'" - sorry, this cannot be done in XSLT/XPath.

    But you could try something like...

    == XML ====================================
    <?xml version="1.0"?>
    <root>
    <SEARCH>
    <TAG>|x|y|</TAG>
    </SEARCH>
    <CHARLIST>
    <CHAR>a</CHAR>
    <CHAR>x</CHAR>
    <CHAR>f</CHAR>
    <CHAR>z</CHAR>
    <CHAR>r</CHAR>
    <CHAR>y</CHAR>
    <CHAR>u</CHAR>
    </CHARLIST>
    </root>
    == end of XML =============================

    NB. where the '|' character used is just a delimiter between the possible
    values list.

    == XSL ====================================
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:variable name="var" select="root/SEARCH/TAG"/>
    <output>
    <xsl:for-each
    select="root/CHARLIST/CHAR[contains($var,concat('|',.,'|'))]">
    <xsl:copy-of select="."/>
    </xsl:for-each>
    </output>
    </xsl:template>
    </xsl:stylesheet>
    == end of XSL =============================

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



    "Dennis Westermann" <> wrote in message
    news:beeoku$nvk$...
    > I have the following problem
    >
    > XML-File:
    > <SEARCH>
    > <TAG>'x' or 'y'</TAG>
    > </SEARCH>
    >
    > <CHARLIST>
    > <CHAR>a</CHAR>
    > <CHAR>x</CHAR>
    > <CHAR>f</CHAR>
    > <CHAR>z</CHAR>
    > <CHAR>r</CHAR>
    > <CHAR>y</CHAR>
    > <CHAR>u</CHAR>
    > </CHARLIST>
    >
    > Stylesheet:
    > this works:
    > <xsl:variable name="var" select="'x' or 'y'"/>
    > <xsl:for-each select="//CHARLIST[CHAR=$var]">
    > <xsl:value-of select="$var"/>
    > </xsl:for-each>
    > => output= x y
    >
    > this doesn't work -> why? what can I do that this works too?:
    > <xsl:variable name="var" select="//SEARCH/TAG"/>
    > <xsl:for-each select="//CHARLIST[CHAR=$var]">
    > <xsl:value-of select="$var"/>
    > </xsl:for-each>
    > => no output
    >
    >
     
    Marrow, Jul 8, 2003
    #2
    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. jkflens
    Replies:
    2
    Views:
    1,514
    jkflens
    May 30, 2006
  2. Replies:
    0
    Views:
    798
  3. mfglinux
    Replies:
    11
    Views:
    725
    Roberto Bonvallet
    Sep 12, 2007
  4. Erik Wasser
    Replies:
    5
    Views:
    483
    Peter J. Holzer
    Mar 5, 2006
  5. Replies:
    5
    Views:
    94
    Chris Angelico
    May 14, 2014
Loading...

Share This Page