Solved

xslt for-each = $variable

Posted on 2011-09-23
5
318 Views
Last Modified: 2012-06-27
Hi,

I would like to create a dynamic for each loop that only picks up the records that match a certain variable passed into the xsl.

So say I have a list of cars. I would like to do a for-each that picks up all cars that are red.

But set as a variable.

<xsl:variable name="colour">red</xsl:variable>

<xsl:for-each select="car[info/colour = $colour]">
XML
<cars>
 <car>
  <info>
   <colour>red</colour>
   <price>500</price>
   <make>ford</ford>
  </info>
  <history>
    <MOT>none</MOT>
  </history>
 </car>
 <car>
  <info>
   <colour>white</colour>
   <price>650</price>
  </info>
 </car>
</cars>

Doesn't seem to be working. I can get it working for numbers though, say do it for price and the variable is 500.

Thanks
0
Comment
Question by:help-is-needed
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 36587191
should work, but you might have some spaces

try
<xsl:for-each select="car[normalize-space(info/colour) = normalize-space($colour)]">

make sure your context for the for-each is cars

if you pass it in, it shoukld be a xsl:param instead of a xsl:variable
0
 

Author Comment

by:help-is-needed
ID: 36587309
ah, putting in the normalize-space worked.

What happens when I get onto cars with a colour that is two words? i.e. dark purple
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36587349
normalize-space takes away all white-space (newlines, tabs, spaces, ...) in the beginning and at the end of the argument
and replaces each sequence of white-space (one or more newlines, spaces, tabs, ...) inside the argument with one space

"     dark          
purple      

"

will become
"dark purple"
for the compare,
which likely fits your purpose
0
 

Author Comment

by:help-is-needed
ID: 36587449
excellent, thats got it.
Im assuming it is case sensitive, is there away of mitigating this along with the spacing too?

Thanks
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 36587507
it is case sensitive
XSLT1 has no lower-case
so you need
translate(normalize-space(.), 'AZERTYUIOPQSDFGHJKLMWXCVBN', 'azertyuiopqsdfghjklmwxcvbn')

translate checks each character from the first argument, checks if it is in the second argument and replaces it by the char at the same position in the third argument
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question