XSLT string replacement question

J

J Trost

I was wondering if anyone knows if it is possible to do basic string
replacement using XSLT even though the strings being replaced may
contain "<" and ">". Here is my problem:

I need to be able to convert XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
<object class="javax.swing.JButton">
<string>Hello, world</string>
</object>
</java>

Into XML like this(notice each "<" is replaced with "&lt;" and each
">" has been replaced with "&gt; and then it was wrapped in a SOAP
envelope"):

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmln
s:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SubmitRequest xmlns="http://apps.someurl.com/somePath/">
<xmlRequest>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;java version="1.4.2_03" class="java.beans.XMLDecoder"&gt;
&lt;object class="javax.swing.JButton"&gt;
&lt;string&gt;Hello, world&lt;/string&gt;
&lt;/object&gt;
&lt;/java&gt;
</xmlRequest>
</SubmitRequest>
</soap:Body>
</soap:Envelope>

I know it is possible to do this using just about any programming
language, but I want to know if it can be done using XSLT stylesheets.
If it is possible does anyone know how? Or could you direct me to a
tutorial on how to do this?

Thank you,

--J
 
P

Patrick TJ McPhee

% I was wondering if anyone knows if it is possible to do basic string
% replacement using XSLT even though the strings being replaced may
% contain "<" and ">". Here is my problem:

What you're doing isn't really string replacement, since the things
that need replacing aren't available to the style sheet as strings.

% <?xml version="1.0" encoding="UTF-8"?>

I don't think the XML declaration is available to XSLT at all.

% <java version="1.4.2_03" class="java.beans.XMLDecoder">

For the other things, you could do something like
<xsl:template match="*">
<xsl:text>&amp;lt;</xsl:text>
<xsl:value-of select='name()'/>
<xsl:for-each select='@*'>
<xsl:text> </xsl:text>
<xsl:value-of select='name()'/>
<xsl:text>="</xsl:text>
<xsl:value-of select='.'/>
<xsl:text>"</xsl:text>
</xsl:for-each>
<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>

This will not give you your original document with <, &, and >
replaced by entities, but it ought to give you an equivalent
document.

I wouldn't even consider using XSLT for this task, by the way.
 
R

Richard Tobin

J Trost said:
I need to be able to convert XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
<object class="javax.swing.JButton">
<string>Hello, world</string>
</object>
</java>
Into XML like this
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;java version="1.4.2_03" class="java.beans.XMLDecoder"&gt;
&lt;object class="javax.swing.JButton"&gt;
&lt;string&gt;Hello, world&lt;/string&gt;
&lt;/object&gt;
&lt;/java&gt;

You can't do the XML declaration, because XSLT doesn't see it. You can
do the rest, but it will be tedious.

You need a template that matches any element. It should then output
"<" as text, followed by the local-name() of the element, and a space.
Then apply-templates to the attributes. Output ">", then apply-templates
to the children. Handle the end-tag in the same way.

The template for the attributes will be even more tedious, because you
will need to handle the possibility of quotes in the attribute value.

Similarly, the template for text will have to handle & and < specially
(you will need to output &amp;amp; and &amp;lt;).

If your XML may use namespaces, you will need to look at the namespace
nodes to determine appropriate prefixes for the element and attribute
names, and you will have to output namespace declarations.

You really don't want to do this. Use sed instead.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top