tags:

views:

5023

answers:

5

Consider the following xml fragment:

<Obj>
   <Name><![CDATA[SomeText]]></Name>
</Obj>

How do I retrieve the "SomeText" value via XPath? I'm using Nauman Leghari's (excellent) Visual XPath tool.
/Obj/Name returns the element
/Obj/Name/text() returns blank

I don't think its a problem with the tool (I may be wrong) - I also read XPath can't extract CDATA (See last response in this thread) - which sounds kinda weird to me.

+3  A: 

i think the thread you referenced says that the CDATA markup itself is ignored by XPATH, not the text contained in the CDATA markup.

my guess is that its an issue with the tool, the source code is available for download, maybe you can debug it...

Jason
It basically had a giant DisplayNode() switch case on the type of XMLNode scanned... with the specific case block with a TODO Inside it :) Issue with the tool it is.
Gishu
+1  A: 

See if this helps - http://www.zrinity.com/xml/xpath/
XPATH = /Obj/Name/text()

shahkalpesh
A: 

CDATA sections are just part of what in XPath is known as a text node or in the XML Infoset as "chunks of character information items".

Obviously, your tool is wrong. Other tools, as the XPath Visualizer correctly highlight the text of the Name element when evaluating this XPath expression:

/*/Name/text()

One can also write a simple XSLT transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>
<xsl:template match="/">
  "<xsl:value-of select="/*/Name"/>"
</xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Obj>
    <Name><![CDATA[SomeText]]></Name>
</Obj>

the correct result is produced:

  "SomeText"
Dimitre Novatchev
+2  A: 
Gishu
> the behavior of the Value propertyYou are seeing the effect of different data models reflected by different APIs. Many APIs follow the XML Infoset which distinguishes text and CData content. But XPath does not..NET's XPathDocument API follows the XPath model so you cannot explicitly see CDATA,
Richard
A: 

A suggestion would be to have another field of the md5 hash of the cdata. You can then use xpath to query based off the md5 with no issue

<sites>
  <site>
    <name>Google</name>
    <url><![CDATA[http://www.google.com]]&gt;&lt;/url&gt;
    <urlMD5>ed646a3334ca891fd3467db131372140</urlMD5>
  </site>
</sites>

Then you can search:

/sites/site[urlMD5=ed646a3334ca891fd3467db131372140]
MANCHUCK