0

Given the following XML, I am wanting to build an XPath query to get me the text of the Body node which contains the HTML

<documents>
 <document>
  <items>
   <item name='Form'>
    Procedure
   </item>
   <item name='Body'>
    <![CDATA[<p>arbitrary html</p>]]>
   </item>
  </items>
 </document>
 <document>
  <items>
   <item name='Form'>
    Process
   </item>
   <item name='Body'>
    Some arbitrary value
   </item>
  </items>
 </document>
</documents>

I am able to get close, I am just missing something. (this may not the best way to get there, but its the only way I have been able to get close)

//document/items/item[@name='Form'][text()='Procedure']/../item[@name='Body']

Results in the CDATA wrapped content, I am lost as to how select the inner text.

//document/items/item[@name='Form'][text()='Procedure']/../item[@name='Body']/text()

Is yielding an empty string

2
  • More background, I have a XSL and using a value-of query to try and inject the results from the query into my page. It appears that there is a bigger issue here than simply my query and the structure of my XML. If I modify the XML and only have the single node I am looking for and do a query for //item[@name='Body']/text() its still an empty string Commented Jun 5, 2012 at 16:19
  • using disable-output-escaping enables this to work on IE, but not in Firefox, any idea on how this can work in Firefox as well as IE? Commented Jun 5, 2012 at 16:29

1 Answer 1

1

Use this XPATH to get your expected results:-

//document[items/item[@name='Form']/text()='Procedure']/items/item[@name='Body']/text()

Result:

<p>arbitrary html</p>

UPDATED:

I got the actual issue on your XML.

The value of form node contains spaces, which is causing issue.

To solve this issue, use this new XPATH with normalize-space()

//document[normalize-space(items/item[@name='Form']/text())='Procedure']/items/item[@name='Body']/text()

Result:

<p>arbitrary html</p>
Sign up to request clarification or add additional context in comments.

1 Comment

Well I like this query more, I am still getting an empty string when I do the /text().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.