Main Content

matlab.io.xml.transform.CompiledStylesheet 类

命名空间: matlab.io.xml.transform

编译的样式表

自 R2021a 起

描述

matlab.io.xml.transform.CompiledStylesheet 类的一个对象表示编译的样式表。您可以为 matlab.io.xml.transform.Transformer 对象的 transformtransformToString 方法提供 CompiledStylesheet 对象。

matlab.io.xml.transform.CompiledStylesheet 类是一个 handle 类。

类属性

ConstructOnLoad
true
HandleCompatible
true

有关类属性的信息,请参阅类属性

创建对象

要创建 matlab.io.xml.transform.CompiledStylesheet 对象,请调用 matlab.io.xml.transform.Transformer 对象的 compileStylesheet 方法。

示例

全部折叠

此示例将包含国家/地区及其首都信息的 XML 标记转换为 XML 表。该示例编译样式表,并将其传递给执行转换的转换方法。

该示例使用下列文件:

  • 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>

创建一个 Transformer 对象,并使用 compileStylesheet 方法编译样式表 capitals.xsl

import matlab.io.xml.transform.*
trObj = Transformer();
compiledStylesheetObj = compileStylesheet(trObj,"capitals.xsl");

执行转换,并提供 compiledStylesheetObj 作为样式表,capitals.xml 作为 XML 源,以及 capitals.html 作为输出文件的名称。

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

在浏览器中打开 capitals.html。

web("capitals.html")

下面是 HTML 表:

版本历史记录

在 R2021a 中推出