XML document verification-Basics

Source: Internet
Author: User
I. advantages and risks of XML scalability

You can see from the XML (eXtensible Markup Language Extensible Markup Language) Name that XML is easy to expand. Indeed, as a markup language that describes "metadata", XML standards provide some basic syntaxes for Building XML documents, but do not define exact tags, anyone can use custom elements and attributes as needed to provide structure information for the XML document to expand the TAG set.

XML scalability is not hard to understand. As a new markup language, XML provides a scalable and powerful standard to describe data so that data can be used in applications.ProgramUsed in or exchanged between different platforms and applications. XML only describes data and does not specify how to display data for an end user or system. (Of course, you can use other XML-related technologies such as XPath, XSLT, or programming languages to manipulate and display the data ).

At this point, XML is very different from the HTML that inherits from SGML (Standard Generalized Markup Language Standard General Markup Language) and is also a subset of SGML. The purpose of HTML is to display data through a browser (for example, a browser). You can only use pre-defined specific tags. For example, to create a link, you must use the <A> tag, and you can only use the "href" attribute to specify the location of the Link Target. Similarly, to import an image into HTML, you can only use the tag, and you can only use the "src" attribute in this tag to specify the path of the image file. Other HTML elements and attributes are also the same. They are all prefixed with specific names.

XML, which allows you to freely create tags and attributes, brings great convenience to applications. Based on application requirements, you can specify more meaningful and self-descriptive names for those tags without blindly following some restrictions that may be inappropriate. For example, you can replace the <A> tag with <link>, and replace "href" with "path" to make the link clear. Rest assured that this is allowed in the XML specification.

In fact, due to the scalability of XML, XML documents are in various fields (such as scientific dictionary, legal vocabulary, medical vocabulary, computer vocabulary, and public telephone exchange network vocabulary) has been widely used in standardization construction.

This custom XML document tag "freedom" undoubtedly has obvious advantages, and it is true in a specific environment. However, XML documents are often used for data exchange, XML provides a very useful and scalable framework to mark data that can be exchanged in a platform-independent manner between applications. It is widely used to exchange data between application objects, between different platform applications, or even on the Internet. At this time, the ability to create your own XML document elements and attribute names really requires a certain price, this price is especially high when you want to exchange XML documents with other customers or applications that are not familiar with your document structure. Can all these participants (platforms, applications, and people) correctly use these XML documents? Can custom tags in them be recognized?

At first glance, this seems to have caused confusion. After all, if everyone can use a tag that does not follow any naming conventions to create XML documents, how can others use these documents?

Ii. xml verification-relativity of freedom

It seems that documents that fully comply with the XML specifications are not always required. In many cases, you need to ensure the validity of the document. A metadata document is created to define the elements, attributes, and other items contained in the XML document. There are three such "metadata" documents, which are DTD, XML-DR schema, and W3C schema, which define what structure an XML document must follow to be effective. By defining the structure of the XML document, the application can verify the document before performing any calculation or conversion.

It is clear that you can customize the required tags in the XML document to describe the data. Anyone who wants to use this document can also use it, you just need to provide them with a document on how to organize and use the tag to describe the definition of data! Since XSD Schema is the W3C recommendation standard of the World Wide Web alliance, the following verification only applies to it.

Iii. XML Schema verification from the XML document

After creating an XML schema document, you can use it to verify the validity of the XML document. It is easy to do. You only need to reference the schema file in the root element of the XML document. However, based on whether the XML schema document contains the targetnamespace attribute, there are two ways to reference the XML document:

1. Use the nonamespceschemalocation attribute to reference the schema file

When the XML schema document does not include the targetnamespace attribute, the nonamespaceschemalocation attribute of the root element of the XML document and the W3C schmea instance namespace (xmlns: xsi = "http://www.w3.org/2001/XMLScheam-instance ";) to reference the XML schema file. The following example references an architecture file named "notargetns. XSD" that does not contain the "targetnamespace" attribute:

