Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file

Reply
Thread Tools

Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file

 
 
jkflens
Guest
Posts: n/a
 
      05-29-2006
Hello,

i convert one XML-document by using XSLT into another XML-document.
First change all attributes to elements is no problem.
Then i try to insert a new element into the new document by XSLT,
but it doesn't work correctly


Example:


The XML-source-document:

<?xml version="1.0" encoding="UTF-8"?>
<data creationTime="2006-05-31" creationNumber="1">
<set number="0001" info="test"/>
<set number="0002" info="test"/>
</data>


The following XML-destination-document has to become
(watch the new element "sets"):

<?xml version="1.0" encoding="UTF-8"?>
<data>
<creationTime>2006-05-31</creationTime>
<creationNumber>1</creationNumber>
<sets>
<set>
<number>0001</number>
<info>test</info>
</set>
<set>
<number>0002</number>
<info>test</info>
</set>
</sets>
</data>


Here is my XSLT-stylesheet-attempt:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml" indent="yes" />
<xsl:template match="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
<xsl:if test="name()='creationNumber'">
<sets/>
</xsl:if>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


Here is the result with wrong positioned "sets":

<?xml version="1.0" encoding="UTF-8"?>
<data>
<creationTime>2006-05-31</creationTime>
<creationNumber>1</creationNumber>
<sets/>
<set>
<number>0001</number>
<info>test</info>
</set>
<set>
<number>0002</number>
<info>test</info>
</set>
</data>

The element "sets" should not to be positioned simply before the first
set-Element,
it should be positiond around the set-Elements, thus as shown in the
XML Destinationdocument above.

Can anybody give me an XSLT-hint?

Thanks

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      05-29-2006


jkflens wrote:


> The XML-source-document:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <data creationTime="2006-05-31" creationNumber="1">
> <set number="0001" info="test"/>
> <set number="0002" info="test"/>
> </data>
>
>
> The following XML-destination-document has to become
> (watch the new element "sets"):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <data>
> <creationTime>2006-05-31</creationTime>
> <creationNumber>1</creationNumber>
> <sets>
> <set>
> <number>0001</number>
> <info>test</info>
> </set>
> <set>
> <number>0002</number>
> <info>test</info>
> </set>
> </sets>
> </data>


You need to write a template for data then that inserts the sets
elements as the parent for set elements:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput method="xml" indent="yes" />

<xsl:template match="data">
<xsl:copy>
<xsl:apply-templates select="@*" />
<sets>
<xsl:apply-templates />
</sets>
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:element name="{name()}"><xsl:value-of select="." /></xsl:element>
</xsl:template>

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

</xsl:stylesheet>
--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
jkflens
Guest
Posts: n/a
 
      05-30-2006
This is a top-XSLT-hint,

now it works correctly

THANKS for your hint martin

 
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
Insert Multiple Records Using One Insert Statemen with MySQLdb module anton.ranieri.it@gmail.com Python 1 12-06-2008 12:47 PM
Possible to Insert One HTML Doc Into Another? Pavlik Morosov HTML 11 08-11-2005 12:59 AM
How To Insert Code With Javascript, How to insert into a div an amountof code Sergio del Amo Javascript 4 05-29-2005 02:45 AM
Insert one ASP.net Page into another pei_world ASP .Net 1 12-27-2004 10:14 PM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57