0

我目前正在努力处理 XML Schema 1.1 中的断言。XML Schema 1.1 建议指出,如果在执行期间发生错误,则违反了断言。这种行为似乎是合理的,但在尝试理解评估结果时可能会引起一些混乱。让我解释:

以下示例 XSD 解释定义了两个元素:str 和 dec。元素 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 文件无效,因为元素 str 具有值“X”,这是不允许的。

<?xml version="1.0" encoding="utf-8" ?>
<data>
  <str>X</str>
  <dec>5</dec>
</data>

当我现在使用 Saxon 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 的值非法)报告了两个错误。我发现这种行为非常令人困惑,因为它使我们更难看到真正的问题(错误的值,而不是失败的断言)。

有什么方法可以“捕获”断言中的类型错误,以便在此示例中断言不会失败?

4

1 回答 1

2

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

Saxon 在这里报告“内部错误”这一事实表明它不应该以这种方式工作。我将在此记录一个错误,看看我们能做些什么。

断言相当奇怪,因为它们对已半验证的数据进行操作:这使得关于它们是对类型化值还是非类型化值进行操作的规则相当复杂。在这种情况下,我怀疑更具防御性地对断言进行编码可能会有很大帮助。但是将'dec must be positive if str具有值“A”'更直接地编码为“if (str='A') then dec>0 else true()”可能是有意义的。

于 2015-06-21T18:12:15.040 回答