How to parse an XML DOM inside a CDATA element in XSLT?

r3st0r3 picture r3st0r3 · Apr 25, 2012 · Viewed 8.7k times · Source

say I have an XML file like:

<library>
 <books>
  <![CDATA[<genre><name>Sci-fi</name><count>2</count></genre>]]>
  <book>
   <name>
    Some Book
   </name>
   <author>
    Some author
   </author>
  <book>
  <book>
   <name>
    Another Book
   </name>
   <author>
    Another author
   </author>
  <book>
 <books>
</library>

I want to read the CDATA element 'name' in an xslt transformer and place its value somewhere in the vaue of a tag. How do I do this? AFAIK, we cannot use xpath on the contents of CDATA. Is there some hack/workaround for this? I want to do this strictly in an XSLT.

Answer

Michael Kay picture Michael Kay · Apr 26, 2012

Some XSLT products have an extension function, for example saxon:parse() that allow you to take a string containing lexical XML and convert it into a tree of nodes.