search
HomeBackend DevelopmentXML/RSS TutorialSpecific example code sharing of XML and Schema namespaces

The above describes XML and SchemaSome related contents of the namespace are introduced in detail. Let’s learn more about it through examples:

The following example is an XML Schema file named "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>

The XML document below and the one given above The XML Schema is associated with it, named "note.xml", and the following discussion will focus on these two documents

. #

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

           

This fragment: xmlns:xsd="
www.w3.org/2001/XMLSchema", indicating that this schema is used The elements and datatypes come from the "www.w3.org/2001/XMLSchema" namespace (it also indicates that they come from "www.w3.org". /2001/XMLSchema"Namespace elements and data types must be used with "xsd: " prefix. As a namespace identifier (in a declaration as a prefix to an element or attribute), you can also use it without xsd or xsi. This xmlns attribute contains basic XML schema elements, such as element, attribute, complexType, group, simpleType, etc. ## For any XML Schema definition document (XSD), there is a top-level schema (XSD) element. The definition must include this namespace:

www.w3.org/2001/XMLSchema

. That is, this namespace is the standard namespace defined by the XML Schema specification - all XML Schema elements must belong to this namespace ##. # This fragment: targetNamespace="www.w3schools.com" indicates this schema The elements defined by (note, to, from, heading, body) come from the "www.w3schools.com
" namespace. The targetNamespace attribute represents the URI of the namespace corresponding to the schema. That is to say, the namespace must be declared in other documents (including its own document) that reference the Schema, and its URI should be the attribute value of targetNamespace. For example, here we need to use the extended data type defined by note.xsd (note, to, from, heading, body), so the namespace xmlns="www.w3schools.com" is also declared. And that namespace is the default namespace (no prefix). The targetNamespace attribute declares an XML namespace for all new types explicitly created in the schema. Let’s look at what the beginning of the XML document note.xml specified by this schema will look like:

<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的命名空间的内容应该就明了了。



The above is the detailed content of Specific example code sharing of XML and Schema namespaces. For more information, please follow other related articles on the PHP Chinese website!

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
XML's Advantages in RSS: A Technical Deep DiveXML's Advantages in RSS: A Technical Deep DiveApr 23, 2025 am 12:02 AM

XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.

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.

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version