xml dom 有三种访问元素文本内容的方式:
| 属性 | 行为 |
| nodevalue | 按照原始的 xml 源中指定的那样,返回 text、cdata、comment 和 pi 节点上的原始文本内容(包括空白字符)。对于 element 节点和 document 本身,则返回空值。 |
| 数据 | 与 nodevalue 相同 |
| 文本 | 重复连接指定子树中的多个 text 和 cdata 节点并返回组合结果。 |
注意: 空白字符包括新行、tab 和空格。
nodevalue 属性通常返回原始文档中的内容,与文档如何加载和当前 xml:space 范围无关。
文本属性连接指定子树中的所有文本并扩展实体。这与文档如何加载、preservewhitespace 开关的当前状态和当前 xml:space 范围有关,请看如下所示:
preservewhitespace = true when the document is loaded| preservewhitespace=true | preservewhitespace=true | preservewhitespace=false | preservewhitespace=false |
| xml:space=preserve | xml:space=default | xml:space=preserve | xml:space=default |
| 保留 | 保留 | 保留 | 保留并截断 |
preservewhitespace = false when the document is loaded
| preservewhitespace=true | preservewhitespace=true | preservewhitespace=false | preservewhitespace=false |
| xml:space=preserve | xml:space=default | xml:space=preserve | xml:space=default |
| 半保留 | 半保留并截断 | 半保留 | 半保留并截断 |
此处的保留表示和原始 xml 文档中完全相同的原始文本内容,截断意味着前导和尾部空格已经删除,半保留意味着保留了“重要的空白字符”并规范化了“不重要的空白字符”。重要的空白字符是文本内容内部的空白字符。不重要的空白字符是标记之间的空白字符,请看如下所示:
\n \t jane \n \tsmith \n
在本示例中,红色是可以忽略的不重要的空白字符,而绿色是重要的空白字符,因为它是文本内容的一部分,因此有不可忽略的重要含义。所以在本例中,文本属性返回下列结果:
| 状态 | 返回值 |
| 保留 | "\n\t jane\n\tsmith \n" |
| 保留并截断 | "jane\n\tsmith" |
| 半保留 | " jane smith " |
| 半保留并截断 | "jane smith" |