Additional menu

View Categories

Transform XML using XSLT

3 min read

As the action name suggests, you can use it to transform XML using XSLT transformation.

Inputs:
XML String – XML to transform
XSLT String – XSLT transformation to apply on the string
XSLT Parameters – parameters in form of a JSON object

Outputs:
XSLT Output

Examples

Example 1

Transform XML data into HTML table.

XML String:

<people>
  <person>
    <name>Paul Murana</name>
    <age>41</age>
    <location>Hersham</location>
    <favouriteHobby>Tennis</favouriteHobby>
  </person>
  <person>
    <name>Jane Doe</name>
    <age>34</age>
    <location>London</location>
    <favouriteHobby>Reading</favouriteHobby>
  </person>
  <person>
    <name>Tom Smith</name>
    <age>28</age>
    <location>Manchester</location>
    <favouriteHobby>Photography</favouriteHobby>
  </person>
  <person>
    <name>Sarah Johnson</name>
    <age>52</age>
    <location>Oxford</location>
    <favouriteHobby>Hiking</favouriteHobby>
  </person>
</people>

XSLT String:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/people">
    <html>
      <body>
        <h2>People</h2>
        <table border="1">
          <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Location</th>
            <th>Favourite Hobby</th>
          </tr>
          <xsl:for-each select="person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="age"/></td>
              <td><xsl:value-of select="location"/></td>
              <td><xsl:value-of select="favouriteHobby"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Example 2

Transform XML data into CSV.

XML String:

<people>
  <person>
    <name>Paul Murana</name>
    <age>41</age>
    <location>Hersham</location>
    <favouriteHobby>Tennis</favouriteHobby>
  </person>
  <person>
    <name>Jane Doe</name>
    <age>34</age>
    <location>London</location>
    <favouriteHobby>Reading</favouriteHobby>
  </person>
  <person>
    <name>Tom Smith</name>
    <age>28</age>
    <location>Manchester</location>
    <favouriteHobby>Photography</favouriteHobby>
  </person>
  <person>
    <name>Sarah Johnson</name>
    <age>52</age>
    <location>Oxford</location>
    <favouriteHobby>Hiking</favouriteHobby>
  </person>
</people>

XSLT String:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/people">
    <xsl:text>Name,Age,Location,Favourite Hobby&#10;</xsl:text>
    <xsl:for-each select="person">
      <xsl:value-of select="name"/><xsl:text>,</xsl:text>
      <xsl:value-of select="age"/><xsl:text>,</xsl:text>
      <xsl:value-of select="location"/><xsl:text>,</xsl:text>
      <xsl:value-of select="favouriteHobby"/><xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Example 3

Transform XML data into CSV including XSLT Parameters.

XML String:

<people>
  <person>
    <name>Paul Murana</name>
    <age>41</age>
  </person>
  <person>
    <name>Jane Doe</name>
    <age>34</age>
  </person>
  <person>
    <name>Tom Smith</name>
    <age>28</age>
  </person>
</people>

XSLT String:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="AgeThreshold" select="0"/>
  <xsl:output method="text"/>

  <xsl:template match="/people">
    <xsl:text>Name,Age&#10;</xsl:text>
    <xsl:for-each select="person[age &gt; $AgeThreshold]">
      <xsl:value-of select="name"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="age"/>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

XSLT Parameters:

{
  "AgeThreshold": "30"
}