Main Content

matlab.io.xml.transform.SourceFile 类

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

用于转换的 XML 源文件

自 R2021a 起

描述

使用 matlab.io.xml.transform.SourceFile 类的对象将一个文件指定为用于转换的源 XML 标记。您可以提供一个 SourceFile 对象作为 matlab.io.xml.transform.Transformer 对象的 transformtransformToString 方法的输入。

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

类属性

ConstructOnLoad
true
HandleCompatible
true

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

创建对象

描述

示例

sourceObj = matlab.io.xml.transform.SourceFile(path) 创建一个 Path 属性设置为指定路径的 matlab.io.xml.transform.SourceFile 对象。

属性

全部展开

XML 文件的路径,指定为字符串标量或字符向量。

属性:

GetAccess
public
SetAccess
public
GetObservable
true
SetObservable
true

示例

全部折叠

此示例将包含国家/地区及其首都信息的 XML 标记转换为 XML 表。该示例将输入 XML 指定为 matlab.io.xml.transform.SourceFile 对象。

该示例使用下列文件:

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

创建一个 SourceFile 对象 sourceObj,其中包含用于转换的 XML 源。

import matlab.io.xml.transform.*
sourceObj = SourceFile("capitals.xml");

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

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

在 Web 浏览器中打开 capitals.html

web("capitals.html")

下面是 HTML 表:

版本历史记录

在 R2021a 中推出