首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >anyType元素的模式验证

anyType元素的模式验证
EN

Stack Overflow用户
提问于 2014-05-02 07:45:31
回答 1查看 1.7K关注 0票数 1

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

代码语言:javascript
复制
<?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中使用了"Create Schema“菜单选项来生成XSD文件,但是编辑器仍然为<a:anyType元素显示了这个错误:

代码语言:javascript
复制
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文件。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2014-05-02 09:21:18

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

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

代码语言:javascript
复制
<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):

代码语言:javascript
复制
<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模式中的任何超类型名称):

代码语言:javascript
复制
<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派生的,所以它们可以用作<anyType>的类型。下面的实例将在此场景中进行验证:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23418565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档