Main Content

matlab.io.xml.transform.StylesheetSourceDocument Class

Namespace: matlab.io.xml.transform

Stylesheet source document for transformation

Since R2021a

Description

Use an object of the matlab.io.xml.transform.StylesheetSourceDocument class to specify a matlab.io.xml.dom.Document object as the stylesheet to use for a transformation. You can provide a StylesheetSourceDocument object as the stylesheet input to the transform or transformToString method of a matlab.io.xml.transform.Transformer object.

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

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

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

example

Properties

expand all

Document containing stylesheet specified as a matlab.io.xml.dom.Document object.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
true

Methods

expand all

Examples

collapse all

Suppose that you want to customize a stylesheet for a transformation, but do not want to modify the file that contains the stylesheet. You can read the stylesheet file into a DOM document, modify the style in the document, and then use the document as the stylesheet for the transformation. Specify the stylesheet as a matlab.io.xml.transform.StylesheetSourceDocument object.

This example transforms the XML markup for countries and their capital cities into an HTML table. The original XSL file specifies a table with the default color, black. The example parses the XSL from the file into a matlab.io.xml.dom.Document object, modifies the style to make the table green, and passes the Document object to the transformation as a matlab.io.xml.transform.StylesheetSourceDocument object.

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>

Parse a stylesheet file into a matlab.io.xml.dom.Document object.

import matlab.io.xml.dom.*
import matlab.io.xml.transform.*
import matlab.io.xml.xpath.*

ssDoc = parseFile(Parser,"capitals.xsl");

Find the table element and add a style attribute that specifies the color green.

tableElem = evaluate(Evaluator,"//table",ssDoc);
styleAttr = createAttribute(ssDoc,"style");
setValue(styleAttr,"color:green");
setAttributeNode(tableElem,styleAttr);

Create a StylesheetSourceDocument object, stylesheetObj, that contains the Document object with the XSL markup to use for the transformation.

stylesheetObj = StylesheetSourceDocument(ssDoc);

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

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

Open capitals.html in a browser.

web("capitals.html")

Here is the HTML table:

Version History

Introduced in R2021a