[xquery-talk] XQuery question about accessing ancestor/parent nodes of return values

Steve Taylor sstaylo at comcast.net
Fri May 9 17:36:00 PDT 2008


Hello all,

 

I'm new to  XQuery and this XQuery mailing list and  I'm hoping someone can
help me answer a question that has been stumping me.

 

I know how to use XQuery to find nodes that match a certain pattern, but I
need to find a way to pass back the parent nodes of the nodes matching the
pattern.

 

For example , here is a small subset from my test XML document (using
completely made up names and addresses) and following that is my Xquery. I
highlighted the matching node tag for my XQuery for easy identification:

 

 

<?xml version="1.0"?>

<Company>

 

<Person>

<Name>

    <LastName>Smith</LastName>

    <FirstName>Bob</FirstName>

    <MiddleInitial></MiddleInitial>

</Name>

<Address>

    <StreetAddress1>123 Elm St</StreetAddress1>

    <StreetAddress2></StreetAddress2>

    <City>Loganville</City>

    <State>LA</State>

    <Zipcode>12345</Zipcode>

</Address>

</Person>

 

<Person>

<Name>

    <LastName>Jones</LastName>

    <FirstName>Mike</FirstName>

    <MiddleInitial>B</MiddleInitial>

</Name>

<Address>

    <StreetAddress1>456 Maple St</StreetAddress1>

    <StreetAddress2></StreetAddress2>

    <City>Baston</City>

    <State>LA</State>

    <Zipcode>12378</Zipcode>

</Address>

</Person>

 

</Company>

 

 

 

I am trying to search the entire XML document using XQuery to return all
<Person> nodes with a <Zipcode> value of '12345'.  Here is my XQuery command
I created:

 

for $address in //Company/Person/Address

where $address/Zipcode = '12345'

return $address

 

This returns to me the following results:

 

<Address>

    <StreetAddress1>123 Elm St</StreetAddress1>

    <StreetAddress2/>

    <City>Loganville</City>

    <State>LA</State>

    <Zipcode>12345</Zipcode>

</Address>

 

 

The problem with the results above is that I need to return the entire
<Person> (including all sub-nodes) with the matching <Zipcode> string, not
just the person's <Address> node since the <Address> node alone does not
tell me who is the matching person. With the results I got back from the
XQuery above I only get the <Address> node, and I can't seem to figure out
how to return the entire <Person> node. Here is the expected results I was
hoping to receive for this example:

 

<Person>

<Name>

    <LastName>Smith</LastName>

    <FirstName>Bob</FirstName>

    <MiddleInitial></MiddleInitial>

</Name>

<Address>

    <StreetAddress1>123 Elm St</StreetAddress1>

    <StreetAddress2></StreetAddress2>

    <City>Loganville</City>

    <State>LA</State>

    <Zipcode>12345</Zipcode>

</Address>

</Person>

 

Does anybody know if it is possible to get the results I am looking for? If
so, please let me know the proper syntax to use for my XQuery command.  I
have looked everywhere for a solution and I can't seem to find anything.

 

Thank you very much in advance,

Steve Taylor

sstaylo at comcast.net

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://x-query.com/pipermail/talk/attachments/20080509/4c02332a/attachment.htm


More information about the talk mailing list