XML 架构:根元素 [英] XML Schema: root element

查看:48
本文介绍了XML 架构:根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的帖子询问如何指示一个元素是根元素XML 架构:

The following post asks how to indicate that an element is the root element in an XML schema:

是是否可以使用 Schema 在 XML 文档中定义根元素?

我已经按照 w3schools 教程学习了 XML 架构,但仍有一些不清楚的地方.考虑来自 https://www.w3schools.com/xml/schema_example.asp(为方便起见,转载如下).此代码如何表示 是根元素吗?这个例子不是说所有元素是否作为根元素有效?

I have followed the w3schools tutorial on XML Schema but something is still not clear. Consider example schema 2 from https://www.w3schools.com/xml/schema_example.asp (reproduced below for convenience). How does this code indicate that <shiporder> is the root element? Isn't the example saying that all elements are valid as root elements?

----------- 实例----------------------------------

------------------ instance ----------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>

<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</xample saying that all elements are valid as root elements?quantity>
    <price>9.90</price>
  </item>
</shiporder> 

--------------- 架构 ----------------

----------------------- schema ------------------------

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<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="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->
<xs:element name="shipto">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="name"/>
      <xs:element ref="address"/>
      <xs:element ref="city"/>
      <xs:element ref="country"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="item">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="title"/>
      <xs:element ref="note" minOccurs="0"/>
      <xs:element ref="quantity"/>
      <xs:element ref="price"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="orderperson"/>
      <xs:element ref="shipto"/>
      <xs:element ref="item" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute ref="orderid" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>


在我看来,XML Schema 应该做两件事:


From my point of view an XML Schema should do two things:

  1. 定义每个节点内可能发生的事情
  2. 定义每个节点可以放置的位置

而且这个例子似乎在 #2 处失败了.有什么建议吗?

And it seems the example fails at #2. Any Suggestions?

推荐答案

据我所知,任何全局定义的元素都可以用作根元素,而 XML Schema 没有指定根元素应该是什么.

As far as I know, any globally defined element can be used as root element, and XML Schema does not have a notion for specifying what the root element is supposed to be.

然而,您可以通过很好地设计 XML 模式来解决这个问题,以便只有一个全局定义的元素 - 只有这个元素作为根元素有效.

You can however work around this by designing your XML Schema well, so that there is only one globally defined element - then only this element is valid as root element.

可以在 W3Schools(标题Using Named类型)这个例子只有一个全局定义的元素,因此只有一个可能的根元素.

An example of this can be found at W3Schools (heading Using Named Types) This example only has one globally defined element, and thus only one possible root element.

这篇关于XML 架构:根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