【问题标题】:XML Schema inheritanceXML 模式继承
【发布时间】:2014-05-17 16:50:11
【问题描述】:

这是我编写的一个简化的 XML Schema 示例:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://pius.lt" xmlns="http://pius.lt" elementFormDefault="qualified">
    <xs:complexType name="person">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="email" type="xs:string"/>
            <xs:element name="sex" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="user">
        <xs:complexContent>
            <xs:extension base="person">
                <xs:sequence>
                    <xs:element name="password" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="id" type="xs:integer" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="admin">
        <xs:complexContent>
            <xs:restriction base="user">
                <xs:sequence>
                    <xs:element name="email" type="xs:string" fixed="admin@pius.lt"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

我得到的错误是:

Not valid.
Error - Line 22, 31: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 31; rcase-Recurse.2: There is not a complete functional mapping between the particles.
Error - Line 22, 31: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 31; derivation-ok-restriction.5.4.2: Error for type 'admin'.  The particle of the type is not a valid restriction of the particle of the base.

我错过了什么?

附:是否可以对该“电子邮件”字段应用模式限制?类似于.+@example.com

【问题讨论】:

    标签: xml schema restriction


    【解决方案1】:

    当您通过限制派生时,您需要在子类型中提供 完整 内容模型 - 在子类型中您未说允许的任何内容都被假定为禁止。

    因此,admin 类型的架构编写方式只允许包含email 元素,但不能包含其他任何内容,但这不是user 的有效限制,因为user 类型的元素必须有一个 name,然后是一个 email,然后是一个 sex,然后是一个 password 元素作为子元素。

    您违反了http://www.w3.org/TR/xmlschema-1/#rcase-Recurse 下的规则 2.2:

    B 的 {particles} 中没有被 R 的 {particles} 中的任何粒子映射到的所有粒子都是·emptiable·,如 Particle Emptiable (§3.9.6) 所定义。

    即您想在受限类型中省略的任何内容都必须在父类型中是可选的 - 受限类型的任何实例也必须是受限制的父类型的有效实例。

    如果您确实希望admin 类型包含 email 元素,那么您必须将personuser 的其他元素标记为minOccurs="0" ,但我怀疑你真的想要

    <xs:complexType name="admin">
        <xs:complexContent>
            <xs:restriction base="user">
                <xs:sequence>
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="email" type="xs:string" fixed="admin@pius.lt"/>
                    <xs:element name="sex" type="xs:string"/>
                    <xs:element name="password" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="id" type="xs:integer" use="required"/>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-01-19
      • 2015-02-09
      • 2021-11-15
      • 1970-01-01
      • 2011-05-09
      相关资源
      最近更新 更多