problem with reading in a variable from xml-file

  • Thread starter Dennis Westermann
  • Start date
D

Dennis Westermann

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
 
M

Marrow

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
 

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,014
Latest member
BiancaFix3

Latest Threads

Top