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

J

jkflens

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
 
M

Martin Honnen

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>
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top