XSD:限制一个复杂型中的简单元素,以供空元素



这是空元素的定义:

<xs:complexType name="processingHook">
    <xs:complexContent>
        <xs:restriction base="xs:anyType">
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>
<xs:element name="callMyApp" type="processingHook" />

和xml-Document:

<callMyApp
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:noNamespaceSchemaLocation='note.xsd'></callMyApp>

验证成功,但是当我用xs:simpleContent替换xs:complexContent时,我会发现一个错误:

src-ct.2.2:类型的复杂类型定义表示错误 " processinghook"。当复杂型带有简单型限制A时 具有混合含量和空粒子的复杂类型,然后必须 成为限制子女的<simpleType>

我是XSD的新手,所以我不了解错误的原因。

在这里,我找到了元素定义,该定义允许在 complexType中限制 simpleContent(但不能用于空元素(:

<xs:element name="title">
    <xs:complexType>
        <xs:simpleContent>
            <xs:restriction base="tokenWithLangAndNote">
                <xs:maxLength value="255"/>
                <xs:attribute name="lang" type="xs:language"/>
                <xs:attribute name="note" use="prohibited"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

那么,为什么它不适用于空元素?

如果我尝试此架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="processingHook">
        <xs:simpleContent>
            <xs:restriction base="xs:anyType">
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:element name="callMyApp" type="processingHook" />
</xs:schema>

我从氧气中获取此错误消息(大概是直接来自Xerces(:

复杂的类型定义表示" processinghook"类型的错误。当一个具有简单状态的复杂型限制具有混合含量和空粒子的复杂类型时,<restriction>的孩子中必须有一个<simpleType>

和此(简单得多的(错误消息9.9:

test.xsd#6的简单类型的基本类型不是简单的类型

这里的撒克逊错误消息可能过于简化;但是,让我们看看规则怎么说:

在XML模式1.0第1部分1的第343页中,我们有:

架构表示约束:复杂类型定义表示ok

    ...all of the following must be true:
    2 If the <simpleContent> alternative is chosen, all of the following must be true:
    2.1 The type definition ·resolved· to by the ·actual value· of the base
 [attribute] must be one of the following:
    2.1.1 ...
    2.1.2 only if the <restriction> alternative is also chosen, a 
complex type definition whose {content type} is mixed and a particle 
which is ·emptiable·, as defined in Particle Emptiable (§3.9.6);
    2.1.3 ...
    2.2 If clause 2.1.2 above is satisfied, then there must be a 
<simpleType> among the [children] of <restriction>.

因此,Xerces错误消息(通常是这种情况(是从规格中抬起的,并引用了包含相关规则的第(§2.2(条款。

§2.1.2说,一个可容纳且具有品种="混合"的复杂类型允许<p>12.3</p>之类的东西,因此应将xs:decimal的简单内容(CTSC(的复杂类型视为有效的限制,因为每个CTSC的实例也是它限制的复杂类型的有效实例。但是§2.2本质上说的是,当您定义CTSC时,必须定义简单内容的类型。即使您想允许简单的内容成为任何字符串,也必须这样说。

我怀疑这样做的原因之一是xs:restriction通常定义一个或多个约束方面(例如minInclusivepattern(,而约束方面的含义取决于您限制的简单类型。

相关内容

最新更新