Main Content

matlab.io.xml.transform.CompiledStylesheet class

Package: matlab.io.xml.transform

Compiled stylesheet

Description

An object of the matlab.io.xml.transform.CompiledStylesheet class represents a compiled stylesheet. You can provide a CompiledStylesheet object to the transform or transformToString method of a matlab.io.xml.transform.Transformer object.

The matlab.io.xml.transform.CompiledStylesheet class is a handle class.

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

To create a matlab.io.xml.transform.CompiledStylesheet object, call the compileStylesheet method of a matlab.io.xml.transform.Transformer object.

Examples

collapse all

This example transforms the XML markup for countries and their capital cities into an HTML table. The example compiles the stylesheet and passes it to the transform method that performs the transformation.

The example uses these files:

  • capitals.xml

<Countries>
    <Country><Name>Canada</Name><Capital>Ottawa</Capital></Country>
    <Country><Name>France</Name><Capital>Paris</Capital></Country>
    <Country><Name>Peru</Name><Capital>Lima</Capital></Country>
</Countries>
  • capitals.xsl

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
      <table>
      <tr>
        <th>Country</th>
        <th>Capital</th>
      </tr>
      <xsl:for-each select="Countries/Country">
        <tr>
          <td><xsl:value-of select="Name"/></td>
          <td><xsl:value-of select="Capital"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Create a Transformer object and use the compileStylesheet method to compile the stylesheet capitals.xsl.

import matlab.io.xml.transform.*
trObj = Transformer();
compiledStylesheetObj = compileStylesheet(trObj,"capitals.xsl");

Perform the transformation and provide compiledStylesheetObj as the stylesheet, capitals.xml as the XML source, and capitals.html as the name of the output file.

transform(Transformer,"capitals.xml",compiledStylesheetObj,"capitals.html");

Open capitals.html in a browser.

web("capitals.html")

Here is the HTML table:

Introduced in R2021a