Main Content

transform

Class: matlab.io.xml.transform.Transformer
Package: matlab.io.xml.transform

Transform XML document

Syntax

transform(transformer,input,stylesheet,output)
document = transform(transformer,input,stylesheet)
transform(transformer,input,output)
document = transform(transformer,input)

Description

transform(transformer,input,stylesheet,output) uses transformer to transform input using stylesheet and stores the result in the location specified by output.

document = transform(transformer,input,stylesheet) returns the result as a matlab.io.xml.dom.Document object.

transform(transformer,input,output) requires that the input document contains a processing instruction that specifies the stylesheet and stores the result in the location specified by output. For example, this markup at the beginning of an XML document specifies a stylesheet named catalog.xsl that is located in the current folder.

<?xml version="1.0" encoding="UTF-8"?>
 <?xml-stylesheet type="text/xsl" href="catalog.xsl"?>

document = transform(transformer,input) requires that the input document contains a processing instruction that specifies the stylesheet and returns the result as a matlab.io.xml.dom.Document object.

Input Arguments

expand all

XML document transformer, specified as a matlab.io.xml.transform.Transformer object.

XML to transform, specified as one of these values:

ValueUse to specify
character vector, string scalar, or matlab.io.xml.transform.SourceFile objectXML file path
matlab.io.xml.transform.SourceString objectXML string
matlab.io.xml.dom.Document, or matlab.io.xml.transform.SourceDocument objectParsed XML document

XML to transform, specified as one of these values:

ValueUse to specify
character vector, string scalar, or matlab.io.xml.transform.StylesheetSourceFile objectStylesheet file path
matlab.io.xml.transform.SourceString objectXSL markup
matlab.io.xml.dom.Document, or matlab.io.xml.transform.StylesheetSourceDocument objectParsed document
matlab.io.xml.transform.CompiledStylesheet objectCompiled stylesheet

Location to store transformed output, specified as one of these values:

ValueUse to specify
character vector, string scalar, or matlab.io.xml.transform.ResultFile objectFile path
matlab.io.xml.transform.ResultString objectResult string
matlab.io.xml.transform.ResultDocument objectParsed document

Output Arguments

expand all

Transformation result, returned as a matlab.io.xml.dom.Document object.

Examples

expand 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:

To return a transformation result as a matlab.io.xml.dom.Document object, provide an output argument, instead of specifying the result location as an input argument.

import matlab.io.xml.transform.*
doc = transform(Transformer,"capitals.xml","capitals.xsl");
Introduced in R2021a