繁体   English   中英

初学者XML架构:具有属性+类型的元素

[英]Beginner XML Schema: element with Attribute + Type

让我们看看我的测试.xsd:

    <!-- lot of stuff... -->
<xsd:element name="root">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="target:child"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:element name="child">
    <xsd:complexType>
            <xsd:attribute name="childAttribute" type="myType"/>
    </xsd:complexType>
</xsd:element>
    <!-- lot of stuff... -->

好了,一切都很好。 只有一个问题:我的“子”元素没有类型! 我不知道如何给元素一个类型。 我尝试过:

<xsd:element name="child" type="xsd:myType2">
    <xsd:complexType>
            <xsd:attribute name="childAttribute" type="myType"/>
    </xsd:complexType>
</xsd:element>

或搭配

<xsd:element name="root">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="target:child" type="xsd:myType2"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

但它不会工作。 总是出现错误消息:“元素'child'不能同时具有type属性和simpleType / complexType type child [xml]]]”

我该如何解决这个问题? 我的意思是,没有类型的验证器将不允许这样的xml:

你好,世界

一个属性只允许一个空孩子

有人知道吗? 谢谢!

就像消息说的那样-您不能在一个元素中同时拥有类型引用和内联定义。 您必须定义“独立类型”并使用type属性引用它,或使用内联定义。 下面是一个示例:

<!-- inline definition -->
<xsd:element name="child">
    <xsd:complexType>
            <xsd:attribute name="childAttribute" type="xsd:string"/>
    </xsd:complexType>
</xsd:element>

<!-- typed definiotion -->
<xsd:complexType name="typeForChild">
    <xsd:attribute name="childAttribute" type="xsd:string"/>
</xsd:complexType>

<xsd:element name="child" type="typeForChild" />

另外,您似乎在xsd命名空间中引用了自定义类型(myType2),这是错误的。 您在声明时的类型将不会成为xsd名称空间的一部分; 它们在当前shema的targetNamespace中(因此您可以在不带任何前缀的情况下引用它们)。 另一方面,我使用xsd:string,因为它是在shema的本机名称空间(在您的示例中为xsd)中定义的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM