簡體   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