440 n examples of xml files. Sample XML Schema. XML Schema - the industry standard for describing XML documents

02.08.2021 Information

For all examples below, the XSL language standard is used. A more modern modification of this standard, the XSL language, is also widely used. T, more details about which you can read in \ xml \ XSLTutorial or MSDN.

Let's look at a simple example XML file (ex01.xml). This and other examples can be found in the \ xml \ XSLTForBeginers folder on disk.



XSL Notes

If we open this file in the Internet Explorer browser, we will see the same text that is shown above, along with all the tags and service information. But we don't need tags and service information! We want to see only the information that is relevant to the case, and with the help of tags, we want to control the appearance of this information. This task can be solved easily and simply: it is necessary to add a transformation template - XSL-file to the XML-file.

Let's rewrite our XML file as follows (ex01-1.xml).




XSL Notes

And create an XSL file ex01-1.xsl. The text of the file is shown below.






If we now open the ex01-1.xsl file in the Internet Explorer browser, we will see that our task is solved - only the information we need remains on the screen, all the tags have disappeared. The result you get on the browser screen is shown below.

XSL Notes

It is also easy to see that the order in which the lines are output is determined only by the content of the transformation template - the XSL file. If necessary, the template can be easily changed without changing our main XML file at all.

Let's rewrite the XML file. We will not change the informational part, but specify another template ex01-2.xml.




XSL Notes

Let's create an XSL file ex01-2.xsl. The text of the file is shown below.






If we now open the ex01-2.xsl file in Internet Explorer, the result will be different.

XSL Notes

Let's now point out a moment that is key for database developers. The information in an XML page usually appears as a result of a database query. Querying a database in a multi-user environment is very expensive. Suppose now that we have no XML and we are rendering standard static HTML pages. In this case, to solve the problem of simple transformation of the external representation of information, for example, to change the sorting, we have two ways to solve the problem: execute the query and store the results in some temporary buffer on the server, or each time the external representation changes, execute a new query and re-generate the HTML page.

The first method requires laborious programming, the second method significantly increases the load on the database server, the performance of which is often the bottleneck of the system - the user always wants to get results faster.

XML and XSL provide a comprehensive solution to the problem described above. In fact, an XML page is a temporary buffer for query results. Only instead of non-standard and time-consuming programming, we now use the standard XSL mechanism.

There is one more consideration that can be significant for database developers. Most modern DBMSs can format the results of a database query as an XML file. That is, when building a user interface in the framework of XML and XSL technology, we achieve a certain independence from the DBMS vendor. In terms of the organization of the withdrawal - almost complete independence. And this part is quite large in most database-oriented applications. Of course, in addition to output, there is also input and server processing of business logic, but here you will have to look for some other solutions.

The first steps

Let us now examine the first example in more detail. Let us recall its text.



XSL Notes

The first line informs the browser that the file is in XML format. The version attribute is required. The encoding attribute is optional, but if you have Russian letters in the text, then you must insert this attribute, otherwise the XML file will simply not be processed - you will receive an error message.

The following lines are the body of the XML file. It consists of elements that together form a tree structure. Elements are identified by tags and can be nested within each other.

Elements can have attributes, the values ​​of which can also be processed according to the template.

There is always one element at the top level of an XML file. That is, a file of the form



XSL Notes



"Introduction to CSP"

will not be processed by the browser. To convert to a valid XML file, you need to add top-level element tags, for example




XSL Notes



"Introduction to CSP"


Note that tag names are case sensitive. You can read more about this in any book on XML elements and attributes; these books pay a lot of attention.

Now let's move on to the transformation template - the XSL file. The task of the XSL file is to transform the tree of the XML file into another tree, which, for example, will correspond to the HTML format and can be displayed on the browser screen, taking into account formatting, choice of fonts, etc.

In order for the browser to perform the necessary transformation, you need to specify a link to the XSL file in the XML file


Now let's look at the text of the XSL file






The first line of the file contains the xsl: stylesheet element tag. Element attributes are version number and namespace reference. These attributes of the xsl: stylesheet element are required. In our case, the namespace is all the names of the elements and their attributes that can be used in the XSL file. For XSL files, the namespace reference is standard.

Note that the XSL file is a type of XML file. It does not contain user data, but the format is the same. The file contains the top-level element xsl: stylesheet, followed by a tree of transformation rules.

In this document, we will not explain in detail what each element of an XSL file means. We will give various examples and show the result in each example, which will make it possible to independently associate various elements of the XSL file and the transformations of the original XML file with user information initiated by these elements. Also note that the value of the attribute select and similar ones with the meaning "choose" is written in a special language XPath, about which you can read in brief on the disk \ xml \ XPathTutorial, and in more detail in MSDN.

