【问题标题】:Beginner XML Schema: element with Attribute + Type初学者 XML Schema:具有属性 + 类型的元素
【发布时间】:2010-07-07 16:16:09
【问题描述】:

让我们看看我的测试.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 类型 child[xml]]]"

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

你好世界

一个属性只允许一个空子

有人知道吗? 谢谢!

【问题讨论】:

    标签: xml validation schema xsd


    【解决方案1】:

    正如消息所说 - 您不能在一个元素中同时拥有类型引用和内联定义。您必须定义“独立类型”并使用类型属性引用它或使用内联定义。下面是一个例子:

    <!-- 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)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多