XML Schema union ignore whiteSpace property
根据 XML Schema 规范的空格:
For all datatypes ·derived· by ·union· whiteSpace does not apply directly; however, the normalization behavior of ·union· types is controlled by the value of whiteSpace on that one of the ·memberTypes· against which the ·union· is successfully validated.
和
for string the value of whiteSpace is preserve
基于此,下面的示例应该是无效的,因为应该保留字符串的空格,并且不应该满足模式。然而它是有效的。所以,我的问题是?为什么这个 XML 对这个模式有效?
XML(注意空格):
1 2 | <?xml version="1.0" encoding="UTF-8" ?> <elem> Hello world</elem> |
XML Schema(注意模式限制):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="myUnion"> <xs:union memberTypes="xs:string"> </xs:union> </xs:simpleType> <xs:element name="elem"> <xs:simpleType> <xs:restriction base="myUnion"> <xs:pattern value="Hello world" /> </xs:restriction> </xs:simpleType> </xs:element> </xs:schema> |
编辑:Xerces 说它有效,Saxon 说它无效。这似乎是一个 Xerces 错误。
但是,如果我们这样定义联合:
1 2 3 4 5 6 7 8 9 | <xs:simpleType name="myUnion"> <xs:union> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType> |
再次,Xerces 说它有效,Saxon 说它无效。但这一次它似乎是一个撒克逊错误(因为应该折叠空格并且应该满足模式)。 ?你怎么看?
谁说它有效?
撒克逊人说:
1 2 3 4 5 6 7 8 9 10 | Processing file:/Users/mike/Desktop/temp/test.xml Validation error on line 2 of test.xml: XSD: The content" Hello world" of element <elem> does not match the required simple type. Value" Hello world" contravenes the pattern facet"Hello world" of the type of element elem Validating /elem[1] See http://www.w3.org/TR/xmlschema11-2/#cvc-datatype-valid clause 1 Validation error on line 2 column 37 of test.xml: XSD: One or more validation errors were reported Validation unsuccessful |
而撒克逊人通常会做对 ;-)