Michael Kaesy
Michael Kaesy

Reputation: 127

XQuery - How to use eq (=) on a group of values

I would like to know how you could query for one specific language, this is a related XML-snippet:

(...)
<Guide guideID = '5'  gname = 'Dexter Schneider'>
<Lang lname = "{'Spanish' , 'German' , 'English'}"/>
</Guide>
(...)

I tried with:

element Result {
//Guide[Lang/@lname = 'German']
}

but I only get the results where "German" is the only language in "lname". I suspect this is either because of a bad XML-document (no warnings from XQuisitor about the syntax), or because the "="-sign can only compare one exact string with another. Could anyone shed some light on this, and show me how a proper query would look like? Thanks!

Upvotes: 2

Views: 951

Answers (2)

AakashM
AakashM

Reputation: 63348

Depending on your XQuery engine, this XPath should work:

element Result {
//Guide[contains(Lang/@lname, 'German')]
}

Upvotes: 2

Vincent Biragnet
Vincent Biragnet

Reputation: 2998

In your specific case, you can use //Guide[contains(Lang/@lname, 'German')].

More generaly, the value of your attribute is a string and not an array. For your use case, you can use sequences but you first have to build the sequence.

For example, if you have an XML like that :

(...)
<Guide guideID = '5'  gname = 'Dexter Schneider'>
<Lang lname = "Spanish German English"/>
</Guide>
(...)

You can use the following XPath 2.0 syntax (xquery compliant) :

//Guide['German' = tokenize(Lang/@lname,' ') ]

The tokenize function creates a sequence and the = tests what you want.

Upvotes: 4

Related Questions