Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2010, 06:36 PM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default > within CDATA tags

Hello,
I am new to xslt and I am trying to figure out how to convert the characters > within CDATA tags to show up as ">" using xslt2.0. In other words, I am getting an xml payload where a string element is enclosed with the CDATA tag. The string contains > in place of the greater than sign. I need to convert it back to the greater than sign before displaying to the user. I'm not sure if this is an issue that can be resolved with xslt or the user display character settings or the encoding settings.


Here's a simple example of the xml payload:

<?xml version="1.0" encoding="UTF-8"?>
<Payload>
<element>
<![CDATA[CRIMINAL MISCHIEF &gt; $50]]>
</element>
</Payload>

This payload is getting sent to me this way and I am not sure why the developers couldn't put the ">" sign within the CDATA tags (cause that's the whole point of the CDATA tags) but I have no control over that.

I have the following xslt to convert the "&gt;" to ">":


<xsl:template name="ConvertChar">
<xsl:param name="string" select="."/>
<xsl:variable name="greaterThan" select=">" />
<xsl:value-of select="translate($string, '&gt;', $greaterThan)" />
</xsl:template>


This is not valid because the select=">" will cause an unexpected token error.
Is it possible for me to use xslt2.0 to do this or is this something that I need to configure on the encoding or character setting?

Hope my question makes sense.
Any help is appreciated and Thanks in advance!
 
Old July 7th, 2010, 03:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're a bit confused, and you've succeeded in confusing me as well. If I've misunderstood the problem, please ignore.

The forum software doesn't help. I'm not seeing the same thing on the forum (in the browser) as I saw in the email notification. That could be because you edited the post after sending it (a very annoying thing when people do it!) or it could be because you didn't use the special forum markup for delimiting code sections. Or it could just be that the forum software is just trying to be too clever. Either way, I saw literal greater-than characters in the email version of your code, but XML ampersand-g-t-semicolon sequences in the forum version, and I don't know which is actually present in your code.

If the XML does contain ampersand-g-t-semicolon within a CDATA section, then that's bad, and when you get bad XML the best thing to do is find out where it came from and fix the process that generated it in the first place. If you can't achieve that, then repairing the XML is a poor second best. People who produce bad XML will carry on doing it unless people complain.

The CDATA section will appear to the XSLT stylesheet as an ordinary text node containing somewhere in the middle the four-character sequence ampersand-g-t-semicolon, which you want to replace with a single character, the greater-than-sign or right-angle-bracket. The translate() function only replaces single characters by single characters (or nothing). You want the XSLT 2.0 replace() function. If you're using XSLT 1.0, then either (a) switch to XSLT 2.0, or (b) download the source code of the EXSLT str:replace template (www.exslt.org) which does string replacement in XSLT 1.0.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 7th, 2010, 03:30 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Oh, and if you have to represent the four character sequence ampersand-g-t-semicolon in your stylesheet, for example in a call to replace(), then remember to write it as 8 characters: ampersand-a-m-p-semicolon-g-t-semicolon
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
ptn77 (July 7th, 2010)
 
Old July 7th, 2010, 10:25 AM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Sorry,
I failed to read the instructions on how to post code. I'll remember to put in the delimiter for the code sections next time.
Thank you for taking the time to respond to my post.
I tried to explain to the developers who sent me the xml that they should not be putting the ampersand-g-t-semicolon in the CDATA section and should put the greater than sign since that is the whole point of using CDATA. Nevertheless, I have no control over how they send me the xml, so I am responsible for repairing it.
I am using xslt2.0 so I will try your suggestion of using the replace() function.
Thanks,





Similar Threads
Thread Thread Starter Forum Replies Last Post
how can split/replace the <br> with <Para> tags. moshaik XML 2 March 8th, 2009 11:11 AM
<!DOCTYPE> Tags? kennethjaysone HTML Code Clinic 0 May 18th, 2007 01:39 AM
How to get specific value from <![CDATA[]]> ShaileshShinde VB.NET 2002/2003 Basics 0 September 12th, 2005 06:59 AM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
using variables within <td> tags handyman Javascript How-To 3 June 15th, 2004 11:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.