tags:

views:

123

answers:

2

I have some XML I'm having trouble processing with XPath. I can't seem to get the values I'm looking for. The XML is structured like this:

<Group>
   <Menu>
      <Name>Top Menu</Name>
      <Document>
         <DocName>Readme.txt</DocName>
      </Document>
      <Menu>
         <Name>Sub Menu</Name>
         <Document>
            <DocName>Manual.pdf</DocName>
         </Document>
         <Document>
            <DocName>UserGuide.pdf</DocName>
         </Document>
      </Menu>
   </Menu>
</Group>

Given the menu name, I want to get back a list of DocumentNodes in the menu. For example, given "Sub Menu", I'd like to get back the two Document nodes for Manual.pdf and UserGuide.pdf.

Currently, I'm pulling this information using code that loops through the children, but, I'd rather just pull it up directly using XPath, but my skills with it are weak.

(And before you ask, I cannot restructure the XML. It's provided to me this way.)

Any thoughts?

+9  A: 

Here is an XPath that will do what you want:

//Menu[Name='Sub Menu']/Document

Andrew Hare
A: 

/<Menu>[\w\n]*<Name>Sub Menu</Name>[\w\n]*<Document>[\w\n]*(<DocName>([a-ZA-Z0-9\.]+)</DocName>[\w\n]*)*</Menu>/

Would more than likely work (with a little tweaking, this is a bit rough!), but, I don't think Regex is the way to go. I'd personally use XPath or DOM

Mez