![]() |
|
|
|||||||
![]() |
XML - replacing < with < using xslt |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Hi all:
I have a source xml document with an element of type string. This element contains something like the following: <stringData> <Header> <Body> </stringData> I would like to apply an XSLT and replace all occurances of < with < and > with >. I tried the following but without success: <xsl:template match="stringData"> <xsl:variable name="lessThan" select='<'/> <xsl:value-of select="translate(text()[1], '<', $lessThan)"/> </xsl:template> I'd appreciate any help. Thanks tentstitcher@gmail.com |
|
|
|
|
#2 |
|
Posts: n/a
|
wrote:
> Hi all: > > I have a source xml document with an element of type string. This > element contains something like the following: > <stringData> > <Header> <Body> > </stringData> > > I would like to apply an XSLT and replace all occurances of < with < > and > with >. you don't need to : as this is not markup but text, escaping < and > with < and > is expected by parsers and applications if you have to produce a text output instead of an XML output, just do that : <xsl <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 < > <xsl:value-of select="translate(text()[1], '<', $lessThan)"/> > </xsl:template> > > I'd appreciate any help. > > Thanks > -- Cordialement, /// (. .) --------ooO--(_)--Ooo-------- | Philippe Poulard | ----------------------------- http://reflex.gforge.inria.fr/ Have the RefleX ! |
|
|
|
#3 |
|
Posts: n/a
|
In article <. com>,
<> wrote: >I would like to apply an XSLT and replace all occurances of < with < >and > 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 |
|
|
|
#4 |
|
Posts: n/a
|
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 < and > 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 < and > occurances. Thanks |
|
|
|
#5 |
|
Posts: n/a
|
Richard,
Thanks for the suggestion. Using <xsl:value-of select="text()" disable-output-escaping="yes"/> did the job. Appreciate it! |
|
|
|
#6 |
|
Posts: n/a
|
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 < and > > 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 : <xcl <xcl <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 > < and > occurances. > > Thanks > -- Cordialement, /// (. .) --------ooO--(_)--Ooo-------- | Philippe Poulard | ----------------------------- http://reflex.gforge.inria.fr/ Have the RefleX ! |
|
|
|
#7 |
|
Posts: n/a
|
wrote:
> Hi all: > > I have a source xml document with an element of type string. This > element contains something like the following: > <stringData> > <Header> <Body> > </stringData> See http://xml.silmaril.ie/authors/html/ ///Peter |
|