RE: [xsl] Escaping <xsl:element> ouput

Subject: RE: [xsl] Escaping <xsl:element> ouput
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 7 Aug 2008 10:49:04 +0100
Thanks, yes, a much clearer description of the problem.

I would tackle this in the serialization phase rather than the
transformation phase. How you do this depends on your environment. In many
cases the simplest approach is probably to write out the HTML using the
standard serializer, and then put it through a simple script (sed or perl)
that replaces " by \". A more elaborate solution with a processor like Saxon
would be to write a custom output method by subclassing the HTML output
method.

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

 

> -----Original Message-----
> From: Anirvan Majumdar [mailto:anirban.majumdar@xxxxxxxxxxxxx] 
> Sent: 07 August 2008 10:29
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Escaping <xsl:element> ouput
> 
> True, even I felt that the explanation of my problem was a 
> bit convoluted. I'll try and simply put across what it is 
> that I'm trying to achieve here [though the scope of the 
> whole problem is a bit too vast]:
> 
> Considering that I have an input XML like:
> 
> <person>
>     <name>ABC</name>
>     <age>24</age>
>     <city>Bangalore</city>
> </person>
> 
> This XML data needs to be converted into an HTML-like snippet 
> which would then be used to construct a JSON object. I'm not 
> quite sure whether you've worked with JSON before, but it is 
> simply a name-value pair construct in JavaScript. Each value 
> needs to be enclosed within double-quotes ("). So basically, 
> whatever value has to go in, CANNOT contain any ", or all " 
> need to be escaped (\").
> 
> So coming back to the XML above, the XSLT should generate a 
> HTML-like snippet shown below:
> 
> <table width=\"100%\">
>     <tr>
>        <td width=\"150\">
>           Name :
>           <input type=\"text\" value=\"[name_node_value]\"/>
>        </td>
>     </tr>
>     <tr>
>        <td width=\"150\">
>           Age :
>           <input type=\"text\" value=\"[age_node_value]\"/>
>        </td>
>     </tr>
>     <tr>
>        <td width=\"150\">
>           City :
>           <input type=\"text\" value=\"[city_node_value]\"/>
>        </td>
>     </tr>
> </table>
> 
> This text can then be set as a value in the JSON construct.
> 
> The alogrithm I tried to adopt was:
> 1. Process i/p XML and store transformed text in a variable.
> 2. Use the text within this variable and use the in-built 
> XSLT function replace, to substitute all occurrences of " with \"
> 
> I hope this would help you understand my problem a bit better.
> 
> Thanks.
> 
> 
> 
> Michael Kay wrote:
> > You're talking about "tags". Tags don't exist in the data 
> manipulated 
> > by XSLT. XSLT only manipulates nodes in a tree. The way to 
> solve your 
> > problem is to use XSLT the way it was designed to be used: forget 
> > about CDATA and tags, and think about element nodes.
> >
> > Now take a step back, and tell us what you actually want to 
> achieve - 
> > that is, the input and output of your transformation, 
> rather than your 
> > incorrect attempts at coding the solution.
> >
> > Michael Kay
> > http://www.saxonica.com/
> >
> >   
> >> -----Original Message-----
> >> From: Anirvan Majumdar [mailto:anirban.majumdar@xxxxxxxxxxxxx]
> >> Sent: 07 August 2008 09:38
> >> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >> Subject: [xsl] Escaping <xsl:element> ouput
> >>
> >> Hello,
> >>
> >> I have a <xsl:variable> element which contains HTML snippet. 
> >> Parts of this snippet are generated by transformations of 
> other XSLT 
> >> elements present.
> >> Next on, I'd like to take this <xsl:variable>'s value and 
> apply some 
> >> string manipulation templates on it. I realized that if I 
> wanted to 
> >> extract the value inclusive of tags then I needed to 
> include the HTML 
> >> code, and not the XSLT code, within <![CDATA]]> sections. 
> Everything 
> >> seemed to be going fine until I realized that all the content 
> >> generated through the <xsl:element>s was missing. The tags are 
> >> ignored and only the node value is captured.
> >>
> >> How can I get around this problem? I checked and there is 
> no way for 
> >> output escaping in <xsl:element>, and neither is there any 
> point of 
> >> putting the code within <[CDATA[]]> since the 
> transformations won't 
> >> occure only.
> >>
> >> I also thought of using <xsl:copy-of> to extract the 
> variable value, 
> >> but this isn't of much use to me, since the data extracted through 
> >> copy-of cannot be used for any further processing as such. 
> I can only 
> >> display this value [something like <xsl:value-of>]
> >>
> >> Here's an example of the kind of XSL I'm working on:
> >> <xsl:variable name="someVar">
> >>    <![CDATA[
> >>    <span id="1">
> >>       <input type="text" value="A" name="txtVal"/>
> >>       ]]>
> >>       <xsl:element name="input">
> >>          <xsl:attribute name="type">hidden<xsl:attribute>
> >>          <xsl:attribute name="value">[some value computed 
> >> before]</xsl:attribute>
> >>       </xsl:elelement>
> >>    <![CDATA[
> >>    </span>
> >>    ]]>
> >> </xsl:variable>

Current Thread