我目前正在使用XML Schema 1.1中的断言挣扎。 XML Schema 1.1建议states如果在执行过程中发生错误,则断言违反了断言。这种行为似乎是合理的,但在试图了解评估结果时可能会造成一些混淆。让我来解释:XML Schema 1.1声明:如何捕获动态类型错误?
下面的例子说明了XSD定义了两个元素:STR及(分解)。元素str必须具有值“A”或“B”。元素dec必须是一个数字。另外,如果str的值为“A”,则dec必须为正数。我试图通过使用断言定义这最后一个属性:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="str">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="A"/>
<xs:enumeration value="B"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="dec" type="xs:decimal"/>
</xs:sequence>
<xs:assert test="str != 'A' or dec gt 0"/>
</xs:complexType>
</xs:element>
</xs:schema>
下面的XML文件是无效的,因为该元素具有海峡值“X”,这是不允许的。
<?xml version="1.0" encoding="utf-8" ?>
<data>
<str>X</str>
<dec>5</dec>
</data>
当我现在使用验证撒克逊9.6.0.6我得到以下输出XML文件:
Validation error on line 3 column 16 of test.xml:
XSD: The content "X" of element <str> does not match the required simple type. Value "X"
contravenes the enumeration facet "A, B" of the type of element str
Validating /data[1]/str[1]
See http://www.w3.org/TR/xmlschema11-2/#cvc-datatype-valid clause 1
Warning: on line 1
Internal error: value doesn't match its type annotation. Value "X" contravenes the
enumeration facet "A, B" of the type of element str
Validation error at data on line 5 column 8 of test.xml:
XSD: Element data does not satisfy assertion str != 'A' or dec gt 0
Validating /data[1]
正如你所看到的,我得到的报告只是一个问题(非法值两个错误元素str)。我发现这种行为令人困惑,因为它使得难以看到真正的问题(错误的值,而不是失败的断言)。
有什么办法来“捕捉”声明中的错误类型,这样的说法在这个例子不失败?
感谢您的反馈。能够看到撒克逊的一些扩展可以更容易地处理这个问题,这将是非常好的。让我知道我是否可以在这里得到任何帮助。 –