About the namespace of the schema

Source: Internet
Author: User
Tags data structures string xmlns
Name space

This is how the basic XML Schema is used, and we're going to introduce another core part of the XML schema that is also the most critical part of the schema, the problem with the namespace of the schema (namespace). In the previous introduction to XML schemas, the introduction of namespaces (namespace) was deliberately omitted in order to focus the reader on the syntactic understanding of XML schemas.

First, let's start with a simple example, and see the following code:

<xsd:schema targetnamespace= ' Http://www.SampleStore.com/Account '
Xmlns:xsd= ' Http://www.w3.org/2000/08/xmlSchema '
Xmlns:acc= ' Http://www.SampleStore.com/Account '

<xsd:element name= ' order number ' type= ' Xsd:positiveinteger '/>
<xsd:element name= ' Item number ' type= ' ACC: Commodity number type '/>
<xsd:simpletype name= ' Commodity number type ' base= ' xsd:string '
<xsd:pattern value= ' [a-z]{1}d{6} '/>
<!-the line above indicates that the first character of the product code should be the letter, followed by six Arabic numerals-->
</xsd:simpleType>

Now you need to focus on the first few lines of code, as follows:

<xsd:schema targetnamespace= ' Http://www.SampleStore.com/Account '
Xmlns:xsd= ' Http://www.w3.org/2000/08/xmlSchema '
Xmlns:acc= ' Http://www.SampleStore.com/Account '

In fact, a given XML schema defines a series of element names, type names, attribute names, and attribute group names. For example, the above code in the order number, commodity number, commodity number type and so on. These names have a certain scope of action, similar to the concept of local variables in C + +. And what are their scope of action? is valid in their target namespace (targeted namespace), in the above code, we can see that its target name space is Http://www.SampleStore.com/Account.

It is important to note that the name of the target namespace must conform to the semantics of the URL. It should also be noted that the target namespace does not refer to a specific file, just give it a name. In fact, the definitions and declarations in the schema can also refer to other namespaces, which we can name as the source namespace (namespaces). Each schema must have a target namespace, but it can have more than one source name space.

In fact, in a given schema, each name belongs to a particular namespace. Namespace names can be very long, such as http://www. Samplestore.com/account), but they can be simplified using semantic xmlns in XML schemas. In the above example, we use an XSD (named XSD is arbitrary, as you can name Kkk,tt,ppp,gh) to represent the namespace ' http://www.w3.org/2000/08/ XmlSchema ', using ACC to denote namespace http://www. Samplestore.com/account. At the same time we refer to the Http://www.w3.org/2000/08/xmlSchema namespace as the standard namespace (standard namespace), because it is the standard for defining schema syntax

In the above code, the target namespace (targetnamespace) contains the name order number, the item number, the item number type. The names schema,element,simpletype,pattern,string and Positiveinteger belong to the standard name space Http://www.w3.org/2000/08/xmlSchema. In other words, their meaning is defined in the Http://www.w3.org/2000/08/xmlSchema. In fact, all of the standard XML SCHMEA syntax, semantics, and data structures are defined in Http://www.w3.org/2000/08/xmlSchema. When the parser encounters a name (such as Schema,element, and so on), it knows where to get the correct semantics for its use.

We do not need to specify the schemalocation for the standard namespace and the target name space. Because for the target namespace, schemalocation is the document itself. For standard namespaces, it is well known and does not need to be specified. For the source target space, it is necessary to specify its schemalocation. See the following example:

<schema targetnamespace= ' Http://www.SampleStore.com/Account '
xmlns= ' Http://www.w3.org/1999/xmlSchema '
Xmlns:acc= ' Http://www.SampleStore.com/Account '
Xmlns:part= ' Http://www.PartnerStore.com/PartsCatalog '

<import namespace= ' Http://www.PartnerStore.com/PartsCatalog '
schemalocation= ' http://www.ProductStandards.org/repository/alpha.xsd '/>
<element name= ' order number ' type= ' positiveinteger '/>
<element name= ' Item number ' type= ' ACC: Commodity number type '/>
<simpletype name= ' commodity numbering type ' base= ' string '
<pattern value= ' [a-z]{1}d{6} '/>
</simpleType>
<element name= ' watch ' type= ' part: Watch type '/>

Before talking about the schemalocation of the source target space, let's take a look at this line of code:

xmlns= ' Http://www.w3.org/1999/xmlSchema '

What does it mean? It means that all the names in the document, preceded by a prefix, are defined and parsed by Http://www.w3.org/1999/xmlSchema. This is certainly very reasonable. Because in fact, many of the names in our XML schema (such as Schema,element,simpletype,pattern, etc.) come from this standard namespace, and repeatedly prefix the front, do you bother? So simply define it as the default, so if a name is not preceded by a prefix, it is assumed to be handled by Http://www.w3.org/1999/xmlSchema.

Okay, now let's see.

<import namespace= ' Http://www.PartnerStore.com/PartsCatalog '
schemalocation= ' http://www.ProductStandards.org/repository/alpha.xsd '/>

This code, because the Http://www.PartnerStore.com/PartsCatalog is a source namespace, you must specify its schemalocation, the import of the keyword to introduce the schemalocation.

Note that the following line of code

<element name= ' watch ' type= ' part: Watch type '/>

It represents the type of the element watch type, which is defined in Http://www.PartnerStore.com/PartsCatalog. If the parser is looking for a definition of a watch type, it needs to be searched under the name space Http://www.PartnerStore.com/PartsCatalog.


So far, you should have a more perceptual understanding of the name space.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.