473,383 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XSLT string replacement question

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
Jul 20 '05 #1
3 5679
In article <af**************************@posting.google.com >,
J Trost <jt****@earthlink.net> wrote:

% 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.
--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #2
In article <af**************************@posting.google.com >,
J Trost <jt****@earthlink.net> wrote:
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
Jul 20 '05 #3
In article <c9***********@pc-news.cogsci.ed.ac.uk>, I wrote:
You really don't want to do this. Use sed instead.


Of course, what I really meant was "use XInclude with parse='text'".

-- Richard
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: K.Simon | last post by:
Hello, it's very often neccessary to replace strings or a single character in my stylesheets. My solution looks awful and very long. Now i thought to solve this with an array like structure but...
0
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag,...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: Michiel Kamermans | last post by:
Hi, I need to generate a list based on a sorted nodeset, except duplicates need to be discarded. I initially though of doing a sort on a nodeset and then passing it to a template that iterates...
1
by: cibsbui | last post by:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:TP="urn:TemplateParser"> <xsl:template match="/"> ...
11
by: cmay | last post by:
I am having this problem... Lets say that your source XML is formatted like this: <somenode> Here is some text Here is some more text </somenode> When to a <xsl:value-of select="somenode" /I...
10
by: Bilal | last post by:
Hello, I'm trying to perform some string manipulations in my stylesheet and have gotten stuck on the issue below so hopefully can elicit some useful hints. Namely, the problem is that I need to...
3
by: shaun roe | last post by:
mild rant follows Working now for a couple of years with xslt and now xslt 2.0, does anyone else get the impression that xslt 2.0 somehow missed the point? Yes its got a fancy new data model...
1
by: qbp90x5lb | last post by:
I'm using an XSLT transform to output the element value contents from a simple XML file into a new .TXT file. Everything works fine except for certain XML files, when calling msxsl with the .xslt, I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.