Skip to Content

XSLT transformation issue

Hi Everybody,

I have the following structure XML badly formatted provided from an external application. It's imposed like that.

<?xml version="1.0" encoding="UTF-8"?>

<n:root xmlns:n="http://www.something">

<order number="1234"/>

<orderline item="1" nom="moi"/>

<orderline item="2" nom="toi"/>

<order number="456"/>

<orderline item="1" nom="bien"/>

<orderline item="2" nom="mal"/>

</n:root>

Each <order> in followed by multiple <orderline> which belong to the <order< without any sequence except the order in xml.

I need to transform it into a better structure as follow

<n:root xmlns:n="http://www.something">

<order>

<number>1234</numero>

<orderline>

<item>1</item>

<nom>moi</nom>

</orderline>

<orderline>

<item>2</item>

<nom>toi</nom>

</orderline>

</order>

<order>

<number>456</number>

<orderline>

<item>1</item>

<nom>bien</nom>

</orderline>

<orderline>

<litem>2</item>

<nom>mal</nom>

</orderline>

</order>

</n:root>

I need to do that in XSLT without the use of PI mapping because I need to create a SOAP envelope completely want I need to put some fields dynamically in the soap-header.

Is there someone who can help me, please?

Kind Regards.

E. Koralewski

.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Jul 16, 2010 at 08:17 AM

    Hi Eric,

    i would recommend you to setup here two mappings: A first mapping converting the structure, a second to create the SOAP envelope.

    For the first mapping XSLT is not suitable, because you can t combine the order number to right items in a proper way. Actually only a very complexe [recursive script|https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/3557] [original link is broken] [original link is broken] [original link is broken]; could do that. It is much easier to use Java (or ABAP), because you just read a line and remember the order number. In case of a new order number you create a new order structure. The problem is that XSLT cant remember a number in a loop.

    Regards,

    Udo

    Add comment
    10|10000 characters needed characters exceeded

    • I found the solution,

      There is the coding of mu XSLT.

      http://www.w3.org/1999/XSL/Transform" xmlns:n="http://www.something" exclude-result-prefixes="n">

      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

      <xsl:key name="fils" match="orderline" use="generate-id(preceding-sibling::order[1])"/>

      <xsl:template match="/">

      <n:root xmlns:n="http://www.something">

      <xsl:apply-templates select="//order "/>

      </n:root>

      </xsl:template>

      <xsl:template match="orderline ">

      <orderline>

      <item>

      <xsl:value-of select="@item"/>

      </item>

      <nom>

      <xsl:value-of select="@nom"/>

      </nom>

      </orderline>

      </xsl:template>

      <xsl:template match="order ">

      <order>

      <number>

      <xsl:value-of select="@number"/>

      </number>

      <xsl:apply-templates select="key('fils',generate-id())"/>

      </order>

      </xsl:template>

      </xsl:stylesheet>