复合扩展 xs:anyType 是否允许文本内容?

Does a complex extending xs:anyType allow textual content?

我有以下复杂类型:

<xs:complexType name="ValuePropertyType">
    <xs:complexContent>
        <xs:extension base="xs:anyType">
            <xs:attribute name="recordCount" type="xs:positiveInteger"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

是否允许简单的文本内容?喜欢:

<my:values>
2007-04-01T00:00:00.000-06:00,30.4,28.8,155.8,1055.32,55,haze
2007-04-01T00:00:10.000-06:00,30.4,28.8,155.8,1055.4,59,haze
</my:values>

我认为确实如此,但是在 XML 架构规范中找到规范参考相当……困难。

我问也是因为 JAXB XJC 在此处生成以下 属性:

@XmlAnyElement
protected List<Element> any;

只需要元素。我认为它也应该允许文本。

更新

Xerces、Eclipse(无论它在下面使用什么)、Stylus Studio 和 Oxygen 已验证 this example against this schema. Specifically, this is the complex type 有问题:

<xs:complexType name="DataValuePropertyType">
    <xs:annotation>
        <xs:documentation>Use to point or include data values inline</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="xs:anyType">
            <xs:attribute name="recordCount" type="xs:positiveInteger"/>
            <xs:attributeGroup ref="gml:AssociationAttributeGroup"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

具有 complexContent 的复杂类型不允许将文本内容作为子项,除非它被声明为 mixed="true"。如果你将它混合,那么它允许任意文本节点混合在内容模型允许的任何元素中,你不能将文本限制为特定类型(除非你使用 simpleContent,但那不会完全允许子元素)。

正式定义可以在 XML 模式规范的第 1 部分中的 §2.2.1.1 XML Schema 1.0, XML Schema 1.1 and §3.4.7 XML Schema 1.0, XML Schema 1.1 中找到。根据版本不同,写法略有不同,但大同小异

1.1版本更明确anyType是混合内容,所以允许同时包含文字内容和标签。

由于您的 complexContent 是此混合内容的扩展,因此它也是一种混合内容类型。

然而,在 anyType 的基础上提供扩展似乎有点奇怪,因为它已经允许任何内容以及任何属性。在规范中,如果你看§3.4.7,提到推导方法限制

@potame 在我之前已经给出了正确答案。但是自从

finding a normative reference in the XML Schema spec is quite... hard

我想在现场给你这样的参考,而不是规格链接。

因此,如果您的 xs:extensionbase 属性的值为 xs:anyType,则在限制之前,所有内容都可以作为其内容。规范在这方面确实含糊不清,好的参考资料是

[...] anyType which allows any children and/or character data content, and any attributes, as long as it is well-formed XML.

沃尔姆斯利,普里西拉。最终 XML 架构。 Charles F. Goldfarb 最终版 XML 系列。 Prentice Hall: 2012. P 97.

anyType is a generic complex type that allows anything; any attributes, any child elements, any text content.

沃尔姆斯利,普里西拉。最终 XML 架构。 Charles F. Goldfarb 最终版 XML 系列。 Prentice Hall: 2012. P 203.


然后,关于模式设计的注释:将复杂类型定义为 xs:anyType 的扩展会导致松散(即不是很严格)XML 模式文档。在实践中,这意味着根据生成的模式有效的文档集比必要的大得多 - 并且该模式不会对文档中的结构进行细粒度控制。

一个XML 架构像

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:attributeGroup name="AssociationAttributeGroup">
        <xs:attribute name="id" type="xs:ID"/>
    </xs:attributeGroup>

    <xs:element name="item" type="DataValuePropertyType"/>

    <xs:complexType name="DataValuePropertyType">
        <xs:annotation>
            <xs:documentation>Use to point or include data values inline</xs:documentation>
        </xs:annotation>
        <xs:complexContent>
            <xs:extension base="xs:anyType">
                <xs:attribute name="recordCount" type="xs:positiveInteger"/>
                <xs:attributeGroup ref="AssociationAttributeGroup"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

甚至不需要在扩展体中明确定义的属性真正出现在输入文档中,因为它们缺少 use 属性,其默认值为 "optional".

JAXB 处理 anyType 的基本原理可以在 JAXB (JSR-22) 规范的第 6.3.3 节中找到并引用如下:

6.3.3xsd:anyType

xsd:anyType is the root of the type definition hierarchy for a schema. All
complex type definitions in a schema implicitly derive from xsd:anyType.
Given that the JAXB 2.0 architecture does not define a common base class for
all JAXB class bindings of complex type definitions, the only possible binding
property base type binding for xsd:anyType is to java.lang.Object.
This binding enables all possible type and element substitutions for an element
of type xsd:anyType.

CODE EXAMPLE 6-1 Binding of element with type xsd:anyType

<xs:element name="anyContent/> <!--@type defaults to xs:anyType-->
    <xs:complexType name="base">
        <xs:sequence>
            <xs:element ref="anyContent/>
            <xs:element name="anyContentAgain" type="xs:anyType"/>
        </xs:sequence>
    </xs:complexType>

public class Base {
    void setAnyContent(Object obj);
    Object getAnyContent();
    void setAnyContentAgain(Object obj);
    Object getAnyContentAgain();
}

A schema author defines an element to be of type xs:anyType to defer
constraining an element to a particular type to the xml document author.
Through the use of xsi:type attribute or element substitution, an xml
document author provides constraints for an element defined as xs:anyType.
The JAXB unmarshaller is able to unmarshal a schema defined xsd:anyType
element that has been constrained within the xml document to an easy to access
JAXB mapped class. However, when the xml document does not constrain the
xs:anyType element, JAXB unmarshals the unconstrained content to an
element node instance of a supported DOM API.

Type substitution is covered in more detail in Section 6.7.4.1 and 6.7.4.2.
Element substitution is covered in more detail in Section 6.7.5.