📜 ⬆️ ⬇️

XSD - Smart XML

XSD is a language for describing the structure of an XML document. It is also called XML Schema. When using XML Schema, an XML parser can verify not only the correctness of the XML document syntax, but also its structure, content model, and data types.

This approach allows object-oriented programming languages ​​to easily create objects in memory, which is undoubtedly more convenient than parsing XML as a plain text file.

In addition, XSD is extensible, and allows you to connect ready-made dictionaries to describe common tasks, such as web services, such as SOAP.
')
It is also worth mentioning that XSD has built-in documentation, which allows you to create self-contained documents that do not require additional description.

Consider as an example XSD document, which describes part of the structure of the account on habr.





I did not include the text of the XSD schema and the XML document corresponding to this schema in the article due to their size.

The first line of the schema indicates that the document is an XML document and uses UTF-8 encoding.
<? xml version ="1.0" encoding ="UTF-8" ? >
From the next line begins the description of the main element of the document - habra_user .
< xs:element name ="habra_user" >
Lines documenting the element:
< xs:annotation >
< xs:documentation > . </ xs:documentation >
</ xs:annotation >

The <xs: complexType> tag describes the “complex” data type user_name . If desired, it can be rendered as a separate data type, by analogy with contact_info . To do this, you need to transfer the <xs: complexType> block to <xs: schema> and specify the name attribute, and set the type attribute to the element.

The elements user_name , first_name , last_name are of string type and describe the user, name and surname of the account holder.

The date_of_birth element has a date data type and describes the date of birth.

The registration date is described by register_date , which has its own data type, customDateTime . The value of this tag will be set using the value attribute. This is indicated by the lines.
< xs:attribute name ="value" use ="required" >
In this case, the attribute is required. In order to meet the requirements, we will describe the “checks”:
< xs:simpleType >
< xs:restriction base ="xs:string" >
< xs:length value ="19" />
< xs:pattern value ="[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]" />
</ xs:restriction >
</ xs:simpleType >

In this case, the length of the string will always be 19, this is specified by the <xs: length> tag and the value itself will correspond to the pattern specified in the <xs: pattern> tag .

The contact_info and blog elements are arrays, as indicated by the maxOccurs = "unbounded" attribute.

The <xs: choice> tag determines that the nested element will be one of the elements of ICQ or linkedin.

The <xs: sequence> tag indicates that nested elements will be blog_name and blog_url in that order. If the sequence is not important, then you need to use the <xs: all> tag .

In addition to XSD schemes, you can read Wikipedia and W3C . The Altova XMLSpy program was used to create the layout.

Thanks for attention!

Source: https://habr.com/ru/post/90696/


All Articles