search
HomeBackend DevelopmentXML/RSS TutorialCrazy XML study notes (8) ---------Simple type of schema

After some hard study and research, I finally figured out the basic principles of XML schema!

What a painful realization this is!

I read the same knowledge point at least 3 times and finally opened up this acupuncture point!

People who have used DTD should know, Naming conflicts are a major problem in DTD, and the introduction of the concept of namespace in Schema solves this problem very well. Specifically:


1. Declare the namespace

The general form of a namespace declaration is: the first part is a keyword xmlns:, the second part is the prefix of the namespace, the third part is an equal sign, and the fourth part is double quotes , including the namespace identifier URI in the fifth part. It should be noted that the prefix of the namespace cannot be xml, because this string is reserved for special purposes in XML. Example:
xmlns:tns="http://www.whtest.com/" //where tns is the prefix.
You can also declare the namespace implicitly, that is, omit the colon and namespace prefix. Example:
xmlns="http://www.whtest.com/" //Note that there can only be one implicitly declared namespace in a document

2. Namespace in Schema:

(1)Global components in Schema

Global components refer to the direct child nodes of the element xsd:schema, including element declarations , attribute declaration, complex/simple type definition, group definition, attribute group definition

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.php.cn/”
targetNamespace=“http://www.php.cn/“>
   // Schema的目标名称空间用属性targetNamespace在根元素上定义。
   //Schema的全局成分被放在名称空间http://www.php.cn/里。
</span>

(2) in Schema Non-global components Sometimes you want to define non-global components in the target space. You can use the following method.

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.php.cn/”
targetNamespace=“http://www.php.cn/“
elementFormDefault=“qualified“></span>


## The default value of the attribute elementFormDefault is unqualified, which stipulates that only global components are defined in the target namespace. Assign the value of elementFormDefault to qualified so that the target namespace contains non-global element definitions. Likewise, assigning the value of attributeFormDefault to qualified causes the target namespace to contain non-global attribute definitions. As follows:

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.php.cn/”
targetNamespace=“http://www.php.cn/“
attributeFormDefault=“qualified“></span>


## You can also modify the value of the attribute form so that some non-global components are not included in the namespace. As follows:


<xsd:element name=”name” type=”xsd:string” form=”unqualified”/>

## (3)targetNamespace

After a targetNameSpace is defined in the xsd file, the elements, attributes, types, etc. defined internally belong to the targetNameSpace, and these elements are used by itself or external xsd files. , attributes, etc. must be found from the defined targetNameSpace.

     targetNamespace定义了Schema定义的新元素与属性的名称空间。而"http://www.php.cn/"名称空间则定义了element, attribute, complexType, group, simpleType等元素。

     若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。     


   3、XML文档中命名空间

      在XML中,名称空间的使用涉及范畴的概念,范畴即名称空间的覆盖范围,它指的是哪些元素和属性在该名称空间中,哪些不在该名称空间中。名称空间既可以限定整个XML文档,也可以只针对XML文档中的一部分。

   (1).名称空间限定整个XML文档

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”?>
<member_details xmlns=”http://www.php.cn/”>
   <name>Tom</name>
   <age>12</age>
   <sex>male</sex>
</member_details></span>


   (2)名称空间只针对XML文档中的一部分

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”?>
<member_details>
   <name xmlns=”http://www.php.cn/”>Tom</name>
   <age>12</age>
   <sex>male</sex>
</member_details></span>


(3)嵌套的命名空间

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”?>
<member_details xmlns=”http://www.php.cn/”
xmlns:newns=”http://www.php.cn/”>
   <name>Tom</name>
   <age>12</age>
   <newns:sex>male</sex>
