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

Discussion in 'XML' started by jkflens, May 29, 2006.

  1. jkflens

    jkflens Guest

    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
     
    jkflens, May 29, 2006
    #1
    1. Advertising

  2. Re: Problem to insert an XML-element by XSLT-converting from oneXML-file into another XML-file

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

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 29, 2006
    #2
    1. Advertising

  3. jkflens

    jkflens Guest

    This is a top-XSLT-hint,

    now it works correctly

    THANKS for your hint martin
     
    jkflens, May 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Y.S.
    Replies:
    3
    Views:
    1,025
    strajan
    Sep 17, 2003
  2. loveNUNO
    Replies:
    2
    Views:
    924
    loveNUNO
    Nov 20, 2003
  3. H. Kaya
    Replies:
    1
    Views:
    492
    Soren Kuula
    Feb 13, 2004
  4. HANM
    Replies:
    2
    Views:
    723
    Joseph Kesselman
    Jan 29, 2008
  5. Replies:
    5
    Views:
    259
Loading...

Share This Page