我看的XML教材对Schema的一点疑惑
这是根据下边Schema定义的一个元素
<?xml version="1.0" ?>
<computer:laptop xmlns:computer="http://www.deitel.com/computer" manufacturer="IBM">
<processor model="Centrino">Intel</processor>
<monitor>17</monitor>
<CPUSpeed>2.4</CPUSpeed>
<RAM>256</RAM>
</computer:laptop>
为什么<all>元素没见踪迹,还有命名空间computer:只在一个复杂类型的最外边引用一下,里面的元素就不用了?
<?xml version = "1.0"?>
<!-- Fig. 19.14: computer.xsd -->
<!-- W3C XML Schema document -->
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
xmlns:computer = "http://www.deitel.com/computer"
targetNamespace = "http://www.deitel.com/computer">
<simpleType name = "gigahertz">
<restriction base = "decimal">
<minInclusive value = "2.1"/>
</restriction>
</simpleType>
<complexType name = "CPU">
<simpleContent>
<extension base = "string">
<attribute name = "model" type = "string"/>
</extension>
</simpleContent>
</complexType>
<complexType name = "portable">
<all>
<element name = "processor" type = "computer:CPU"/>
<element name = "monitor" type = "int"/>
<element name = "CPUSpeed" type = "computer:gigahertz"/>
<element name = "RAM" type = "int"/>
</all>
<attribute name = "manufacturer" type = "string"/>
</complexType>
<element name = "laptop" type = "computer:portable"/>
</schema>
[解决办法]
其实你提的两个问题,主要是对命名空间有疑惑
主要看这行
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
如果修改成
<schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
<xsd:all>就比较容易理解了,它表示的意思,这个portable复合类型中,必须包括所有的4个类型,并且为唯一。如果使用<xsd:choice>意思就不同了,可以任意选择这4个类型组合到一起
而命名空间computer的引用,其实在这里有命名空间默认引用的,很容易理解