Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get attribute values if they are not equal to some values?

I have a xml like:

<movies>
    <movie name="aaa">
        ...
    </movie>
    <movie name="bbb">
        ...
    </movie>
    <movie name="ccc">
        ...
    </movie>
    <movie name="ddd">
        ...
    </movie>
    <movie name="eee">
        ...
    </movie>
</movies>

I would like to get the name of the movie if it is not 'aaa', or 'bbb', or 'ddd'. So I would like my output to be:

ccc
eee

Because I have several restrictions, I don't think it's suitable to use 'xsl:if'..I wrote my xslt like (I'm using xslt 1.0):

<xsl:value-of select="movies/movie/@name[not(self::aaa) and not(self::bbb) and not(self:ddd)]"/>

But the compiler complained there is a syntax error in this sentence..and I can't figure out why..

So could anybody help me with this? Many thanks!!

like image 238
D.Q. Avatar asked Mar 25 '23 09:03

D.Q.


2 Answers

Try this XPath expression:

movies/movie[@name!='aaa' and @name!='bbb' and @name!='ddd']/@name

or this one:

movies/movie/@name[.!='aaa' and .!='bbb' and .!='ddd']

the latter is shorter, but the former can be easily extended to check for other return not only the name, but the whole <movie> element - just remove /@name at the end. Or if you want that for the second, just add /.. at the end.

like image 151
Alex Shesterov Avatar answered Apr 01 '23 00:04

Alex Shesterov


Consider using an expression like this, in case there are too-many values for comparison:

/*/movie[not(contains('|aaa|bbb|ddd|', concat('|', @name, '|)))]/@name

This selects any name attribute of any movie element that is a child of the top element of the XSLT document, and whose value (of the name attribute) isn't one of the strings in the pipe-separated list of string values "|aaa|bbb|ddd|".

like image 26
Dimitre Novatchev Avatar answered Apr 01 '23 00:04

Dimitre Novatchev