Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > replacing &lt; with < using xslt

Reply
Thread Tools

replacing &lt; with < using xslt

 
 
tentstitcher@gmail.com
Guest
Posts: n/a
 
      05-19-2006
Hi all:

I have a source xml document with an element of type string. This
element contains something like the following:
<stringData>
&lt;Header&gt; &lt;Body&gt;
</stringData>

I would like to apply an XSLT and replace all occurances of &lt; with <
and &gt; with >.

I tried the following but without success:

<xsl:template match="stringData">
<xsl:variable name="lessThan" select='<'/>
<xsl:value-of select="translate(text()[1], '&lt;', $lessThan)"/>
</xsl:template>

I'd appreciate any help.

Thanks

 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      05-19-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all:
>
> I have a source xml document with an element of type string. This
> element contains something like the following:
> <stringData>
> &lt;Header&gt; &lt;Body&gt;
> </stringData>
>
> I would like to apply an XSLT and replace all occurances of &lt; with <
> and &gt; with >.


you don't need to : as this is not markup but text, escaping < and >
with &lt; and &gt; is expected by parsers and applications

if you have to produce a text output instead of an XML output, just do
that :

<xslutput method="text"/>
<xsl:template match="stringData">
<xsl:value-of select="text()"/>
</xsl:template>

the output will be :
<Header> <Body>

>
> I tried the following but without success:
>
> <xsl:template match="stringData">
> <xsl:variable name="lessThan" select='<'/>

illegal, use &lt;

> <xsl:value-of select="translate(text()[1], '&lt;', $lessThan)"/>
> </xsl:template>
>
> I'd appreciate any help.
>
> Thanks
>



--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      05-19-2006
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:

>I would like to apply an XSLT and replace all occurances of &lt; with <
>and &gt; with >.


You seem to want to transform character data into markup. XSLT is not
well suited to this, since it expects to be working with existing
document structure, and you are giving it (in effect) strings.

The simplest solution, if you can get it to work, is just to copy
the strings with output escaping disabled.

<xsl:value-of select="text()" disable-output-escaping="yes"/>

But you may have problems if the strings contain ampersands, since
those will not be escaped either.

The other approach is essentially to parse the text and create the
corresponding elements.

-- Richard
 
Reply With Quote
 
tentstitcher@gmail.com
Guest
Posts: n/a
 
      05-19-2006
Thanks for reply. The source document and the target document are both
SOAP messages. The <stringData> element is read out of the source's
SOAP Body and has to be placed in the target's SOAP Body. The &lt; and
&gt; have to be converted before being added to the target SOAP body so
that the body now contains an new xml element.

I tried the suggestion, yet the SOAP output of the XSLT retains the
&lt; and &gt; occurances.

Thanks

 
Reply With Quote
 
tentstitcher@gmail.com
Guest
Posts: n/a
 
      05-19-2006
Richard,

Thanks for the suggestion. Using <xsl:value-of select="text()"
disable-output-escaping="yes"/> did the job.

Appreciate it!

 
Reply With Quote
 
Philippe Poulard
Guest
Posts: n/a
 
      05-19-2006
(E-Mail Removed) wrote:
> Thanks for reply. The source document and the target document are both
> SOAP messages. The <stringData> element is read out of the source's
> SOAP Body and has to be placed in the target's SOAP Body. The &lt; and
> &gt; have to be converted before being added to the target SOAP body so
> that the body now contains an new xml element.


as Richard said, you can try :
<xsl:value-of select="text()" disable-output-escaping="yes"/>
but the output will be broken if the tags are not well-balanced, which
is the case in your example

you should consider an alternative tool such as RefleX (
http://reflex.gforge.inria.fr/ ) that can fix invalid markup thanks to
an HTML parser ; the process would be :

<xclarse name="input" source="file:///path/to/input.xml"/>
<xclarse-html name="data" text-source="{ string( $input/stringData ) }"/>
<xcl:document name="result">
<stringData>
{ $data }
</stringData>
</xcl:document>
<xcl:transform source="{ $result }" output="file:///path/to/result"/>

there are also means to update the $input, by replacing the old content
of <stringData> with the new one, made of markup

>
> I tried the suggestion, yet the SOAP output of the XSLT retains the
> &lt; and &gt; occurances.
>
> Thanks
>


--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      05-19-2006
(E-Mail Removed) wrote:
> Hi all:
>
> I have a source xml document with an element of type string. This
> element contains something like the following:
> <stringData>
> &lt;Header&gt; &lt;Body&gt;
> </stringData>


See http://xml.silmaril.ie/authors/html/

///Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
Replacing - and not Replacing... Rob Meade ASP General 5 04-11-2005 06:49 PM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
Replacing parts of xml with xslt C Gillespie XML 1 07-17-2004 03:28 PM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments