I have an XML that does not have a consistent structure. I need to see if there is an easy way to search for all parent nodes that have at least one child node with an attribute modified=”yes”. Is there an easy way to do this with XPath? The XML I will be dealing with is pretty huge and I will be working with a 4GL language that does not have all the functionality of Visual Studio, but it does provide some COM objects to help navigate XML DOM. The code looks very similar to what I would do in Visual Studio (see below the sample xml)
Sample XML (simplified greatly for easy readability):
<RootNode>
<BOOK>
<PRICE modified="yes">15</PRICE>
<COPIES>10</COPIES>
</BOOK>
<MAGAZINE>
<PRICE>8</PRICE>
<COPIES modified="yes">100</COPIES>
</MAGAZINE>
<NEWSPAPER modified="yes">
<PRICE>2</PRICE>
<COPIES>75</COPIES>
</NEWSPAPER>
<KINDLE_SUB>
<PRICE modified="yes">6</PRICE>
<COPIES/>
</KINDLE_SUB>
<EMAIL_SUB>
<UNSUBSCRIBE modified="yes">1</UNSUBSCRIBE>
</EMAIL_SUB>
</RootNode>
In Visual Studio you might do:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml( [above xml] );
XmlNodeList nodelist = xmldoc.SelectNodes(" *XPATH HERE* ");
should hopefully return to me:BOOK, MAGAZINE, KINDLE_SUB and EMAIL_SUB nodes in a XmlNodeList object.
The NEWSPAPER node should not be selected because it does not have child nodes that have a attribute of modified=”yes” even though the NEWSPAPER node itself has this attribute.
Probably something like this:
/*//*[*[@modified='yes']]