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

16,742

Solution 1

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 ..

Solution 2

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().

Share:
16,742

Related videos on Youtube

Mazzi
Author by

Mazzi

SPARTAAAAAAAA!!!

Updated on April 16, 2022

Comments

  • Mazzi
    Mazzi about 2 years

    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?

  • InfantPro'Aravind'
    InfantPro'Aravind' about 14 years
    +1 for explanation on UNION operator .. and for the point "DEPENDING ON HOW YOU THINK ABOUT IT"