Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add multiple filters to a XSLT for-each statement?

I have the following XSLT node:

<xsl:for-each select="Book[title != 'Moby Dick']">
....
</xsl:for-each>

However, I'd like use multiple filters in the for-each. I've tried the following, but it doesn't seem to work:

<!-- Attempt #1 -->
<xsl:for-each select="Book[title != 'Moby Dick'] or Book[author != 'Rowling'] ">
....
</xsl:for-each>


<!-- Attempt #2 -->
<xsl:for-each select="Book[title != 'Moby Dick' or author != 'Rowling']">
....
</xsl:for-each>
like image 649
Huuuze Avatar asked Jan 13 '11 23:01

Huuuze


1 Answers

However, I'd like use multiple filters in the for-each

Your real question is an XPath one: Is it possible, and how, to specify more than one condition inside a predicate?

Answer:

Yes, use the standard XPath boolean operators or and and and the standard XPath function not().

In this particular case, the XPath in the select attribute may be:

Book[title != 'Moby Dick' or author != 'Rowling']

I personally would always prefer to write an equivalent expression:

Book[not(title = 'Moby Dick') or not(author = 'Rowling')]

because the != operator has a non-intuitive behavior when one of its operands is a node-set.

But I am guessing that what you probably wanted was to and the two comparissons -- not to or them.

like image 183
Dimitre Novatchev Avatar answered Sep 27 '22 18:09

Dimitre Novatchev