Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of XML and Schema Namespace

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of XML and Schema namespaces, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Example 1: focus on understanding the relevant concepts of namespaces.

The following example is an XML Schema file named note.xsd

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

ToveJaniReminderDon't forget me this weekend!

This snippet, xmlns:xsd= "www.w3.org/2001/XMLSchema", indicates that the elements and data types used in this schema come from the "www.w3.org/2001/XMLSchema" namespace (namespace). It also indicates that elements and data types from the "www.w3.org/2001/XMLSchema" namespace must use the prefix "xsd:". As a namespace identifier (as a prefix to an element or attribute in a declaration), you can also not use xsd or xsi. This xmlns attribute contains basic XML schema elements, such as element, attribute, complexType, group, simpleType, and so on.

For any XML Schema definition document (XSD), there is a top-level schema (XSD) element. And the schema (XSD) element definition must contain this namespace: www.w3.org/2001/XMLSchema. That is, this namespace is a standard namespace defined by the XML schema specification-all XML schema elements must belong to that namespace.

This snippet, targetNamespace= "www.w3schools.com", indicates that the elements defined by this schema (note, to, from, heading, body) come from the "www.w3schools.com" namespace. This targetNamespace attribute represents the URI of the namespace corresponding to the schema. That is, namespaces should be declared in other documents (including their own documents) that reference the Schema, whose URI should be the attribute value of the targetNamespace. For example, the namespace xmlns= "www.w3schools.com" is also declared here because the extended data types (note, to, from, heading, body) defined by note.xsd itself are used. And the namespace is the default namespace (no prefix). The targetNamespace attribute declares the XML namespace for all new types explicitly created in the schema.

Let's take a look at what the beginning of the XML document note.xml specified by the schema will look like:

The default namespace declaration xmlns= "www.w3schools.com" is combined with the XML Schema namespace just declared to specify the XML document. (that is, the document uses the data defined in this namespace.) xmlns:xsi= "www.w3.org/2001/XMLSchema-instance" is the XML schema instance namespace inherent to any XML instance document and is defined by the XML schema specification. The xsi:schemaLocation= "www.w3schools.com note.xsd" specifies the location of the schema corresponding to the namespace, that is, the note.xsd file in the same path.

Example 2: focus on understanding that Schema documents use self-defined types

After a targetNameSpace is defined in the xsd file, its internally defined elements, attributes, types, etc., all belong to the targetNameSpace, and its own or external xsd files use these elements, attributes, etc., must be found from the defined targetNameSpace. Modify note.xsd to remove the declaration of the default namespace and add a complex type:

In the above code, the complex type stu cannot be found because you have defined a namespace "www.w3schools.com" that exists in "www.w3schools.com", so you should modify the code as follows:

If you do not use reusable components on your own, but only for external use, you can just define targetNameSpace without specifying an alias.

Through the above example, we can have a deep understanding of targetNameSpace. TargetNamespace defines the namespaces of new elements and attributes defined by Schema. The "www.w3.org/2001/XMLSchema" namespace defines elements such as element, attribute, complexType, group, simpleType, etc.

After understanding the above two examples, the contents of the Schema namespace should be clear.

This is all of the article "sample Analysis of XML and Schema Namespaces". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report