XML Transformation

The xml-transformation module lets you transform an xml with xslt (extensible stylesheet language transformation). In case you shouldn’t know xslt, you can learn it on w3schools.com.

You can achieve two goals with this module, depending on your needs:

  • Transform an xml to a special format, which automatically transforms to a table.
  • Transform xml’s to another format.

Transform an xml to a table

This requires a transformation, which brings the source-xml to the following result-xml format:

<it_table>
    <it_row>
        <myfield>The Field</myfield>
        <another>The Other</another>
    </it_row>
</it_table>

When you open the xml-transformation for the first time, it will automatically create a template, upon which you can build up your transformation. If you preview the template without changing anything, it results in the xml-example above. The xslt-template looks like this:

<?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="/">
<it_table>
    <it_row>
        <myfield>The Field</myfield>
        <another>The Other</another>
    </it_row>
</it_table>
</xsl:template>

</xsl:stylesheet>

Real example

Say we get the following xml from our datasource and want to output the productname and the productprice to the output table. As you can see, it is not a beauty of an xml, as it has namespaces and nested elements as well.

<?xml version="1.0" encoding="utf-8" ?>
<ProductList xmlns:tst="http://www.freedevelopertutorials.com/testspace">
  <Products>
    <tst:Product>
      <Name>Prod 1</Name>
      <Description>This is prod 1</Description>
      <tst:ProductAttributes>
        <Color>Green</Color>
        <Price>300$</Price>
      </tst:ProductAttributes>
    </tst:Product>
    <tst:Product>
      <Name>Prod 2</Name>
      <Description>This is prod 2</Description>
      <tst:ProductAttributes>
        <Color>Blue</Color>
        <Price>450$</Price>
      </tst:ProductAttributes>
    </tst:Product>
    <tst:Product>
      <Name>Prod 3</Name>
      <Description>This is prod 3</Description>
      <tst:ProductAttributes>
        <Color>Purple</Color>
        <Price>145$</Price>
      </tst:ProductAttributes>
    </tst:Product>
  </Products>
</ProductList>

To get our preferred output, we can use the following xslt. It registers the tst-namespace and then iterates over each product from the xml above. Make sure no included namespaces go to the result, by using the exclude-result-prefixes!

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tst="http://www.freedevelopertutorials.com/testspace"
    exclude-result-prefixes="tst">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <it_table>
      <xsl:for-each select="ProductList/Products/tst:Product">
        <it_row>
          <ProductName><xsl:value-of select="Name"/></ProductName>
          <ProductPrice><xsl:value-of select="tst:ProductAttributes/Price"/></ProductPrice>
        </it_row>
      </xsl:for-each>
    </it_table>
  </xsl:template>

</xsl:stylesheet>

When the transformation is executed, it results as follows:

<?xml version="1.0" encoding="utf-8"?>
<it_table>
  <it_row>
    <ProductName>Prod 1</ProductName>
    <ProductPrice>300$</ProductPrice>
  </it_row>
  <it_row>
    <ProductName>Prod 2</ProductName>
    <ProductPrice>450$</ProductPrice>
  </it_row>
  <it_row>
    <ProductName>Prod 3</ProductName>
    <ProductPrice>145$</ProductPrice>
  </it_row>
</it_table>

Transform an xml to another format

Simply delete or adjust the generated template, so that it doesn’t convert the xml to the it_table-format from above.