0
votes

I have some XML that looks like this:

<contacts>
    <person>
        <name>Bob</name>
        <phoneNumber>1234</phoneNumber>
        <mobileNumber>5678</mobileNumber>
        <address>Address 1</address>
    </person>
    <person>
        <name>Sue</name>
        <phoneNumber>4321</phoneNumber>
        <address>Address 2</address>
    </person>
</contacts>

And some Xpath that queries for all the child nodes of <person>:

//name | //phoneNumber | //mobileNumber | //address

The result is this:

Bob
1234
5678
Address 1
Sue
4321
Address 2

I would like the Xpath query to return something (a string?) so that I can identify which <person> is missing a child node (in this case the second person has no <mobileNumber>.

The output I would like would look something like this:

Bob
1234
5678
Address 1
Sue
4321
null
Address 2

Thanks and let me know if you need more details!

UPDATE

The expression must be valid for Xpath 1.0 as I am using javascript's document.evaluate.

UPDATE

I have got some of the way with this expression:

concat(substring(/contacts/person/mobileNumber, 1 div boolean(/contacts/person/mobileNumber)),substring('null', 1 div not(/contacts/person/mobileNumber)))

However it now only evaluates the first <person> and returns nothing for the second. Any suggestions?

1
With XPath 1.0, you can use the hack described in this answer. - nwellnhof
//person | //person/* - splash58
@nwellnhof Thanks, that pointed me some of the way. See update above :) - sammy88888888

1 Answers

0
votes

It is not possible for xpath queries to create nodes; xslt is used to create/transform xml. I suggest, that you use xpath to select all person nodes, then use javascript to iterate the child nodes of person. Within the iteration loop you can get the text of each node and at the same time test for the missing mobileNumber node is react accordingly.