在命名空间中定义和引用 XSD 类型



我知道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>

为什么定义的新类型像personauthor 使用 lib 命名空间前缀引用,但不引用xsd前缀,其中 两者都在文档中定义?是什么让他们属于lib但不属于 xsd

xsd命名空间前缀由下式定义

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

此命名空间是为 XML 架构构造保留的。

lib命名空间前缀

xmlns:lib="http://www.codesynthesis.com/library"

此命名空间是用户定义的(在本例中由 Codesynthesis 为其组件控制)。

其次,当它们被定义时,它们是独立引用的,但它们 使用命名空间前缀时具有命名空间前缀。他们不应该是 也用命名空间前缀定义?

否,对于目标命名空间声明,例如:

targetNamespace="http://www.codesynthesis.com/library"

类型的定义,例如 author(此处没有命名空间前缀),

<xsd:complexType name="author">...</xsd:complexType>

自动位于 http://www.codesynthesis.com/library 命名空间中,但必须通过相应的命名空间前缀 type="lib:author" 引用。

架构标记中的 targetNamespace 属性定义定义当前架构元素的命名空间。 在您的示例中,这是"http://www.codesynthesis.com/library"。文档中定义的所有类型、属性和元素都属于 targetNamespace。为了获得对其中一个的引用,您需要为命名空间定义 xmlns(在您的示例中为 xmlns:lib="http://www.codesynthesis.com/library")。因此,仅当获取对架构中定义的类型的引用时,才必须使用定义的前缀"lib"。顺便说一下,您可以将目标命名空间定义为默认命名空间 (xmlns="http://www.codesynthesis.com/library"),并且目标命名空间不需要前缀。

最新更新