XQuery

Hi:
I’m new to XQuery. I have a document like the following

John Doe
7031234567
8041234567
9051234567

Mary Ann
2021234567
9011234567
8051234567


I want to get the home phone, the query I used like the following :

for $a in input()/person
let $b :=$a/phone
where $b/@type=“home”
return $b

Obviously this doesn’t work as it gives back all phone no matter which type. Please help.

Hi,

The reason for the unexpected result is that by using a ?let? clause you bind all phones of a person to variable $b. In the ?where? clause you just check if there is a ?home? phone bound to $b. If this is the case you return all phones of a person by returning $b.
To get the only the ?home? phones you have to replace the ?let? by a ?for? clause in the following way:

for $a in input()/person
for $b in $a/phone
where $b/@type=“home”
return $b

A shorter variant of this query is the following:

for $b in input()/person/phone
where $b/@type=“home”
return $b

The shortest variant just consists of a single path expression:

input()/person/phone[@type=“home”]

Best regards,

Thorsten Fiebig