使用多个 XSD [英] Working with Multiple XSD's

查看:31
本文介绍了使用多个 XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xsd,它有 3 个对另一个 xsd 的导入,8 个复杂类型和 3 个简单类型.

I have an xsd which has 3 imports to another xsd, 8 complex types and 3 simple types.

现在我只需要处理 1 个 compex 类型,它又继承了许多其他 XSD - 主要是当前 xsd 的 3 个导入.

Now of this I have to work on only 1 compex type, which in turn inherits many other XSD - mainly the 3 imports which current xsd's has.

我将仅根据我提到的 1 种复杂类型获取包含数据的 XML 文件.

I will be getting XML file containing data only according to 1 complex type only, which I have mentioned.

现在我正在尝试根据单个复杂类型生成包含数据的示例 XML 文件并尝试对其进行验证,但是当我尝试使用 XMLSpy 或 OxygenXML 这样做时,它说根节点未定义.

Now am trying to generate sample XML file which contains data according to single complex type and am trying to validate it, but when I try to do so using XMLSpy or OxygenXML it says that root node is not defined.

现在,当我尝试定义根节点时,它不允许我这样做,如果我尝试仅为这种复杂类型创建另一个 xsd,那么它也会给我一些错误,如继承的 xsd,这种复杂类型指向初始 xsd 在新的 xsd 中不起作用,我尝试继承 3 个 xsd,其中复杂类型指向新 xsd 中的初始 xsd,但仍然无法正常工作.

Now, when I try to define root nodes it does not allow me to do so and if I try to create another xsd just for this complex type than also it gives me some errors as inherited xsd which this complex type points to in initial xsd is not working in new one, I tried to inherit 3 xsd's to which complex type point to in initial xsd in new xsd but still it is not working.

我的另一个问题是 - 与完整的 XSD 相比,我们能否针对 XSD 的某些部分验证 XML 文件,因为我得到的 XML 是根据 XSD 中的 1 个复杂元素类型?

Also my another question is - Can we validate XML file against some part of XSD as compared to complete XSD because XML what am getting is according to 1 complex element type in XSD ?

初始 XSD 的格式:

Format of Initial XSD:

 <?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schema location targetNamespace=targetnamespace elementFormDefault="qualified" attributeFormDefault="unqualified" version="0.652">
    <xs:import namespace=first xsd which is imported>
    <xs:import namespace=second xsd which is imported>
    <xs:import namespace=third xsd which is imported>
    <xs:complexType name="firstcomplextype" abstract="false">
        <xs:sequence>
            <xs:element name="some value" type="xs:string" minOccurs="0"/>
            <xs:element name="some value" type="some value"/>
            <xs:element name="some value" type="xs:string" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="some value" type="xs:int" use="required"/>
        <xs:attribute name="some value" type="xs:value" use="required"/>
        <xs:attribute name="some value" type="xs:int" use="required"/>
    </xs:complexType>
    <xs:complexType name="second complex type" abstract="false">
        <xs:sequence>
            <xs:element name="some value" type="xs:some value" minOccurs="0"/>
            <xs:element name="some value" type="xs:some value" minOccurs="0"/>
            <xs:element name="some value" type="some value"/>
            <xs:element name="some value" type="some value"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="third complex type**I need to work with only this complex type and xml file will contain data according to this complex type only and I need to validate incoming XML against only this complex type**" abstract="false">
        <xs:sequence>
            <xs:element name="some value" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="some value" type="some value" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="some value" type="some value"/>
            <xs:element name="some value" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="some value" type="some value" minOccurs="0" maxOccurs="unbounded"/>---**Here this particular element points to another XSD, one of the imported XSD's**
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:sequence minOccurs="0">
                <xs:element name="some value" type="xs:some value" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element name="some value" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="some value" type="some value" minOccurs="0" maxOccurs="unbounded"/>---**Here this particular element points to another XSD, one of the imported XSD's**
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:sequence minOccurs="0">
                <xs:element name="some value" type="xs:some value" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element name="some value" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="some value" type="some value" minOccurs="0" maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="4th complex type" abstract="false">
        <xs:sequence>
            <xs:element name="elements">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="some value" type="some value" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

并且 xsd 继续,因为它有许多更复杂的类型和简单的类型.任何指导将不胜感激.

and the xsd continues as it has many more complex types and simple types. Any guidance would be highly appreciated.

推荐答案

您发布的架构文件仅显示 complexType 定义.要验证 XML 文档,您应该在与文档的根元素匹配的顶层有一个 element 声明.如果您的架构(或导入的架构之一)有这样的架构,请发布该部分以及您尝试验证的简短 XML 文档.

The schema file you posted shows only complexType definitions. To validate an XML document, you should have an element declaration at the top level that matches the document's root element. If your schema (or one of the imported schemas) has such, please post that part and also a short XML document that you're trying to validate.

针对模式的部分"进行验证没有问题.任何在顶层的 element 声明都可以作为经过验证的 XML 文档的根元素,因为 XML Schema 没有定义根元素的概念(不像 DTD 和 RELAX NG,它们更面向文档).

There is no problem with validating against a "part" of a schema. Any element declaration at the top level can serve as the root element of a validated XML document, as XML Schema has no concept of defining the root element (unlike DTD and RELAX NG, which are more document-oriented).

如果您可以控制您的 XML 文档,您还可以尝试将 xsi:type 属性附加到您的根元素,以指示您希望对其进行验证的 complexType.所以你会添加类似

If you have control over your XML documents, you could also try attaching an xsi:type attribute to your root element to indicate the complexType you wish to validate against. So you would add something like

xsi:type="firstcomplextype" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

在您的 XML 文档的根元素的属性中.

in the attributes of the root element of your XML document.

这篇关于使用多个 XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