Main Content

transformToString

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

Transform XML document and return result as a string

Since R2021a

Syntax

result = transformToString(transformer,input,stylesheet)
result = transformToString(transformer,input)

Description

result = transformToString(transformer,input,stylesheet) uses transformer to transform input using stylesheet and returns the result as a string scalar.

result = transformToString(transformer,input) requires that the input document contains a processing instruction that specifies the stylesheet. 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"?>

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, matlab.io.xml.dom.SourceDocument objectParsed XML document

Stylesheet for transformation, 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.StylesheetSourceString objectXSL markup
matlab.io.xml.dom.Document, or matlab.io.xml.transform.StylesheetSourceDocument objectParsed document
matlab.io.xml.transform.CompiledStylesheet objectCompiled stylesheet

Output Arguments

expand all

Transformation result, returned as a string scalar.

Examples

expand all

This example transforms the XML markup for countries and their capital cities into an HTML table and returns the resulting HTML as a string scalar.

The examples use 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>

Use the transformToString method to transform the XML in capitals.xml to an HTML string. Specify that the stylesheet is capitals.xsl.

import matlab.io.xml.transform.*
result = transformToString(Transformer,"capitals.xml","capitals.xsl");

Display the resulting HTML string.

result
result = 
    "<html>
     <body>
     <table>
     <tr>
     <th>Country</th><th>Capital</th>
     </tr>
     <tr>
     <td>Canada</td><td>Ottawa</td>
     </tr>
     <tr>
     <td>France</td><td>Paris</td>
     </tr>
     <tr>
     <td>Peru</td><td>Lima</td>
     </tr>
     </table>
     </body>
     </html>
     "

Version History

Introduced in R2021a