2015-06-21 75 views
0

我目前正在使用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)。我发现这种行为令人困惑,因为它使得难以看到真正的问题(错误的值,而不是失败的断言)。

有什么办法来“捕捉”声明中的错误类型,这样的说法在这个例子不失败?

回答

2

有趣。像大多数验证人一样,撒克逊人会尝试通过在单次运行中捕获尽可能多的验证错误来提供帮助。但是这只在错误彼此独立时才有用。对已知无效的值测试断言并不十分有用:但当然,断言是由不知道已经报告了哪些错误的XPath处理器“盲目”执行的。

那撒克逊报告一个“内部错误”这里的事实是一个线索,它不应该以这种方式工作。我会记录一个错误,看看我们能做些什么。

断言是相当奇怪的,因为它们对已经半验证的数据进行操作:这使得有关他们是否是在类型化或无类型值操作相当复杂的规则。在这种情况下,我怀疑你更坚定地编码你的断言可能会有很大的帮助。但是,如果str的值“A”更直接为“if(str ='A')then dec> 0 else true()”,则代码'dec必须为正值。

+0

感谢您的反馈。能够看到撒克逊的一些扩展可以更容易地处理这个问题,这将是非常好的。让我知道我是否可以在这里得到任何帮助。 –