Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

xslt for-each = $variable

Posted on 2011-09-23
5
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

647 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