I have the following XML Schema
<xml>
<series>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<a>
<b>
<c><![CDATA[ Foo ]]></c>
</b>
</a>
</series>
<series>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<a>
<b>
<c><![CDATA[ Bar ]]></c>
</b>
</a>
</series>
</xml>
I am trying to get the series that has a c value of foo. If c wasn’t wrapped in CDATA I could find it using the following xpath /xml/series/a/b[c="Foo"]/../.. but that does not work because of the CDATA – how can I find the series that has a c value of foo?
Thanks.
CDATA sections don’t appear in the data model operated upon by XPath. You’re right that your XPath isn’t finding the node you want, but it’s not the CDATA section that’s making things go wrong. The predicate
[c="Foo"]is looking for acelement whose string value is “Foo“, but there are no such nodes here: yourcelements have string values of “Foo” and “Bar” (that is, “Foo” and “Bar” each preceded and followed by a blank space).Try
/xml/series/a/b[contains(c,"Foo")]/../..or/xml/series/a/b[normalize-space(c) = "Foo"]/../..and I think you’ll be getting what you expect.