简体   繁体   English

XML两个名称相同的标签

[英]XML two tags with the same name

In my XML document I have two tags with the same name, both are called item, I have now created an XML schema for this document, considering that I have two tags with the same name, is my XML schema accurate? 在我的XML文档中,我有两个名称相同的标签,都称为item。现在,考虑到我有两个名称相同的标签,我已经为该文档创建了XML模式,我的XML模式是否准确?

XML document XML文件

    <?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> <!--First Item Tag-->
            <title>Empire Burlesque</title> 
            <note>Special Edition</note> 
            <quantity>1</quantity> 
            <price>10.90</price> 
        </item> 
        <item> <!--Second Item Tag-->
            <title>Hide your heart</title> 
            <quantity>1</quantity> 
            <price>9.90</price> 
        </item> 

XML Schema XML模式

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 

<xs:element name="shiporder">
    <xs:complexType >
        <xs:sequence>
            <xs:element name="orderperson"/>
            <xs:element name="shipto">
                <xs:complexType>
                    <xs:sequence>
                    `   <xs:element name="name" type="xs:string"/>
                        <xs:element name="address" type="xs:string"/>
                        <xs:element name="city" type="xs:string"/>
                        <xs:element name="country" type="xs:string"/>
                        <xs:element name="item"> <!--Only 1 item tag defined in the schema-->
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="title" type="xs:stri+ng"/>
                                        <xs:element name="note" type="xs:string"/>
                                        <xs:element name="quantity" type="xs:integer"/>
                                        <xs:element name="price" type="xs:decimal"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

To match your XML instance, you should make some changes in your XML Schema. 为了匹配您的XML实例,您应该在XML模式中进行一些更改。

(I'm assuming you wanted to declare item and not items .) (我假设您要声明item而不是items 。)

First, you are defining item as a child element of shipto when I believe (from your XML instance and from the meaning of the tags) it should probably be at the same level: 首先,当我相信(从您的XML实例和标签的含义)它应该应该处于同一级别时,您将item定义为shipto的子元素:

 <xs:element name="shiporder">
    <xs:complexType >
        <xs:sequence>
            <xs:element name="orderperson"/>
            <xs:element name="shipto">
               ...
            </xs:element>
            <xs:element name="item">
               ...
            </xs:element>
        </xs:sequence>
    </xs:complexType>

</xs:element>

To allow more than one item you can declare maxOccurs="unbounded" or define a lower limit: 要允许多个item ,可以声明maxOccurs="unbounded"或定义一个下限:

<xs:element name="item" maxOccurs="unbounded">

You also need to declare the attribute in shiporder . 您还需要在shiporder声明该属性。 It should be done in the complexType definition: 应该在complexType定义中完成:

<xs:element name="shiporder">
    <xs:complexType >
        <xs:sequence>
          ...
        </xs:sequence>
        <xs:attribute name="orderid" type="xs:string" />
    </xs:complexType>
</xs:element>

Finally, you have to decide whether note is optional or not. 最后,您必须确定note是否可选。 If it's optional, then declare 如果是可选的,则声明

<xs:element name="note" type="xs:string" minOccurs="0"/>

since the default is 1 . 因为默认值为1 If it's not optional, then you have to include it in your XML instance otherwise it will not validate. 如果不是可选的,则必须将其包括在XML实例中,否则它将无法验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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