简体   繁体   English

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

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

Lets take a look at my test .xsd: 让我们看看我的测试.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... -->

Well here is everything fine. 好了,一切都很好。 There is just one problem: my "child" element dont got a type! 只有一个问题:我的“子”元素没有类型! I dont know how to give the element a type. 我不知道如何给元素一个类型。 I tried with: 我尝试过:

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

or with 或搭配

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

But it wont work. 但它不会工作。 There is always an error message: "Element 'child' cannot have both a type attribute and a simpleType/complexType type child[xml]]]" 总是出现错误消息:“元素'child'不能同时具有type属性和simpleType / complexType type child [xml]]]”

How can i fix this problem? 我该如何解决这个问题? I mean without a type the validator wont allow an xml like this: 我的意思是,没有类型的验证器将不允许这样的xml:

Hello World 你好,世界

just an empty child is allowed with one attribute 一个属性只允许一个空孩子

Somebody any idea? 有人知道吗? Thank you! 谢谢!

as the message says - you can't have both type reference and inline definition in one element. 就像消息说的那样-您不能在一个元素中同时拥有类型引用和内联定义。 You have to either define a "stand alone type" and reference it with type attribute or use inline definition. 您必须定义“独立类型”并使用type属性引用它,或使用内联定义。 An example follows: 下面是一个示例:

<!-- 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" />

Also you seem to reference custom type (myType2) in xsd namespace which is wrong. 另外,您似乎在xsd命名空间中引用了自定义类型(myType2),这是错误的。 Your types when declaring won't become part of xsd namespace; 您在声明时的类型将不会成为xsd名称空间的一部分; they are in targetNamespace of current shema (thus you reference them w/o any prefix). 它们在当前shema的targetNamespace中(因此您可以在不带任何前缀的情况下引用它们)。 One the other hand I use xsd:string because it's a type defined in shema's native namespace (xsd in your example). 另一方面,我使用xsd:string,因为它是在shema的本机名称空间(在您的示例中为xsd)中定义的类型。

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

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