Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

xml - Working with Multiple XSD's

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

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.

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

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.

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.

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 ?

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>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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).

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"

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...