Computers Windows Internet

XML Schema is a description of the structure of XML documents. Create an XML File Simple XML Schema Example

Sometimes understanding XML file, sent to the cadastral chamber, is simply necessary when you feel that you need to, somewhere, that something should 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 for 300 memory, the 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. Having opened 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 figure, the 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" - XML version 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 : Beneficiary organization code, in this case Management Federal Service 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 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 a file in Notepad ++, the entire structure of an 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 software product Polygon Map Plan

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

The XML format is designed to store 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 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 do not know how to work with code. However, in this case, the user may encounter certain problems when rewriting the file into XML format. Unfortunately, the operation of converting a regular table to XML is possible 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 code editing programs, 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 in operating system 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 as 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 one, 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.

    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 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 new file which we will call "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 the namespace associated with the 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 rather complex, this method can make the corresponding schema quite cumbersome, which will greatly affect its readability and maintainability.

    The next way to lay out the schema 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 the 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.

    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 take a 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 Internet browser Explorer, 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 - to manage appearance this information. This task can be solved easily and simply: you need to add a transformation template - an 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 browser Internet Explorer, then we will see that our task has been 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 generating 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 a 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 your 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 whose values ​​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 element tags top level, 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.

    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 (the 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

    In the previous article on XML, we looked at such an outdated way of validating XML documents, the XML DTD. This method is still used to validate XML data, but is being replaced more and more by the day new technology called XML Schema. Many shortcomings in XML schemas have been fixed, which were in the XML DTD, so on this moment all leading developers use only XML schemas for document validation.

    To appreciate the advantages of XML Schema over DTDs, let's take a closer look at the main disadvantages of DTDs that have been successfully corrected in XML Schemas. I already mentioned them in the article "", but for a better understanding, let's repeat.

    Disadvantages of XML DTDs over XML Schema

    1. Language syntax other than XML. That is, the DTD is not XML. This can lead to a variety of problems with encoding and verifying XML documents.
    2. No data type checking. There is only one data type in XML DTDs - string. In this regard, for example, if there is text in a numeric field, the document will still be verified, since the XML DTD cannot check the data type.
    3. You cannot map more than one Description DTD to a single XML document. That is, a document can be verified with only one DTD description. If there are several of them, then you will have to redo the descriptions and combine everything in one file, which is very inconvenient.

    These were the main shortcomings of the XML DTD, which were successfully corrected in the industry standard for describing XML documents, XML Schema.

    XML Schema - the industry standard for describing XML documents

    In short, XML Schema does the following:

    1. Describes the names of elements and attributes (dictionary).
    2. Describes the relationship between elements and attributes and their structure (content model).
    3. Describes data types.

    I also want to note that at the moment, using diagrams, you can describe almost everything. That is, a schema is a universal way of describing a data grammar that can be used not only for verifying XML documents, but also for describing databases, etc. Thus, the scope of application of the schemes is very wide at the moment.

    XML Schema Example for XML Document Validation

    As practice shows, the material is much better assimilated if you immediately start studying with examples. I must say right away that we will not delve into all the subtleties, since the material is very complex, especially if you study it in text form.

    Simple XML Schema Example

    Using this schema, you can validate an XML document with the following content.

    <книга xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "book.xsd"> <название>XML basics <цена>300

    So, back to our XML schema. As you have already noticed, XML already known to us is used to create XML schemas. The only difference here is that XML Schema already defines elements, unlike regular XML. Because of this, namespaces are used. In this case, the required namespace is "http://www.w3.org/2001/XMLSchema", which will be prefixed with "xs".

    I want to note right away that you can use both namespace prefixes and set the namespace for the root element. As such, there is no difference here. Everyone decides for himself how to act in a given situation. I will just say that the prefixes "xs" or "xsd" are usually used.

    Well, now let's figure out how to decipher the above scheme. As mentioned above, XML Schema is a description of a dictionary and data types. Based on this, we will decipher each element.

    • - we declare the element "book" with the type "Book".
    • - we declare a complex type named "Book" (xs: complexType - may contain nested elements).
    • - nesting declaration. That is, the type will contain nested elements.
    • - we declare an element with the name "name" (of the standard type "string" - xs: string).
    • - we declare an element named “price” (of the standard type “number” - xs: decimal).

    As you can see, there is nothing super complicated here. If you think about it, everything is very simple.

    Basic XML Schema Elements

    In short, XML schema can be described as follows.

    Your circuit

    As you can see from the example, each XML schema consists of a root element "schema" and a required namespace "http://www.w3.org/2001/XMLSchema". The following is a description of the circuit and the circuit itself. At the same time, very often in very high-quality schemas, the description is much larger than the XML Schema itself.

    Description of elements in XML Schema

    At the beginning of the article, we have already looked at an example of the simplest XML schema. In it, we separately described elements and types. At the same time, I want to note right away that the sequence does not play a role here. The circuit will work anyway.

    Now let's look at the second way to write XML Schema, which is based on describing the type immediately inside the element. The way will fit in the event that you do not plan to use the same description for different elements. For clarity, consider an example.

    As you can see from the example, the element is declared using the special construction "element" using the appropriate prefix. In this case, we define an element named "root" without specifying a type (type), since it will be described inside the element. That is, there are two ways to describe items.

    1 way

    Type Description

    2 way

    Item type description

    You can use both the first method and the second. They all work the same way. The only question is convenience in this particular case.

    Further, after declaring the element, we indicate that it is of a complex type ( ) and enumerate ( ) nested elements. In this case, these are elements name and age with types "xs: string" and "xs: integer". The xs prefix means that this is a base type that is already described in the XML Schema standard.

    As you can see, everything is pretty simple so far. Again, we will not go into all the details, since this article is intended to familiarize yourself with XML Schema, and not study it in detail.

    How XML Schema and Document Mapping

    The peculiarity of XML Schema is that it does not describe the document itself, but the namespace. In this regard, most often there is no mention of her in the document. The handler itself maps the schema you need without using any instructions in the XML document.

    In case the handler does not know where the schema is located, we can indicate where to look for it. This is done using the special "schemaLocation" attribute. Since this attribute belongs to a different namespace, the space must also be specified before using the attribute. For clarity, consider an example.

    XML Schema

    XML Document

    Now let's take a closer look at each line.

    • targetNamespace = "http://www.site.com" - indicate for which namespace this XML Schema.
    • xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" - we connect the namespace in which the "schemaLocation" attribute is described.
    • xsi: schemaLocation = "http://www.site.com/product.xsd" - indicate where the schema can be found in case the parser does not know where it is located. If the XML document does not belong to any namespace, and therefore there is no reference to it in the schema, then the "schemaLocation" attribute is replaced with "noNamespaceSchemaLocation" (reference to the schema without defining namespaces).

    That's all. Good luck and success in learning XML!