Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 14th, 2005, 11:16 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Retrieve the escaped node content

Hello all:

I want to retrieve the value of a node that has children, and escape it (transform the "<" to "&lt;", and ">" to "&gt;"), something like:

XML:

<root>
  <node>
     <nodechild>A</nodechild>
     <nodechild>B</nodechild>
     <nodechild>C</nodechild>
  </node>
</root>


Desired output:

&lt;root&gt;
  &lt;node&gt;
     &lt;nodechild&gt;A&lt;/nodechild&gt;
     &lt;nodechild&gt;B&lt;/nodechild&gt;
     &lt;nodechild&gt;C&lt;/nodechild&gt;
  &lt;/node&gt;
&lt;/root&gt;


I've tried to retrieve directly the root value with:


<xsl:value-of select="." />


... but the output doesn't contain the "<,>" characters, only blank spaces.

...any ideas?

Thanks in advance ;)

Carlos.

Reply With Quote
  #2 (permalink)  
Old September 14th, 2005, 01:27 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Actually, the value of the node does not have any angle brackets in it, so there are none to escape. You need to serialize the node, and then escape its serialization. With a saxon extension function you can do


<xsl:output method="xml"/>

<xsl:value-of select="saxon:serialize($node)"/>

I can't immediately think of any other solution, at least not within XSLT. It's more common for people to want to remove one level of escaping than to add one: which can be achieved using disable-output-escaping.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old September 14th, 2005, 01:29 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Of course the other way you can do this is to serialize "by hand", something like

<xsl:template match="*">
&lt;<xsl:value-of select="name()"/>&gt;
<xsl:apply-templates/>
&lt;/<xsl:value-of select="name()"/>&gt;
</xsl:template>

Not too difficult if there are no attributes and namespaces around.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old September 15th, 2005, 01:45 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael:

I already knew the second solution, but in my case, the attributes are neccessary :-S. I'll try with the node serialization.

Reply With Quote
  #5 (permalink)  
Old September 16th, 2005, 03:23 PM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Hi!

Following Mr. Kay's "hand serialization", here's a version that include attributes.

I leave it to you to indent it if you want.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
        <xsl:text>&amp;lt;</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:apply-templates select="@*"/>
        <xsl:text>&amp;gt;</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>&amp;lt;/</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>&amp;gt;</xsl:text>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:text> </xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>="</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Rushman

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve numbered node path from xml jeft0nes XSLT 6 July 20th, 2007 03:59 PM
How Retrieve data of a node or from an element chandu.mca007 XML 1 December 16th, 2006 03:11 PM
How to use XPath to retrieve a node tag rs:data? y10k XML 1 May 5th, 2006 01:11 PM
constructing node-set content to xsl:variable MrWay XSLT 3 February 28th, 2006 10:39 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM



All times are GMT -4. The time now is 10:09 PM.


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