Solved

xslt for-each = $variable

Posted on 2011-09-23
5
314 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
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, …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now