Main Content

matlab.io.xml.transform.SourceDocument class

Package: matlab.io.xml.transform

XML source document for transformation

Description

Use an object of the matlab.io.xml.transform.SourceDocument class to specify a matlab.io.xml.dom.Document object as the source XML markup for a transformation. You can provide a SourceDocument object as an input to the transform or transformToString method of a matlab.io.xml.transform.Transformer object.

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

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

example

sourceObj = matlab.io.xml.transform.SourceDocument(doc) creates a matlab.io.xml.transform.SourceFile object with the Document property set to the specified matlab.io.xml.dom.Document object.

Properties

expand all

XML document, specified as a matlab.io.xml.dom.Document object.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
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 specifies the input XML as a matlab.io.xml.transform.SourceDocument object.

The example uses the file 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 document with element nodes for the countries and capital cities.

import matlab.io.xml.dom.*

doc = Document("Countries");
docRootNode = getDocumentElement(doc);
names = ["Canada" "France" "Peru"];
capitals = ["Ottawa" "Paris" "Lima"];
for i=1:3
    countryElement = createElement(doc,"Country");
    nameElement = createElement(doc,"Name");
    appendChild(nameElement,createTextNode(doc,names(i)));
    capitalElement = createElement(doc,"Capital");
    appendChild(capitalElement,createTextNode(doc,capitals(i)));
    appendChild(countryElement,nameElement);
    appendChild(countryElement,capitalElement);
    appendChild(docRootNode,countryElement);
end

Create a SourceDocument object, sourceObj, to contain the document.

import matlab.io.xml.transform.*
sourceObj = SourceDocument(doc);

Perform the transformation and provide sourceObj as the XML input, capitals.xsl as the stylesheet, and capitals.html as the name of the output file.

transform(Transformer,sourceObj,"capitals.xsl","capitals.html");

Open capitals.html in a browser.

web("capitals.html")

Here is the HTML table:

Introduced in R2021a