【问题标题】:Schema validation for anyType elementanyType 元素的架构验证
【发布时间】:2014-05-01 23:45:31
【问题描述】:

我在编辑一些 XML 文件时尝试使用 Visual Studio 的架构验证。这些文件包含供DataContractSerializer 读取的序列化对象。

<?xml version="1.0" encoding="utf-8" ?>
<MyRoot xmlns="http://schemas.datacontract.org/2004/07/MyDomain"
        xmlns:lib="http://schemas.datacontract.org/2004/07/MyLibrary"
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <MyList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <a:anyType i:type="lib:MyObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyProperty>my-property-value</lib:MyProperty>
        </a:anyType>
        <a:anyType i:type="lib:MyOtherObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyOtherProperty>my-other-property-value</lib:MyOtherProperty>
        </a:anyType>
    </MyList>
</MyRoot>

我使用 Visual Studio 中的“创建架构”菜单选项来生成 XSD 文件,但编辑器仍然为 &lt;a:anyType 元素显示此错误:

This is an invalid xsi:type 'http://schemas.datacontract.org/2004/07/MyLibrary:MyObject'

我尝试为“http://schemas.microsoft.com/2003/10/Serialization/Arrays”命名空间编辑 XSD 文件,但到目前为止我还未能消除该错误。这是 Visual Studio 生成的 XSD 文件。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import namespace="http://schemas.datacontract.org/2004/07/MyLibrary" />
    <xs:element name="anyType">
        <xs:complexType>
            <xs:sequence>
                <xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/MyLibrary" ref="q1:MyProperty" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

【问题讨论】:

标签: xml visual-studio serialization xsd datacontractserializer


【解决方案1】:

我在这里做了几个假设,因为您没有提供所有文件来诊断问题。我将包含您可以使用和测试的完整 XSD 文件,并尝试适应您的问题。

我假设你有一个像这样的主模式,它定义了你的实例的默认命名空间中的元素(我称之为DataContract.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns="http://schemas.datacontract.org/2004/07/MyDomain"
    targetNamespace="http://schemas.datacontract.org/2004/07/MyDomain"> 

    <xs:element name="MyRoot">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="MyList"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="MyList">
        <xs:complexType>
            <xs:sequence>
                <xs:any maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

根据您发布的数据,我还假设您的 anyType 元素基于抽象类型,MyObjectMyOtherObject 都源自该抽象类型。我在下面的示例中将其称为AbstractObject (MyLibrary.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns="http://schemas.datacontract.org/2004/07/MyLibrary"
    targetNamespace="http://schemas.datacontract.org/2004/07/MyLibrary"> 

    <xs:complexType name="AbstractObject" abstract="true">
        <xs:sequence>
            <xs:element name="Identifier" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="MyObject">
        <xs:complexContent>
            <xs:extension base="AbstractObject">
                <xs:sequence>
                    <xs:element name="MyProperty" type="xs:string"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="MyOtherObject">
        <xs:complexContent>
            <xs:extension base="AbstractObject">
                <xs:sequence>
                    <xs:element name="MyOtherProperty" type="xs:string"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

要允许通过anyType 在您的实例中使用这些类型,您可以将其声明为具有AbstractObject 类型(或在您的实际MyLibrary 架构中调用的任何超类型):

<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
           attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:q1="http://schemas.datacontract.org/2004/07/MyLibrary">

    <xs:import namespace="http://schemas.datacontract.org/2004/07/MyLibrary" schemaLocation="MyLibrary.xsd"/>
    <xs:element name="anyType" type="q1:AbstractObject" />

</xs:schema>

由于MyObjectMyOtherObject 都派生自AbstractObject,因此它们可以用作&lt;anyType&gt; 的类型。下面的实例将在这种情况下验证:

<MyRoot 
    xmlns="http://schemas.datacontract.org/2004/07/MyDomain"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:lib="http://schemas.datacontract.org/2004/07/MyLibrary"
    i:schemaLocation="http://schemas.datacontract.org/2004/07/MyDomain DataContract.xsd
                      http://schemas.microsoft.com/2003/10/Serialization/Arrays SerializationArrays.xsd">

    <MyList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <a:anyType i:type="lib:MyObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyProperty>my-property-value</lib:MyProperty>
        </a:anyType>
        <a:anyType i:type="lib:MyOtherObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyOtherProperty>my-other-property-value</lib:MyOtherProperty>
        </a:anyType>
    </MyList>
</MyRoot>

【讨论】:

  • 我似乎无法让您的最后一点建议发挥作用。首先它说“'type'属性无效-根据其数据类型'w3.org/2001/XMLSchema:QName',值'q1:MyObject'无效-'q1'是未声明的前缀。”如果我将前缀移动到 xs:element 上,那么即使我确实声明了它,它也会显示“类型'schemas.datacontract.org/2004/07/MyLibrary:MyObject'未声明”。
  • 可能是语法错误(例如,您粘贴的代码中有一些语法错误 - 不匹配的标签)。如果声明了命名空间和前缀,则没有理由发生错误。这可能是由于其他一些问题。我添加了一个可以用作示例的实例。在此处剪切并粘贴所有模式的代码,然后对其进行测试。然后,您至少可以从一个可行的示例开始,然后再适应您的实际问题。
  • 我测试了您的完整示例,并且如您所说,它可以很好地协同工作。谢谢。看起来我在原始问题中过度简化了我的代码。请查看我的更新。
  • 我编辑了答案以匹配您问题中的更改,并对所有文件进行了更改。
  • +1 这可能是我遗漏的部分。稍后我会测试一下。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
  • 2010-10-22
  • 2012-05-29
相关资源
最近更新 更多