【问题标题】:Applying XML schema on XML document在 XML 文档上应用 XML 模式
【发布时间】:2014-03-12 21:16:18
【问题描述】:

如果您在一个 XML 文档中有两个同名的标签,您是否必须为每个标签使用两个不同的模式,或者您可以使用 1 个模式,它会有效吗?下面是一个使用 2 个架构的示例,但我只使用了 1 个架构,哪一个是正确的?

使用 2 模式的 XML 文档

    <?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(有 2 个同名标签,称为 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 架构:

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

【问题讨论】:

    标签: xml xml-parsing


    【解决方案1】:

    您可以在第三个架构中定义项目,然后将其包含在其他架构中。

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

    【讨论】:

      【解决方案2】:

      第一个 xml 使用两个不同的模式,因此使用两个不同的名称空间。 是的,你可以让 xml 从不同的模式中派生出来

      第二个xml有两个元素,没有错。可重复元素是您如何表示对象列表或类似的东西在这种情况下是项目列表。

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

      【讨论】:

      • 是的,所以在这种情况下我不必定义两个模式?我可以直接使用一个吗?
      猜你喜欢
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多