473,320 Members | 2,161 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,320 software developers and data experts.

How to add an element using XSL.

ppl
I'm very new to XSL and have come across a stumbling block with a
recent assignment at work. I need to translate from XML to XML using
an XSL style sheet. Here is the input XML:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="untitled9.xsl"?>
<booklist>
<book>
<title>bookname1</title>
<price>80</price>
<amount>3</amount>
</book>
<book>
<title>bookname2</title>
<price>60</price>
<amount>2</amount>
</book>
</booklist>

which needs to be transformed to:

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="untitled9.xsl"?>
<booklist>
<book>
<title>bookname1</title>
<price>80</price>
<amount>3</amount>
<total>240</total>
</book>
<book>
<title>bookname2</title>
<price>60</price>
<amount>2</amount>
<total>120</total>
</book>
</booklist>

I want to copy all content from source XML and insert an element on
specificall node, can anyone konw?
Jul 20 '05 #1
3 3795
Use the identity template and override it for "book" nodes like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="book">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<total>
<xsl:value-of select="price * amount"/>
</total>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

When this transformation is applied on your source.xml:
<?xml version="1.0" encoding="GB2312"?>
<booklist>
<book>
<title>bookname1</title>
<price>80</price>
<amount>3</amount>
</book>
<book>
<title>bookname2</title>
<price>60</price>
<amount>2</amount>
</book>
</booklist>

the wanted result is produced:

<booklist>
<book>
<title>bookname1</title>
<price>80</price>
<amount>3</amount>
<total>240</total></book>
<book>
<title>bookname2</title>
<price>60</price>
<amount>2</amount>
<total>120</total></book>
</booklist>

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"ppl" <pp******@sina.com> wrote in message
news:89**************************@posting.google.c om...
I'm very new to XSL and have come across a stumbling block with a
recent assignment at work. I need to translate from XML to XML using
an XSL style sheet. Here is the input XML:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="untitled9.xsl"?>
<booklist>
<book>
<title>bookname1</title>
<price>80</price>
<amount>3</amount>
</book>
<book>
<title>bookname2</title>
<price>60</price>
<amount>2</amount>
</book>
</booklist>

which needs to be transformed to:

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="untitled9.xsl"?>
<booklist>
<book>
<title>bookname1</title>
<price>80</price>
<amount>3</amount>
<total>240</total>
</book>
<book>
<title>bookname2</title>
<price>60</price>
<amount>2</amount>
<total>120</total>
</book>
</booklist>

I want to copy all content from source XML and insert an element on
specificall node, can anyone konw?

Jul 20 '05 #2
In article <bk***********@ID-152440.news.uni-berlin.de>,
Dimitre Novatchev <dn********@yahoo.com> wrote:

% <xsl:template match="@* | node()">
% <xsl:copy>
% <xsl:apply-templates select="@* | node()"/>
% </xsl:copy>
% </xsl:template>

I would write this one as

<xsl:template match="@* | node()">
<xsl:copy-of select='.'/>
</xsl:template>

Is there a difference in the result?
--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #3

"Patrick TJ McPhee" <pt**@interlog.com> wrote in message
news:bk**********@news.eusc.inter.net...
In article <bk***********@ID-152440.news.uni-berlin.de>,
Dimitre Novatchev <dn********@yahoo.com> wrote:

% <xsl:template match="@* | node()">
% <xsl:copy>
% <xsl:apply-templates select="@* | node()"/>
% </xsl:copy>
% </xsl:template>

I would write this one as

<xsl:template match="@* | node()">
<xsl:copy-of select='.'/>
</xsl:template>

Is there a difference in the result?

Very big difference if you need to do something additional to just copying
the whole document. In this last and trivial case one would simply have:

<xsl:copy-of select="/" />

The identity template (the first above), is instantiated for every attribute
and node(). This makes it possible to override this generic template just
for some nodes in order to modify or delete them when copying to the output.
The "copy at once template precludes any possibility of overriding, if it is
instantiated at all. To use it for such "copy all with the exception of ..."
tasks would be extremely awkward -- you'll have to process all ancestor
nodes of the nodes that must be treated differently, then process these "to
be treated differently" nodes, and only then use xsl:apply-templates, in
order to copy the rest of the tree.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


Jul 20 '05 #4

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

Similar topics

1
by: Igor | last post by:
Is there any way to resort and xml document using xslt based on element position. For example if I have xml like this: <root> <element> 1st thing </element> <element> 2nd thing </element>...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
1
by: pjeung | last post by:
Say that I have an element <elementA> that has several layers of subelements. In System.Xml.XmlDocument and related classes, how do I rename <elementA> to <elementB> leaving the subelements intact?
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
5
by: nospam | last post by:
I'm trying to transform an xml file that contains empty short elements like the following: <element attrib="abc"/> using the XmlTransform class. But I cannot seem to preserve the short format...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
3
by: namewitheldbyrequest | last post by:
"The XML element 'EnableTheming' from namespace 'http://tempuri.org/' is already present in the current scope" I created a Web Service: I imported System.Data.SqlClient so I could access SQL...
5
by: felipevaldez | last post by:
how can I put an element below another element, that's not absolutely positioned? so I have a n element, called X, and next to that, an element Y X Y XXXXXX
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.