简体   繁体   中英

Applying XML schema on XML document

If you have two tags with the same name in an XML document, do you have to use two different schema's for each tag or can you use 1 schema, would it validate? below is an example where it is using 2 schema's, but I am using only 1 schema, which one is correct?

XML Document Using 2 Schema's

    <?xml version="1.0" encoding="ISO-8859-1"?> 
<receipt xmlns: cu="http://www.mydomain.com/customer" 
 xmlns: pr="http://www.mydomain.com/product">  <!--two schema's being referenced-->

<cu:customer> <!--Using Schema 1-->
<cu:name> Michael Johnson </cu:name> 
<cu:areaCode> BH2 3XY </cu:areaCode> 
</cu:customer> 

<products> 
<pr:product>  <!--Using Schema 2-->
<pr:name>RAM</pr:name> 
<pr:quantity>100</pr:quantity> 
</pr:product> 
</products> 
</receipt>

My XML Document Using 1 Schema (has 2 tags with the same name, called item)

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

<shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> 
    <orderperson>John Smith</orderperson> 
        <shipto> 
            <name>Ola Nordmann</name> 
            <address>Langgt 23</address> 
            <city>4000 Stavanger</city> 
            <country>Norway</country> 
        </shipto> 
        <item> 
            <title>Empire Burlesque</title> 
            <note>Special Edition</note>  
            <quantity>1</quantity> 
            <price>10.90</price> 
        </item> 
        <item> 
            <title>Hide your heart</title> 
            <quantity>1</quantity> 
            <price>9.90</price> 
        </item> 
</shiporder> 

XML Schema:

    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
    <xs:complexType >
        <xs:sequence>
            <xs:element type="xs:string" name="orderperson"/>
            <xs:element name="shipto">
                <xs:complexType>
                  <xs:sequence>
              <xs:element type="xs:string" name="name"/>
              <xs:element type="xs:string" name="address"/>
              <xs:element type="xs:string" name="city"/>
              <xs:element type="xs:string" name="country"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element> 
            <xs:element name="item"  maxOccurs="unbounded" minOccurs="0"> 
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="title" type="xs:string"/>
                        <xs:element name="note" type="xs:string" minOccurs="0"/> <!--optional-->
                        <xs:element name="quantity" type="xs:integer"/>
                        <xs:element name="price" type="xs:decimal"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    <xs:attribute name="orderid" type="xs:int" /> <!--must be required-->
    </xs:complexType>
</xs:element>

You can define item in a third schema, then include it in the other schemas.

See Can I have one XML Schema (XSD) include another XML-Schema? . And http://www.w3schools.com/schema/el_include.asp

the first xml uses two different schemas hence uses two different name spaces. yes you can have xml derive from different schemas

the second xml has two elements and is nothing wrong. repeatable elements is how you represent a list of objects or something similar in this case a list of items.

<item> is defined to be occurring more than once as per the schema.
maxOccurs="unbounded" so it is valid xml as per the schema.

Is that not true?

<xs:element name="item"  maxOccurs="unbounded" minOccurs="0"> 
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="title" type="xs:string"/>
                        <xs:element name="note" type="xs:string" minOccurs="0"/> <!--optional-->
                        <xs:element name="quantity" type="xs:integer"/>
                        <xs:element name="price" type="xs:decimal"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM