【发布时间】: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