Main Content

matlab.io.xml.transform.Transformer Class

Namespace: matlab.io.xml.transform

XML document transformer

Since R2021a

Description

Use an object of the matlab.io.xml.transform.Transformer class to define an XML document transformer that transforms an XML document into another type of document. For example, use a transformer to transform an XML document into an HTML document.

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

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

transformer = Transformer() creates an XML document transformer.

example

Properties

expand all

Specifies the number of spaces used to pretty-print XML output generated by the transformer. The term pretty print refers to the use of new line and space characters in the transformed output to indicate XML document hierarchy. For example, the following output illustrates the use of pretty printing to represent a document hierarchy consisting of a para element containing a text element:

<para>
    <text>Hello World</text>
</para>

Note

A transformer pretty prints output only if the stylesheet output element (xsl:output) specifies that the output should be indented XML.

Path of an external schema to use to validate a document to be transformed, specified as a character vector or string scalar. This property applies only to source documents that use namespaces to name elements and attributes.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Path of an external schema to use to validate a document to be transformed, specified as a character vector or string scalar. This property applies only to source documents that do not use namespaces to name elements and attributes.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Whether to validate the input document and stylesheet, specified as true or false. If the value is true, the transform method of this transformer validates the input document and stylesheet.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Character encoding of the output document, specified as "UTF-8" or "UTF-16".

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Object that resolves stylesheet entity references, specified as an object of a subclass of the matlab.io.xml.dom.EntityResolver class.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Stylesheet execution tracer, specified as a matlab.io.xml.transform.Tracer object. Use this property to specify options for tracing the execution of a stylesheet.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true
Transient
true

Methods

expand all

Examples

collapse all

This example transforms the XML markup for countries and their capital cities into an HTML table.

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>

Call the transform method to transform the XML markup in capitals.xml using the stylesheet in the file capitals.xsl and store the result in the file capitals.html.

import matlab.io.xml.transform.*
transform(Transformer,"capitals.xml","capitals.xsl","capitals.html");

Open capitals.html in a browser.

web("capitals.html")

Here is the HTML table:

Algorithms

The MATLAB® Transformer object supports the use of many, but not all, standard XPath functions in stylesheets. For a list of supported XPath functions, see matlab.io.xml.xpath.Evaluator.

The Transformer object also supports these standard xslt functions in stylesheets and xslt extension (EXSLT) function categories:

xslt Functions in StylesheetsEXSLT Functions
  • current

  • document

  • element-available

  • format-number

  • function-available

  • generate-id

  • key

  • system-property

  • unparsed-entity-uri

For more information on definitions and usage of functions in stylesheets, see XSL Transformations (XSLT) on the World Wide Web Consortium website.

Common:

  • exsl:nodeset

  • exsl:object-type

  • exsl:document

Math:

  • math:abs

  • math:acos

  • math:asin

  • math:atan

  • math:atan2

  • math:constant

  • math:cos

  • math:exp

  • math:highest

  • math:log

  • math:lowest

  • math:max

  • math:min

  • math:power

  • math:random

  • math:sin

  • math:sqrt

  • math:tan

Set:

  • set:difference

  • set:distinct

  • set:has-same-node

  • set:intersection

  • set:leading

  • set:trailing

String:

  • str:align

  • str:concat

  • str:decode-uri

  • str:encode-uri

  • str:padding

Dynamic:

  • dyn:evaluate

Date and Time:

  • date:date-time

Version History

Introduced in R2021a

expand all