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 October 4th, 2004, 12:15 PM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Escaping < and >

Hi,

I've got an issue with an application where I pull some data froma database and it returns the following string:

A story with a &lt;a href="www.dirtyeye.com"&gt;link&lt;/a&gt; to my website. Wonder If it works.

What I want to be able to do is convert this string into valid XML/HTML with the link working correctly. Like this:

A story with a <a href="www.dirtyeye.com">link</a> to my website. Wonder If it works.

 Now I've read the section in the XSLT book about ourtput escaping, etc but I cannot seem to find a way to make this work correctly.

Any ideas?
Glen


 
Old October 5th, 2004, 03:18 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

Using XSLT <xsl:value-of select="tagname" disable-output-escaping="yes"> we can get the output like <a href="www.dirtyeye.com">link</a>.But <xsl:value-of> returns the string value.So hyperlink wont work.It will display the whole string not link alone.If you want to display only the link,store the value in one variable and do like this
<a>
   <xsl:attribute name="href"><xsl:value-of select="Get the value of href from variable using "/></xsl:attribute>
   <xsl:value-of select="Get hyperlink text from variable"/>
</a>
Link will work fine.

 
Old October 5th, 2004, 03:33 AM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Santhi
 Using XSLT <xsl:value-of select="tagname" disable-output-escaping="yes"> we can get the output like <a href="www.dirtyeye.com">link</a>.But <xsl:value-of> returns the string value.So hyperlink wont work.It will display the whole string not link alone.If you want to display only the link,store the value in one variable and do like this
<a>
<xsl:attribute name="href"><xsl:value-of select="Get the value of href from variable using "/></xsl:attribute>
<xsl:value-of select="Get hyperlink text from variable"/>
</a>
Link will work fine.
If I read this correctly the line <xsl:value-of select="tagname" disable-output-escaping="yes"> will get the entire string including the text outside the <a> tag and display it? Replacing the &lt; and &gt; with < and >? If so then this is what I want. However in the output when I try this it still leaves the &lt; and &gt; as they are and does not convert them in the output. Am I misunderstanding something here?

Glen

 
Old October 5th, 2004, 04:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, the database is giving you &lt; rather than < because it doesn't know that the < is intended to be markup, it thinks it is an ordinary character. If at all possible, you should correct the problem at source: some databases allow you to declare that a field holds XML, in which case the < should be recognized as markup and returned as such.

If you can't correct the problem at source, the next best thing is to parse the XML before you process it. Then your application will be able to process <a/> as an element node, rather than having it misrepresented as a sequence of (in this case five) characters.

Note that the &lt; will never remain as is. The normal process is that it is converted to < on input to your application, and is then converted back to &lt; by the serializer. Within your XSLT code, string-length() on this content will return 1, not 4.

You can suppress the conversion to &lt; in the serializer by using disable-output-escaping="yes", thus making it appear as a < markup character to the next process in the pipeline. However, this is a last resort. It only works if the XSLT transformation is serializing its output. It doesn't work if the result tree is passed directly to the next phase of processing as a tree, which happens for example when you run transformations in the Mozilla browser, or with the Microsoft API when you send the transformation result to a DOM.

Michael Kay
http://www.saxonica.com/
 
Old October 5th, 2004, 08:56 AM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that Michael. I managed to get my source application to convert the string using the java:parse() function. That seemed to have done the trick. It now outputs a clean nodeset which I can process using the template.

Regards,
Glen






Similar Threads
Thread Thread Starter Forum Replies Last Post
<?javax.xml.transform.disable-output-escaping ?> robbert XSLT 5 January 5th, 2011 07:28 PM
Ch 8: <asp:image> inside <a> & ext.CSS (pg. 274) epc BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 12th, 2008 04:37 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
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM
<STRONG> vs <B> and <EM> vs <I> anshul HTML Code Clinic 12 September 1st, 2004 05:22 PM





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