Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use 'and' operator in xsl for-each?

Simply can I execute the following in xsl?

 <xsl:for-each select="trip/instance[.!=''] and trip/result[.!='']">
 </xsl:for-each>

Q: When I use select="" in for-each does it change the scope of my selector for the code I use inside for-each?

like image 816
Mazzi Avatar asked Feb 08 '10 01:02

Mazzi


2 Answers

You can use 'and' in for-each loop, but not in the way you have mentioned (being not sure what exactly you want to achieve)

I assume your requirements something like, either

1) You want to loop through Trip whose both child entities are (instance and result) not null, In this case you have to write like this ..

<xsl:for-each select="trip[instance!='' and result!='']>

if any one among instance and result are null, then your loop doesn't enter the element namely, trip.


2) You want to seek through each instanceandresult children inside parent trip whose values are not null. In this case you Don't need and ..

<xsl:for-each select="trip/instance[.!=''] | trip/result[.!='']">

This will work.

Now answer to your Q ..
with FOR-EACH loop you can set the scope of selector ..
for-example:In case (1), scope of selector was "root_name//trip" and in case (2) scope of selector was "root_name//trip/instance" also "root_name//trip/result" ..

I hope, I understood your question correctly and answered it as understandable ..

like image 93
InfantPro'Aravind' Avatar answered Nov 11 '22 22:11

InfantPro'Aravind'


No, you cannot use and in the select attribute.

You want to use the union operator: |, which behaves kind of like an and and kind of like an or, depending on how you think about it.

It will give you a distinct list of both sets of nodes and will return them in the document order that it finds them(not all instance and then all result elements).

 <xsl:for-each select="trip/instance[.!=''] | trip/result[.!='']">
 </xsl:for-each>

Inside the for-each the context will switch between each of the selected nodes during each iteration. You can access the current node with . or current().

like image 28
Mads Hansen Avatar answered Nov 11 '22 22:11

Mads Hansen