RE: [xsl] Escaping nodeset content

Subject: RE: [xsl] Escaping nodeset content
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 9 Mar 2006 17:47:36 -0000
Call out to an extension function that serializes the node-set as lexical
XML, and pass the lexical XML back as a string.

Saxon has such a function built-in: see saxon:serialize().

You can write your own serializer in XSLT if you must, but it's not much
fun.

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: Kipp.Howard@xxxxxxxxxxxxxx [mailto:Kipp.Howard@xxxxxxxxxxxxxx] 
> Sent: 09 March 2006 17:41
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Escaping nodeset content
> 
> I have an application where I need to convert, with an xsl 
> transform, some nodeset to escaped text (i.e., <abc/> => 
> "&lt;abc/&gt;").  Attached you will find some tests cases 
> with a transform that kind-of does what I want, but it is not 
> escaping the result.  Is there anyway to do what I want 
> within an xsl transform?
> 
> I know this is a brain-dead way of processing XML, but the 
> guy that designed the message I'm trying to generate didn't 
> know much about including XML within another XML document 
> (using namespaces and such) so he just put it in as text :(.  
> We are planning on redesigning this at the end of this year 
> but for now, we have to work within this design.
> 
> Thanks for any help.
> 
> I tried to send these as attachments but the message was 
> rejected with:
> <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>:
> ezmlm-reject: fatal: Sorry, I don't accept messages of MIME 
> Content-Type
> 'multipart/mixed' (#5.2.3)
> 
> Here is the content of the files I tried to send:
> 
> commonTools.xsl:
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>    <xsl:output method="xml" encoding="utf-8"/>
> 
>    <xsl:template match="commonToolsData">
>       <commonToolsTest>
>          <xsl:apply-templates />
>       </commonToolsTest>
>    </xsl:template>
> 
>    <xsl:template match="nodeSetToConvert">
>       <textFromNodeSet>
>          <!-- Not sure how to copy the output as text -->
>          <xsl:copy-of select="*"/>
>       </textFromNodeSet>
>    </xsl:template>
> 
>    <!-- Identity -->
>    <!-- Whenever you match any node or any attribute -->
>    <xsl:template match="node()|@*">
>       <!-- Copy the current node -->
>       <xsl:copy>
>          <!-- Including any attributes it has and any child nodes -->
>          <xsl:apply-templates select="@*|node()"/>
>       </xsl:copy>
>    </xsl:template>
> 
> </xsl:stylesheet>
> 
> commonTools.xml:
> <?xml version="1.0" encoding="utf-8" ?> 
> <commonToolsData>
>    <convertedNodesetTest>
>       <convertionTest>
>          <nodeSetToConvert><result>&amp;</result></nodeSetToConvert>
>          
> <expectedText>&lt;result&gt;&amp;amp;&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert><result>&lt;</result></nodeSetToConvert>
>          
> <expectedText>&lt;result&gt;&amp;lt;&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert><result>&gt;</result></nodeSetToConvert>
>          
> <expectedText>&lt;result&gt;&amp;gt;&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert><result>&apos;</result></nodeSetToConvert>
>          
> <expectedText>&lt;result&gt;&apos;&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert><result>&quot;</result></nodeSetToConvert>
>          
> <expectedText>&lt;result&gt;&quot;&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert><result
> dq="double">data</result></nodeSetToConvert>
>          <expectedText>&lt;result
> dq=&quot;double&quot;&gt;data&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert><result
> sq='double'>data</result></nodeSetToConvert>
>          <!-- Single quotes get automatically converted to 
> double quotes -->
>          <expectedText>&lt;result
> sq=&quot;double&quot;&gt;data&lt;/result&gt;</expectedText>
>       </convertionTest>
>       <convertionTest>
>          <nodeSetToConvert>
>             <top double="two" single='one'>
>                <amp>&amp;</amp>
>                <greater>&gt;</greater>
>                <less>&lt;</less>
>                <apos>&apos;</apos>
>                <quot>&quot;</quot>
>             </top>
>          </nodeSetToConvert>
>          <expectedText>&lt;top double=&quot;two&quot;
> single=&quot;one&quot;&gt;&lt;amp&gt;&amp;amp;&lt;/amp&gt;&lt;
> greater&gt;&am
> p;gt;&lt;/greater&gt;&lt;less&gt;&amp;lt;&lt;/less&gt;&lt;apos
> &gt;&apos;&lt;
> /apos&gt;&lt;quot&gt;&quot;&lt;/quot&gt;&lt;/top&gt;</expectedText>
>       </convertionTest>
>    </convertedNodesetTest>
> </commonToolsData>

Current Thread