<? XML version = "1.0" encoding = "UTF-8"?>
<Employees xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance;
"Xsi: nonamespaceschemalocation =" notargetns. XSD ">
<Name> Liny </Name>
<Age> 42 </age>
</Employees>

2. Use the schemalocation attribute to reference the schema file

However, if the XML schmea file contains a targetnamespace attribute, the schema document will be referenced through the schemalocation attribute instead of the nonamespaceschemalocation attribute in the XML document. The value specified by this attribute must be complete. It must contain two parts separated by spaces. The first part is Uri, which is consistent with the URI referenced inside the targetnamespace attribute of the schema document; the last part is the complete path and name of the schema file. In addition, the root element of the XML document must also declare the schema instance namespace (xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance";). The following example references the name yestargetns that contains the "targetnamespace" attribute. XSD architecture file:

<? XML version = "1.0" encoding = "UTF-8"?>
<Employees xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance ";
Xsi: schemalocation = "http://www.tuha.net; yestargetns. XSD"
Xmlns = "http://www.tuha.net";>
<Name> xiaom </Name>
<Age> 25 </age>
</Employees>

After the schema file is correctly referenced in the XML file, verification is performed when the XML file is parsed.

4. Use XML schmea for verification in Visual Studio. NET ide

Visual Studio. NET ide provides rich support for using the XML architecture. Its integrated XML architecture designer allows you to create XML architecture documents by drag and drop components. The integrated XML document designer allows you to create XML architectures based on existing XML documents. After that, you can associate the XML document with the XML schema file and verify the correctness and validity of the XML document. In this case, you only needCodeYou do not need to write a single line of code to complete the verification of the document.

1. Create an XML Architecture

You can use any text editor to create an XML schema file. Obviously, you must be familiar with the specifications of the schema document. In addition, Visual Studio. Net ide provides several simple ways to create XML architecture documents. With the XML architecture designer, drag the node element components you need from the toolbox to complete most of the work created in the XML architecture document (if you do not need to modify the node data type, this is all about creating XML Schema files ). In particular, if you have already created an XML document, you can create an architecture based on the existing XML file. with simple operations, you can generate the XML schema file that this XML document should follow:

1. Load the XML Source Document (. xml file) to the XML designer.

2. Select the "XML" menu item from the IDE menu bar and click "create schema ".

Simply look at the project "all files" and you will find that the XML architecture (. XSD file) with the same name as the XML document is generated.

However, when using this method to generate an XML schema, all data types are set to XSD: string by default. Therefore, you may need to modify them to meet the requirements of the XML document.

2. Associate XML documents with XML architecture files

If you have created an XML source file and an XML schema file, you must associate the XML source file with the XML schema file to make the XML schema file work for the XML file. In Visual Studio. net ide (integrated development environment), the implementation of this association is very simple, by defining the targetschema attribute of the XML file can be completed:

1. Load the XML file to the "XML designer.

2. Switch to the "XML" view mode.

3. In the Properties window, select the schema file associated with the namespace from the drop-down list of the "targetschema" attribute.

In this case, you should note that the drop-down list of the targetschema attribute lists all the XML architectures included in the project and some suggested specifications, including the corresponding namespace. This also tells us that if the required XML schema is not included in this project, you will need to enter the Uniform Resource Identifier (URI) that will be used to verify the schema of the XML document here ).

After completing these steps, return to view your xml source file and you will find that, within the root element, the XML architecture is referenced with the default namespace.

Note: Clear the content of the targetschema attribute to remove the association between the XML document and the XML schema.

3. perform verification

After the XML schema file is associated with the XML source file, the "XML designer" verification function can check whether the XML file is valid:

1. Load the XML file to be checked to the XML designer.

2. Select "verify XML data" from the "XML" menu ".

Then, perform the verification operation. This process also checks the correctness of the XML document. If an error is found, the status bar will indicate it. In the event of a validity error, the system prompts "verification error found" and provides detailed instructions in the "Task List". Otherwise, the system prompts "verification error not found" and passes verification.

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.