简体   繁体   中英

Beginner XML Schema: element with Attribute + Type

Lets take a look at my test .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]]]"

How can i fix this problem? I mean without a type the validator wont allow an xml like this:

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. 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. Your types when declaring won't become part of xsd namespace; they are in targetNamespace of current shema (thus you reference them w/o any prefix). One the other hand I use xsd:string because it's a type defined in shema's native namespace (xsd in your example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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