</member_details>
//<span style="line-height: 26px;"> 此例中,除了元素sex被定义在新的名称空间中外,其余的元素仍然使用原来的名称空间。</span></span>


 

 

 (4)schemaLocation


    schemaLocation 属性引用具有目标名称空间的 XML 架构文档(.xsd)。该xml文件中用到的所有新创的元素、属性等的.xsd文件都必须在这里声明。

    其中的anyURI是一个架构位置,该架构包含限定的(具有名称空间的架构)架构构造。每一对中的第一个  URI 引用是名称空间名称,第二个则是描述名称空间的架构的位置。

将具有目标名称空间的架构文档与实例文档相关联。可以列出多对  URI 引用,每一对都有不同的名称空间名称部分。
    根据万维网联合会 (W3C) XML 架构建议,XML 实例文档可以同时指定 xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 属性。此外,还可以多次列出同一个命名空间。

    以下示例显示如何使用 xsi:schemaLocation 属性为多个 XML 架构文档提供位置信息。

<span style="font-family: SimSun; font-size: 14px;"><p:Person
   xmlns:p="http://contoso.com/People"
   xmlns:v="http://contoso.com /Vehicles"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
     "http://contoso.com/People
      http://www.php.cn/
      http://www.php.cn/
      http://www.php.cn/
      http://www.php.cn/
      http://www.php.cn/">
   <name>John</name>
   <age>28</age>
   <height>59</height>
   ....
</p:Person></span>

 


      通过上边的分析,我们可以看到,XML和Schema的命名空间标签使用格式是相同的(这也是Schema相对与DTD的优势),但XML和Schema都有各自的独特的属性,这也是由他们不同的功能决定的,Schema主要给XML提供服务,所以会规定好targetNameSpace来声明命名空间的名字,而XML需要使用schema的服务,所以需要SchemaLocation来声明使用的命名空间。

 

 

  上文把XML和Schema的命名空间的一些相关内容进行了详细介绍,下面通过例子来具体了解:

    例一:重点理解名称空间的相关概念。

    下面的例子是一个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 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&#39;t forget me this weekend!</body>
</note>
<?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&#39;t forget me this weekend!</body>
</note>



            此片段:xmlns:xsd="http://www.php.cn/",表明此schema中使用的元素和数据类型来自于"http://www.php.cn/"名称空间(namespace)。它同样指出来自于"http://www.php.cn/"名称空间的元素和数据类型必须使用带"xsd: "前缀。作为名称空间的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。这个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。

     对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含这个名称空间:http://www.php.cn/。即此名称空间是由XML模式规范定义的标准名称空间-所有XML模式元素必须属于该名称空间。
     此片段:targetNamespace="http://www.php.cn/",表明此schema (note, to, from, heading, body)定义的元素来自于"http://www.php.cn/"名称空间。这个targetNamespace属性表示了该schema所对应的名称空间的URI。也就是说在引用该Schema的其它文档(包括自身文档)中要声明名称空间,其URI应该是targetNamespace的属性值。例如在这里因为要用到note.xsd自己定义的扩展数据类型(note, to, from, heading, body),所以也声明了名称空间xmlns="http://www.php.cn/"。而且该名称空间是默认名称空间(没有前缀)。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">
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">

     其中缺省名称空间声明xmlns="http://www.php.cn/"就是和刚刚声明的XML Schema的名称空间相结合来规定该XML文档。(即该文档用到了此名称空间中定义的数据) xmlns:xsi="http://www.php.cn/"  是任何XML实例文档固有的XML模式实例名称空间,它由XML模式规范定义。而xsi:schemaLocation="http://www.php.cn/.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>


<?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是找不到的,因为你定义了一个名称空间"http://www.php.cn/",该复杂类型存在于"http://www.php.cn/"中,因此应该修改代码如下:

<?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>



<?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定义的新元素与属性的名称空间。而"http://www.php.cn/"名称空间则定义了element, attribute, complexType, group, simpleType等元素。


    理解了上面的两个例子,Schema的命名空间的内容应该就明了了。

 

以上就是疯狂XML学习笔记(8)---------schema 的简单类型 的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.