In what follows, we will present the texts of XML and XSL files in black and white. You can always open a real file and see everything in color. If necessary, comment out the link to the XSL file. The comment syntax is as follows -... You cannot insert symbols - into the comment text.

In the first example, we saw how the xsl: value-of element can be used to display the content of an element (text enclosed between tags) in HTML format. Now we'll see how the same element can be used to display the value of an element's attribute.

Consider the following xml file ex02-1.xml

Sometimes understanding the XML file sent to the cadastral chamber is simply necessary when you feel that you need to, where something needs to be corrected, and many have developed a natural curiosity. But ... Important. Remember that after signing the EDS file, you cannot edit the XML file! As an example from practice, when dividing a plot of 300 memory, a girl accidentally erased just one line in the address from one of the plots. And that's it .. The XML file did not pass validation. Searching through the program through all the records seemed tedious to me, the more I knew what to look for. Opening the XLM file in the test editor with a search, I found the required line and made changes. But again, I repeat - if you edited the file, then you need to re-sign it with the EDS again. So let's get started. I will present drawings and describe them.

And one more retreat, for editing and convenient viewing of XML files I recommend using the program. It will allow you to see the structure of the file and conveniently color it by values. For example, I will take an electric heated towel rail for a bathroom, well, of course not his -), but an XML file formation of 2 storage units by allocating a share in the common ownership of a storage unit with a cadastral number….
As an example, I will first show a picture of a collapsed XML file with one collapsed block.

So, judging by the line counter on the left, we have a file with 336 lines. As you can see from the picture block Collapsed, plus sign on the right. Therefore, I will first describe line by line what can be seen from this figure, and then we will analyze the blocks included in the block Since it is the most voluminous.

We will not consider the entire file line by line, but consider only those lines in which our data is entered.

3 line : CodeType = "014" - type of work, there are only two of them, as in our case - education and "015" - clarification. Version = "03" - the version of the XML file. GUID (Globally Unique Identifier) ​​is a statistically unique 128-bit identifier; in our case, the file name must match this indicator.
4 line : The data of the organization in which the cadastral engineer works, or the data of the cadastral engineer himself.
5 line : Code of the recipient's organization, in this case the Office of the Federal Service for State Registration, Cadastre and Cartography in the Republic of Tatarstan.

Now let's immediately go down to the end of the file, based on the presented figure and consider the block ... This block contains information about the cadastral engineer who performed the work. Next comes the block where the coordinate system fits. by the way & quotin text file values, denote quotation marks.

In principle, by opening the file in Notepad ++, the entire structure of the XML file becomes clear. - Location - contains the address. Value in written when there is no part of the address to KLADR.

An example of a plan map XML file generated in the Polygon Map plan software product

For the most visual view of this fragment, copy it and view it in the test editor Notepad ++, which you can download from the section

Taking the means for the end, people become disillusioned with themselves and others, due to which nothing comes out of all their activities, or the opposite of what they were striving for.I. Goethe

This chapter will show you how to write XML schemas. You will also learn that diagrams can be written in many different ways.

XML document

Let's take a look at the following XML document called "shiporder.xml":

John smith Ola Nordmann

Langgt 23
4000 Stavanger Norway Empire burlesque Special Edition 1 10.90 Hide your heart 1 9.90

The above XML document consists of a root element shiporder with required attribute orderid ... Element shiporder contains three children: orderperson, shipto and item... Element item is used twice and contains the element title, optional element note as well as elements quantity and price.

Line xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against the schema. Line xsi: noNamespaceSchemaLocation = "shiporder.xsd" specifies where the schema is located (in this case, it is located in the same folder as the "shiporder.xml" file).

XML schema creation

Now let's create an XML schema for the above XML document.

Let's create a new file called "shiporder.xsd". To create an XML schema, we will simply follow the structure of the XML document and define each element it encounters. Let's start with a standard XML declaration followed by an element xs: schema , which defines the scheme itself:

...

Here we are using the standard namespace ( xs ) and the URI associated with that namespace, which has a default value http://www.w3.org/2001/XMLSchema .

Now we have to define the element shiporder... This element has an attribute and contains other elements, so we treat it as a composite type element. Element child definitions shiporder put in the declaration xs: sequence , which defines a rigid sequence of subelements:

...

Now let's define the element orderperson, which will be of a simple type (since it does not contain any attributes or other elements). Its type ( xs: string ) is prefixed with a namespace associated with an XML schema, which indicates the use of a predefined data type:

Now we need to define: shipto and item... Let's start by defining the element shipto:

With the help of diagrams, we can define any element. Attributes will help us with this. maxOccurs and minOccurs ... Attribute maxOccurs specifies the maximum number of occurrences of an element, and the attribute minOccurs specifies the minimum number of occurrences. By default, the value of both attributes is 1.

Now let's define the element item... This element can be used an unlimited number of times within an element. shiporder... Determine such a feature of an element item allows assignment to attribute maxOccurs meaning "unbounded" ... This means that the element item can be used as many times as needed by the author of the document. Note that the element note optional. We define this by setting the attribute minOccurs to zero:

Now we can declare the attribute of the element shiporder... Since this is a required attribute, we use the definition use = "required" .

Note: Attributes should always be declared last:

Here is the complete code for the "shiporder.xsd" schema file:

Splitting a schema

The previous method of layout of the schema is quite simple, however, when the document is quite complex, this method can make the corresponding schema quite cumbersome, which will greatly affect its readability and maintainability.

The next way to lay out a diagram is to first define all the elements and attributes, and then reference these definitions using the attribute ref .

Following is the new layout of the schema file ("shiporder.xsd"):

Using named types

The third way to layout a schema involves defining classes or types that allow reuse of member definitions. This becomes possible by giving names to the elements simpleTypes and complexTypes and then point to them using the attribute type .

The third way to link the schema file ("shiporder.xsd"):

Element restriction indicates that the data type is derived from the data types in the W3C XML Schema namespace. Thus, the following piece of code means that the value of an element or attribute must be a string:

However, much more often the element restriction used to impose constraints on elements. Take a look at the following lines from the above diagram:

This code snippet specifies that the value of an element or attribute must be a string, exactly six characters in length, and that those characters must be numbers from 0 to 9.

XML-format is intended for storing data that can be useful in the work of some programs, sites and support for certain markup languages. It is not difficult to create and open a file with this format. This can be done even if the computer does not have any specialized software installed.

XML itself is a markup language, somewhat similar to HTML, that is used in web pages. But if the latter is used only for displaying information and its correct markup, then XML allows it to be structured in a certain way, which makes this language something like an analogue of a database that does not require a DBMS.

You can create XML files both using specialized programs and the built-in Windows text editor. The convenience of writing the code and the level of its functionality depend on the type of software used.

Method 1: Visual Studio

Instead of this Microsoft code editor, you can use any of its counterparts from other developers. In fact, Visual Studio is a more advanced version of the regular "Notepad"... The code now has special highlighting, errors are highlighted or corrected automatically, and special templates are already loaded into the program, which make it possible to simplify the creation of large XML files.

To get started, you need to create a file. Click on item "File" in the top bar and from the drop-down menu select "Create…"... A list will open where the item is indicated "File".

  • You will be transferred to the window with the choice of the file extension, respectively, select the item "XML file".
  • The newly created file will already have the first line with the encoding and version. The default is the first version and encoding. UTF-8 which you can change at any time. Next, to create a full-fledged XML file, you need to register everything that was in the previous instruction.

    When finished, select again from the top bar "File", and there from the drop-down menu item "Save All".

    Method 2: Microsoft Excel

    You can create an XML file without writing any code, for example, using modern versions of Microsoft Excel, which allows you to save tables with this extension. However, you need to understand that in this case, you will not be able to create something more functional than a regular table.

    This method is more suitable for those who do not want or know how to work with code. However, in this case, the user may encounter certain problems when rewriting the file into XML format. Unfortunately, it is possible to perform the operation of converting a regular table to XML only on the newest versions of MS Excel. To do this, use the following step-by-step instructions:


    Method 3: Notepad

    Even the usual one is fine for working with XML. "Notebook", however, a user who is not familiar with the syntax of the language will have a difficult time, since various commands and tags must be written in it. The process will be somewhat simpler and much more productive in specialized programs for editing code, for example, in Microsoft Visual Studio. They have special tag highlighting and tooltips, which greatly simplifies the work of a person who is new to the syntax of this language.

    For this method, you do not need to download anything, since the operating system is already built-in "Notebook"... Let's try to make a simple XML table in it according to the given instructions:


    The finished result should look something like this:




    25
    True


    XML compilers must process this code in the form of a table with one column, which contains data about a certain Ivan Ivanov.

    V "Notepad" it is quite possible to make simple tables like this, but when creating more voluminous data arrays, difficulties may arise, since in the usual "Notepad" there are no functions for correcting errors in the code or highlighting them.

    As you can see, there is nothing difficult about creating an XML file. If desired, it can be created by any user who more or less knows how to work on a computer. However, to create a full-fledged XML file, it is recommended that you learn this markup language, at least at a primitive level.