<Series> <A> <id>1</id> </A> <B> <id>1</id> </B> <B> <id>1</id> </B> <B> <id>2</id> </B> </Series>
使用XSD 1.1和XPath 2.0,我想断言名为“B”的元素的最大数量,它们与“A”具有相同的“id”值.具体来说,我想将具有“id”= 1的名称“B”的元素数限制为具体为2次.我不关心有多少名为B的元素与其他“id”值不匹配A的id =“1”(因此可能有一百万< B>< id> 2< / id> < / B> ;,它仍然会验证. 这是我尝试的XML Schema 1.1强制执行此操作,在assert指令中使用XPath 2.0表达式:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Series" type="series"/> <xs:complexType name="series"> <xs:sequence> <xs:element name="A" type="a"/> <xs:element name="B" type="b" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="a"> <xs:sequence> <xs:element name="id" type="xs:string"/> </xs:sequence> <xs:assert test="count(following-sibling::element(B)[id/text() = ./id/text()]) eq 2"/> </xs:complexType> <xs:complexType name="b"> <xs:sequence> <xs:element name="id" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema>
但是我的断言总是因cvc断言错误消息而失败.如果我试图放宽断言只是< xs:assert test =“count(follow-sibling :: element(B))ge 1”/>,它仍然会失败,所以看起来XML Schema 1.1不能处理所有XPath 2.0构造?
通常,有没有办法在XML Schema 1.1中对兄弟进行断言?谢谢!
XML Schema 1.1断言可以处理所有XPath 2.0,但是根据规范( section 3.13.4)1.3 From the “partial” ·post-schema-validation infoset·, a data model instance is constructed as described in [XDM]. The root node of the [XDM] instance is constructed from E; the data model instance contains only that node and nodes constructed from the [attributes], [children], and descendants of E.
Note: It is a consequence of this construction that attempts to refer, in an assertion, to the siblings or ancestors of E, or to any part of the input document outside of E itself, will be unsuccessful. Such attempted references are not in themselves errors, but the data model instance used to evaluate them does not include any representation of any parts of the document outside of E, so they cannot be referred to.
(我的大胆).在评估断言时,您只有以托管断言的元素为根的子树,因此如果要在树的不同部分中断言元素之间的关系,则必须将断言置于其共同的祖先之一上.
我认为它是这样设计的,允许验证解析器在解析期间,在相关元素的末尾评估断言,而不是必须等到整个树已经构建,然后整体评估所有断言.