summaryrefslogtreecommitdiffhomepage
path: root/ru-ru/xml-ru.html.markdown
blob: 0044f6bfa7c88a532800120a9312329b8c64aac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
language: xml
filename: learnxml-ru.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
lang: ru-ru
---

XML - это язык разметки, предназначеный для хранения и передачи данных.

В отличие от HTML, XML не определяет, как отображать или формировать данные, он только содержит их.

* XML Синтаксис

```xml
<!-- Комментраии в XML выглядят так -->

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="КУЛИНАРИЯ">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="ДЕТИ">
    <title lang="ru">Гарри Поттер</title>
    <author>Дж. К. Роулинг</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="ВСЕМИРНАЯ ПАУТИНА">
    <title lang="ru">Изучаем XML</title>
    <author>Эрик Рэй</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

<!-- Вышеописанный документ - типичный XML-файл.
  Он начинается с определения, информирующее о некоторых метаданных (необязательно).
  
  XML использует древовидную структуру. У вишеописанного документа корневой узел - 'bookstore', который содержит
  три дочерних узла - все 'books'. Такие узлы содержат много дочерних узлов и т.д. 
  
  Узлы создаются с помощью открывающего/закрывающего тегов, а дочерние - это узлы между
  открывающимися и закрывающимися тегами.-->


<!-- XML carries two kind of data:
  1 - Attributes -> That's metadata about a node.
      Usually, the XML parser uses this information to store the data properly.
      It is characterized by appearing in parenthesis within the opening tag
  2 - Elements -> That's pure data.
      That's what the parser will retrieve from the XML file.
      Elements appear between the open and close tags, without parenthesis. -->
      
  
<!-- Below, an element with two attributes -->
<file type="gif" id="4293">computer.gif</file>


```

* Well-Formated Document x Validation

A XML document is well-formated if it is syntactically correct.
However, it is possible to inject more constraints in the document,
using document definitions, such as DTD and  XML Schema.

A XML document which follows a document definition is called valid, 
regarding that document. 

With this tool, you can check the XML data outside the application logic.

```xml

<!-- Below, you can see an simplified version of bookstore document, 
  with the addition of DTD definition.-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Bookstore.dtd">
<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>

<!-- This DTD could be something like:-->

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>


<!-- The DTD starts with a declaration.
  Following, the root node is declared, requiring 1 or more child nodes 'book'.
  Each 'book' should contain exactly one 'title' and 'price' and an attribute
  called 'category', with "Literature" as its default value.
  The 'title' and 'price' nodes contain a parsed character data.-->

<!-- The DTD could be declared inside the XML file itself.-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>
```