Solved

xslt for-each = $variable

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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