将 HTML 转换为 DOM 对象的要求
要将 HTML 内容转换为 mlreportgen.dom.HTML
或 mlreportgen.dom.HTMLFile
对象,HTML 内容必须是 XML 可解析的。当 HTML 内容符合正确格式的 XML 规则时,它是 XML 可解析的,例如:
为所有元素添加结束标签。
元素的开始和结束标签(开始和结束)均使用小写字母。例如,对段落元素使用
<p>
和</p>
,而不是<P>
和</P>
。正确嵌套元素。如果在另一个元素内打开一个元素,请先关闭第一个元素,然后再关闭包含它的元素。
将属性值括在引号中。例如,使用
<p align="center"></p>
。
有关详细信息,请参阅 www.w3schools.com/xml/xml_syntax.asp 上的 W3Schools XML 规则摘要。
提示
为了使 HTML 内容可 XML 解析,您可以使用 mlreportgen.utils.html2dom.prepHTMLString
、mlreportgen.utils.html2dom.prepHTMLFile
和 mlreportgen.utils.tidy
。请参阅转换之前准备 HTML。
支持的 HTML 元素和属性
此表显示了将 HTML 转换为 DOM 对象时支持的 HTML 元素和属性。不受支持的元素和属性将被忽略。
HTML 元素 | 属性 |
---|---|
a | class , style , href , name |
address | class , style |
b | class , style |
big | class , style |
blockquote | class , style |
body | class , style |
br | 不适用 |
center | class , style |
cite | class , style |
code | class , style |
dd | class , style |
del | class , style |
dfn | class , style |
div | class , style |
dl | class , style |
dt | class , style |
em | class , style |
font | class , style , color , face , size |
h1, h2, h3, h4, h5, h6 | class , style , align |
hr | class , style , align |
i | class , style |
ins | class , style |
img | class , style , src , height , width |
kbd | class , style |
li | class , style |
mark | class , style |
nobr | class , style |
ol | class , style |
p | class , style , align |
pre | class , style |
s | class , style |
samp | class , style |
small | class , style |
span | class , style |
strike | class , style |
strong | class , style |
sub | class , style |
sup | class , style |
table | class , style , align , bgcolor , border , cellspacing , cellpadding , frame , rules , width |
tbody | class , style , align , valign |
tfoot | class , style , align , valign |
thead | class , style , align , valign |
td | class , style , bgcolor , height , width , colspan , rowspan ,align , valign , nowrap |
th | class , style , bgcolor , height , width , colspan , rowspan ,align , valign , nowrap |
tr | class , style , align ,bgcolor , valign |
tt | class , style |
u | class , style |
ul | class , style |
var | class , style |
有关这些元素的信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/HTML/Element。
所有元素均支持 HTML CSS 样式属性
您可以使用 HTML 样式属性来格式化追加到 DOM 报 告的 HTML 内容。样式属性是层叠样式表 (CSS) 格式的字符串。
支持以下 CSS 格式:
background-color
border
border-bottom
border-bottom-color
border-bottom-style
boder-bottom-width
border-color
border-left
border-left-color
border-left-style
boder-left-width
border-right
border-right-color
border-rigtht-style
border-right-width
border-style
border-top
border-top-color
border-top-style
border-top-width
border-width
color
counter-increment
counter-reset
display
font-family
font-size
font-style
font-weight
height
line-height
list-style-type
margin
margin-bottom
margin-left
margin-right
margin-top
padding
padding-bottom
padding-left
padding-right
padding-top
text-align
text-decoration
text-indent
vertical-align
white-space
width
有关这些格式的信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/CSS/Reference。
支持 HTML 字符实体
您可以追加包含特殊字符的 HTML 内容,例如英镑符号、美元符号或保留的 XML 标记字符。XML 标记特殊字符是 >
、<
、&
、"
和 '
。要包含特殊字符,请使用 HTML 命名或数字字符引用。例如,若要在要追加的 HTML 内容中包含左尖括号 (<),请使用以下字符实体引用之一:
命名字符实体引用
<
数字字符实体引用
&003c;
更多信息请参阅 https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references。
DOCTYPE 声明
追加到 DOM 报 告的 HTML 内容不需要包含文档类型声明(请参阅 https://en.wikipedia.org/wiki/Document_type_declaration)。如果内容包含文档类型声明,则必须满足以下条件:
如果内容包含字符实体引用(特殊字符),则文档类型声明必须引用定义所引用实体的文档类型定义 (DTD)。例如,以下声明指定了定义所有 HTML 字符实体的 DTD 文件:
<!DOCTYPE html SYSTEM "html.dtd">
html.dtd
包含在 MATLAB® Report Generator™ 软件中。如果文档类型声明引用了 DTD 文件,则声明指定的路径下必须存在有效的 DTD 文件。否则,追加内容会导致 DTD 解析错误。例如,以下声明会导致解析错误:
<!DOCTYPE html SYSTEM "foo.dtd">
如果要追加的内容不包含字符实体引用,则文档类型声明不需要引用 DTD 文件。例如,以下声明适用于不使用特殊字符的内容:
<!DOCTYPE html>
提示
为了避免文档类型声明问题,请从您打算追加到 DOM 报 告的现有 HTML 内容中删除声明。如果内容不包含声明,DOM 会在前面添加一个定义整个 HTML 字符实体集的有效声明。