【问题标题】:XML schema validation of literal versus excaped new line?XML 模式验证文字与扩展新行?
【发布时间】:2017-01-06 10:30:29
【问题描述】:

我有一个有效的 xsd 拒绝验证包含无效空格的 XML 实例(有关详细信息,请参见下文,但包括回车符 (#xD)、换行符 (#xA) 或制表符 (#x9) 字符,否开始或结束空格 (#x20) 字符,或两个或多个相邻空格字符的序列)。

XSD 示例:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" 
targetNamespace="http://www.example.com"
xmlns:test="http://www.example.com">

<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>

<xs:element name="test-token" type="test:Tokenized500Type"></xs:element>

<xs:simpleType name="Tokenized500Type">
    <xs:annotation>
        <xs:documentation>An element of this type has minimum length of one character, a max of 500, and may not
            contain any of: the carriage return (#xD), line feed (#xA) nor tab (#x9) characters, shall
            not begin or end with a space (#x20) character, or a sequence of two or more adjacent space
            characters.</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
        <xs:maxLength value="500"/>
        <xs:minLength value="1"/>
        <xs:pattern value="\S+( \S+)*"/>

    </xs:restriction>
</xs:simpleType>

我用上面的文字空白字符对此进行了测试。

如果 XML 实例在相关元素内容中包含转义空格怎么办?这会导致验证错误吗?

这是一个带有转义版本的示例:

<?xml version="1.0" encoding="UTF-8"?>
<test-token xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com">&#13;</test-token>

另见:

【问题讨论】:

  • 您能否提供 XSD 和示例 XML,并说明您遇到/预期的错误是什么
  • 谢谢,我已经添加了。因为看起来 就像文字 nl 一样使此架构的实例无效,除非您愿意,否则我会回答我的问题。

标签: validation xsd escaping whitespace tokenize


【解决方案1】:

正则表达式应该对扩展(非转义)字符串进行操作,因此新行文字和 之间应该没有区别

\S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029]).

还要注意,XSD 中使用的正则表达式是 Unicode 正则表达式(这与更标准的 posix 正则表达式不同,更糟糕的是,一些解析器使用任何正则表达式解析器通过敲击发生(xsd 验证在 .net 中使用它的内部正则表达式解析器 - 这不是“Unicode 正则表达式”)。

注意:这里定义的·正则表达式·语言不会尝试 为 UCS 上的“正则表达式”提供通用解决方案 字符序列。特别是,它不容易提供 匹配基本字符序列和组合标记。这 语言的目标是支持“1 级”功能,如 [Unicode 正则表达式指南]。希望未来 本规范的版本将提供对“2 级”的支持 功能。

【讨论】:

  • 感谢@Sprotty 提供有用的实现提示!
猜你喜欢
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 2010-11-22
  • 2019-09-09
  • 1970-01-01
相关资源
最近更新 更多