user3147593
user3147593

Reputation: 1

Getting info from nodes in CDATA with Xpath?

I have a little problem with getting the correct info from my RSS-feed.

This is how the feed looks like.

<description>
<![CDATA[<img height="64" width="64" align="top" src="http://www.festivalfoton.nu/images/festivaler/skogsrojet.jpg" alt="" title="" width="222" height="222" />
<datum>1 - 2 augusti</datum>
<stad>Rejmyre</stad>
<pris>690 kronor</pris>]]>
</description>

I've tried to fetch the info inside the different nodes , and with this command:

[data:row("description/stad")]

But nothing happens and I've learned that it has to do with CDATA.

What can I do? Does anyone have a clue?

Upvotes: 0

Views: 770

Answers (1)

Jens Erat
Jens Erat

Reputation: 38662

CDATA encodes text nodes which you can query using description/text(). You cannot query the "nodes" inside, as they are none (but only large plain text node).

Possible solutions:

  • Fix the input to actually hand over XML data instead of text nodes
  • Parse the text node as XML again (but watch out, it's no valid XML as it lacks a common root node)
  • Use some proprietary extensions that might be available to parse the text nodes again, but most probably these will only be available in fully-fledged and more recent XQuery/XSLT engines (which include XPath as embedded language).
  • "Fix" the XML before parsing and remove the CDATA tag using regular expressions/string search. Really ugly solution and much more likely to break, but depends on what you need to do and how often (single time analysis or long-running server software?).

Upvotes: 1

Related Questions