我知道xmlns
定义了一个命名空间,但我对它在 XSD 文件中的使用有点困惑(这是代码合成提供的示例)。
<?xml version="1.0"?>
<!--
file : examples/cxx/parser/library/library.xsd
copyright : not copyrighted - public domain
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:lib="http://www.codesynthesis.com/library"
targetNamespace="http://www.codesynthesis.com/library">
<xsd:simpleType name="isbn">
<xsd:restriction base="xsd:unsignedInt"/>
</xsd:simpleType>
<xsd:complexType name="title">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="lang" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:simpleType name="genre">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="romance"/>
<xsd:enumeration value="fiction"/>
<xsd:enumeration value="horror"/>
<xsd:enumeration value="history"/>
<xsd:enumeration value="philosophy"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="person">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="born" type="xsd:string"/>
<xsd:element name="died" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="author">
<xsd:complexContent>
<xsd:extension base="lib:person">
<xsd:attribute name="recommends" type="xsd:IDREF"/> <!-- Book -->
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="book">
<xsd:sequence>
<xsd:element name="isbn" type="lib:isbn"/>
<xsd:element name="title" type="lib:title"/>
<xsd:element name="genre" type="lib:genre"/>
<xsd:element name="author" type="lib:author" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="available" type="xsd:boolean" use="required"/>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
<xsd:complexType name="catalog">
<xsd:sequence>
<xsd:element name="book" type="lib:book" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="catalog" type="lib:catalog"/>
</xsd:schema>
为什么定义的新类型是用命名空间前缀而不是前缀引用的person
,两者都在文档中定义?是什么让它们属于但不属于?author
lib
xsd
lib
xsd
其次,它们在定义时被独立引用,但在使用时它们具有命名空间前缀。不应该用命名空间前缀来定义它们吗?
例如,author
在定义时没有 lib 前缀,但它使用lib:person
了命名空间前缀(同样在author
以后使用时,它属于lib
!)。这增加了混乱!
<xsd:complexType name="author">
<xsd:complexContent>
<xsd:extension base="lib:person">
<xsd:attribute name="recommends" type="xsd:IDREF"/> <!-- Book -->
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>