Working with CDATA in XML and XSLT

Ray's Avatar

Ray

08 Apr, 2016 04:26 PM

I am having problems in XSLT trying to get a value from a XML file that has CDATA tags. I am not sure what I am doing wrong.
This is my XML:

<description><![CDATA[
<div>
<b>Country:</b>
<a href="xxx.php">USA</a>
<b>City:</b>
<a href="xxx.php">Chicago</a>
</div>
]]></description>

In XSLT, I have the following:

        <p>Country: <xsl:value-of select="description/div/b['Country:']/following-sibling::a" disable-output-escaping="yes"/></p>

The result is:
<p>Country: </p>

I would like to get the result
<p>Country: USA <p>

Any help would be appreciated.
Thanks

  1. 1 Posted by Ryan Griffith on 08 Apr, 2016 06:03 PM

    Ryan Griffith's Avatar

    Hi Ray,

    I am wondering if perhaps the XPath just needs some tweaking. When you have a moment, please attach your Format and sample XML so I can do some local testing.

    Thanks!

  2. 2 Posted by Ray on 08 Apr, 2016 06:22 PM

    Ray's Avatar

    @Ryan Griffith

    I have included the sample results, XML and XSLT all in one file.

    Thanks for your help

  3. 3 Posted by Ryan Griffith on 08 Apr, 2016 08:13 PM

    Ryan Griffith's Avatar

    Hi Ray,

    Thank you for attaching the sample file.

    Unfortunately, the main issue here is the XML is within CDATA which isn't really meant to be parsed. During testing, I was able to get the HTML within the CDATA, but was unsuccessful in parsing it.

    Thinking about this a bit, what you could possibly do is output the XML that is within the CDATA in a temporary <div> with a specific class name and then attach an XSLT Format at the page, template or configuration set level that processes the XML within those hidden <div> tags and outputs the desired HTML.

    So basically you are processing the content twice, once to generate XML elements and again to actually process it. I've attached two Formats that appear to do the trick given your sample XML. A variation of the first_format.xml would be applied to your page region and the second_format would be applied at the page, template or configuration set level.

    Please let me know if you have any questions.

    Thanks!

  4. Tim closed this discussion on 27 Apr, 2016 08:47 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM