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

jkflens

Usenet Archive
Joined
May 29, 2006
Messages
2
Reaction score
0
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput 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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:eek:utput 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>
 

jkflens

Usenet Archive
Joined
May 29, 2006
Messages
2
Reaction score
0
This is a top-XSLT-hint,

now it works correctly

THANKS for your hint martin
 

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

No members online now.

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top