redaction
redaction

Reputation: 9

How do I complete my xsl file to have my XML transform in to an other XML

I have a XML file (that i provide here) and of this XML i want to transform an other XML file (you can see the result I wish to have in the second code under here) with the use of an .XSL file I want to accomplish this

The original XML

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Test.xsl"?>
<requests>
<request requestId="req0001" type="standard">
<shipmentAdress>
  <line1>33, del Plebiscito </line1>
  <line2>60100 Ancona</line2>
  <line3>Italy</line3>
</shipmentAdress>
<items>
  <item productId="L 220.kaalie" quantity="15" />
</items>
<note>dont come here realy big dog.</note>
</request>
<request requestId="req0003" type="express">
<shipmentAdress>
  <line1>Ignazio 52</line1>
  <line2>00186 ROMA</line2>
  <line3>Lazio</line3>
  <line4>Italy</line4>
</shipmentAdress>
<items>
  <item productId="frindutyloiuunt" quantity="100" />
  <item productId="L 149.kilopoz" quantity="50" />
  <item productId="L 160.rtyniue" quantity="150" />
</items>
</request>
<request requestId="req0002" type="express">
<shipmentAdress>
  <line1>Ignazio 54</line1>
  <line2>00186 ROMA</line2>
  <line3>Lazio</line3>
  <line4>Italy</line4>
</shipmentAdress>
<items>
  <item productId="fulky245" quantity="150" />
  <item productId="kilmoniy23" quantity="500" />
  <item productId="Frienudyt34" quantity="10" />
  <item productId="DERULIA45" quantity="200" />
</items>
<note>This is <b>hard to find</b> !</note>
</request>
</requests>

The XML how it needs to look after my transformation

<?xml version="1.0" encoding="utf-8"?>
<expressOrders>
<request city="00186 ROMA">
<req>req0003</req>
<items>
  <item productId="frindutyloiuunt" quantity="100" />
  <item productId="L 149.kilopoz" quantity="50" />
  <item productId="L 160.rtyniue" quantity="150" />
</items>
</request>
<request city="00186 ROMA">
<req>req0002</req>
<items>
  <item productId="fulky245" quantity="150" />
  <item productId="kilmoniy23" quantity="500" />
  <item productId="Frienudyt34" quantity="10" />
  <item productId="DERULIA45" quantity="200" />
</items>
</request>
</expressOrders>

My xsl file

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="requests">
<expressOrders>

</expressOrders>
</xsl:template>
<xsl:template match="request">

</xsl:template>
</xsl:stylesheet>

Now I see the need to select the requests of the type express, set the line2"00186 ROMA" as the attribute of city, the attribute requestId as a childelement of request and copy the request element of my item element. But after a few steps I get stuck.

How can I achieve this.

Upvotes: 0

Views: 68

Answers (1)

matthias_h
matthias_h

Reputation: 11416

The following XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
  <xsl:template match="requests">
    <expressOrders>
      <xsl:apply-templates select="request[@type='express']"/>
    </expressOrders>
  </xsl:template>
  <xsl:template match="request">
    <request>
      <xsl:attribute name="city">
        <xsl:value-of select="shipmentAdress/line2"/>
      </xsl:attribute>
      <req><xsl:value-of select="@requestId"/></req>
      <xsl:copy-of select="items"/>
    </request>
  </xsl:template>
</xsl:stylesheet>

when applied to your input XML produces the output

<?xml version="1.0" encoding="utf-8"?>
<expressOrders>
  <request city="00186 ROMA">
    <req>req0003</req>
    <items>
      <item productId="frindutyloiuunt" quantity="100"/>
      <item productId="L 149.kilopoz" quantity="50"/>
      <item productId="L 160.rtyniue" quantity="150"/>
    </items>
  </request>
  <request city="00186 ROMA">
    <req>req0002</req>
    <items>
      <item productId="fulky245" quantity="150"/>
      <item productId="kilmoniy23" quantity="500"/>
      <item productId="Frienudyt34" quantity="10"/>
      <item productId="DERULIA45" quantity="200"/>
    </items>
  </request>
</expressOrders>

In the template matching requests templates are applied only to request elements with the type express: <xsl:apply-templates select="request[@type='express']"/>

The template matching request sets the attribute city to the request element:

<request>
  <xsl:attribute name="city">
    <xsl:value-of select="shipmentAdress/line2"/>
  </xsl:attribute>

sets the value of requestId as value in the req node:

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

and copies the items node (including all item child nodes) to the request:

<xsl:copy-of select="items"/>

As useful resource for XPath syntax and location paths you can check http://www.w3.org/TR/1999/REC-xpath-19991116/#path-abbrev

Upvotes: 2

Related Questions