Navigating to nodes using xpath in flat structure

Posted by James Berry on Stack Overflow See other posts from Stack Overflow or by James Berry
Published on 2009-03-05T11:16:27Z Indexed on 2010/05/08 21:38 UTC
Read the original article Hit count: 356

Filed under:
|
|

I have an xml file in a flat structure. We do not control the format of this xml file, just have to deal with it. I've renamed the fields because they are highly domain specific and don't really make any difference to the problem.

<attribute name="Title">Book A</attribute>
<attribute name="Code">1</attribute>
<attribute name="Author">
   <value>James Berry</value>
   <value>John Smith</value>
</attribute>
<attribute name="Title">Book B</attribute>
<attribute name="Code">2</attribute>
<attribute name="Title">Book C</attribute>
<attribute name="Code">3</attribute>
<attribute name="Author">
    <value>James Berry</value>
</attribute>

Key things to note: the file is not particularly hierarchical. Books are delimited by an occurance of an attribute element with name='Title'. But the name='Author' attribute node is optional.

Is there a simple xpath statement I can use to find the authors of book 'n'? It is easy to identify the title of book 'n', but the authors value is optional. And you can't just take the following author because in the case of book 2, this would give the author for book 3.

I have written a state machine to parse this as a series of elements, but I can't help thinking there would have been a way to directly get the results that I want.

© Stack Overflow or respective owner

Related posts about xpath

Related posts about Xml