例一:重点明白称号空间的相干观点。
下面的例子是一个XML Schema文件,名为"note.xsd"
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xs:string"/> <xsd:element name="from" type="xs:string"/> <xsd:element name="heading" type="xs:string"/> <xsd:element name="body" type="xs:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
下面的XML文档和上文给出的XML Schema相干联,名为"note.xml"。而且下文的议论将缭绕这两个文档睁开。
<?xml version="1.0"?> <note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
此片断:xmlns:xsd="www.w3.org/2001/XMLSchema",表明此schema中运用的元素和数据范例来自于"www.w3.org/2001/XMLSchema"称号空间(namespace)。它一样指出来自于"www.w3.org/2001/XMLSchema"称号空间的元素和数据范例必需运用带"xsd: "前缀。作为称号空间的标识符(在声明中作为元素或属性的前缀),你也能够不运用xsd或xsi。这个 xmlns属性包含了基础的XML schema元素,比方element, attribute, complexType, group, simpleType等。
关于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必需包含这个称号空间:www.w3.org/2001/XMLSchema。即此称号空间是由XML形式范例定义的规范称号空间-一切XML形式元素必需属于该称号空间。
此片断:targetNamespace="www.w3schools.com",表明此schema (note, to, from, heading, body)定义的元素来自于"www.w3schools.com"称号空间。这个targetNamespace属性示意了该schema所对应的称号空间的URI。也就是说在援用该Schema的别的文档(包含本身文档)中要声明称号空间,其URI应当是targetNamespace的属性值。比方在这里由于要用到note.xsd本身定义的扩大数据范例(note, to, from, heading, body),所以也声清楚明了称号空间xmlns="www.w3schools.com"。而且该称号空间是默许称号空间(没有前缀)。targetNamespace属性为在形式中显式建立的一切新范例均声清楚明了XML称号空间。
我们再来看由该schema划定的XML文档note.xml的开首将是什么模样:
<note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd">
个中缺省称号空间声明xmlns="www.w3schools.com"就是和方才声明的XML Schema的称号空间相结合来划定该XML文档。(即该文档用到了此称号空间中定义的数据) xmlns:xsi="www.w3.org/2001/XMLSchema-instance" 是任何XML实例文档固有的XML形式实例称号空间,它由XML形式范例定义。而xsi:schemaLocation="www.w3schools.com note.xsd"则划定了该称号空间所对应的schema的位置,即在雷同途径的note.xsd文件。
例二:重点明白Schema文档运用本身定义范例
xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,范例等都属于该targetNameSpace,其本身或外部xsd文件运用这些元素,属性等都必需从定义的targetNameSpace中找。修正一下note.xsd,去除默许称号空间的声明,并增加一个庞杂范例:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified"> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xs:string"/> <xsd:element name="from" type="xs:string"/> <xsd:element name="heading" type="xs:string"/> <xsd:element name="body" type="xs:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Student" type="stu"/> <xsd:complexType name="stu"> <xsd:sequence> <xsd:element name="Name" type="xs:string"/> <xsd:element name="Class" type="xs:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
上述代码中,庞杂范例stu是找不到的,由于你定义了一个称号空间"www.w3schools.com",该庞杂范例存在于"www.w3schools.com"中,因而应当修正代码以下:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns:student="http://www.w3schools.com" elementFormDefault="qualified"> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xs:string"/> <xsd:element name="from" type="xs:string"/> <xsd:element name="heading" type="xs:string"/> <xsd:element name="body" type="xs:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Student" type="student:stu"/> <xsd:complexType name="stu"> <xsd:sequence> <xsd:element name="Name" type="xs:string"/> <xsd:element name="Class" type="xs:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
若本身并不运用重用组件,仅供外部运用的话,则只定义targetNameSpace就能够,不必指定别号。
经由过程上面的例子,我们能够很深入的明白targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的称号空间。而"www.w3.org/2001/XMLSchema"称号空间则定义了element, attribute, complexType, group, simpleType等元素。
明白了上面的两个例子,Schema的定名空间的内容应当就清楚明了了。
以上就是XML和Schema定名空间的详细实例代码分享的细致内容,更多请关注ki4网别的相干文章!