summaryrefslogtreecommitdiffstats
path: root/writerfilter/documentation/ooxml/model.rng
blob: a7d298991abf4ed8628ce55b628c4091d3388cc0 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?xml version="1.0" encoding="UTF-8"?>
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
-->
<!--
This file is both a relax-ng schema for writerfilter/source/ooxml/model.xml and
documentation for that file. The schema has two parts:

- first part: a subset of the relax-ng grammar to define *what* we expect as
  the input in a DOCX file
- second part: additional annotation on top of that to define *how* to handle
  that expected input
-->
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
  <!--
  First part: a subset of the relax-ng XML markup.

  The order of elements in this part follow a bottom-up approach.
  -->

  <!-- Basic building blocks: element, attribute and their contents. -->

  <!--
  Describes an XML element.

  Example:

  <element name="charset">
    <ref name="CT_Charset"/>
  </element>
  -->
  <define name="element-element">
    <element name="element" ns="http://relaxng.org/ns/structure/1.0">
      <optional>
        <attribute name="name"/>
      </optional>
      <oneOrMore>
        <choice>
          <ref name="attribute-element"/>
          <ref name="data-element"/>
          <ref name="ref-element"/>
        </choice>
      </oneOrMore>
    </element>
  </define>

  <!--
  Describes an attribute.

  Example:

  <attribute name="name">
    <data type="string"/>
  </attribute>
  -->
  <define name="attribute-element">
    <element name="attribute" ns="http://relaxng.org/ns/structure/1.0">
      <optional>
        <attribute name="name"/>
      </optional>
      <zeroOrMore>
        <choice>
          <ref name="data-element"/>
          <ref name="ref-element"/>
        </choice>
      </zeroOrMore>
    </element>
  </define>

  <!--
  Describes the type of the data contained in an attribute. Possible values:
  boolean, integer or string. See also <text>.
  -->
  <define name="data-element">
    <element name="data" ns="http://relaxng.org/ns/structure/1.0">
      <attribute name="type"/>
    </element>
  </define>

  <!--
  Describes an enumeration element: a possible value for an attribute.
  -->
  <define name="value-element">
    <element name="value" ns="http://relaxng.org/ns/structure/1.0">
      <text/>
    </element>
  </define>

  <!--
  This element is ignored during parsing, it just helps readability.

  Example:

  <choice>
    <value>true</value>
    <value>false</value>
  </choice>
  -->
  <define name="choice-element">
    <element name="choice" ns="http://relaxng.org/ns/structure/1.0">
      <oneOrMore>
        <choice>
          <ref name="data-element"/>
          <ref name="element-element"/>
          <ref name="ref-element"/>
          <ref name="value-element"/>
        </choice>
      </oneOrMore>
    </element>
  </define>

  <!-- Grouping elements: define and grammar. -->

  <!--
  A define is named definition of its contents, so that multiple <ref> elements
  can refer to it, to avoid copy&paste. OOXML named (complex and simple) types
  are described using defines.
  -->
  <define name="define-element">
    <element name="define" ns="http://relaxng.org/ns/structure/1.0">
      <attribute name="name"/>
      <oneOrMore>
        <choice>
          <ref name="choice-element"/>
          <ref name="attribute-element"/>
          <ref name="element-element"/>
          <ref name="data-element"/>
          <ref name="ref-element"/>
          <empty/>
        </choice>
      </oneOrMore>
    </element>
  </define>

  <!--
  A reference to a define.
  -->
  <define name="ref-element">
    <element name="ref" ns="http://relaxng.org/ns/structure/1.0">
      <attribute name="name"/>
    </element>
  </define>

  <!--
  A grammar is a set of defines, one grammar is equivalent to one .xsd file
  from the OOXML spec.
  -->
  <define name="grammar-element">
    <element name="grammar" ns="http://relaxng.org/ns/structure/1.0">
      <attribute name="ns"/>
      <optional>
        <attribute name="attributeFormDefault"/>
      </optional>
      <zeroOrMore>
        <ref name="include-element"/>
      </zeroOrMore>
      <oneOrMore>
        <ref name="define-element"/>
      </oneOrMore>
    </element>
  </define>

  <!--
  Controls the resolution of <ref> elements. The order is:

  - the current grammar
  - included grammars, if there are any
  - the first define in the whole model
  -->
  <define name="include-element">
    <element name="include" ns="http://relaxng.org/ns/structure/1.0">
      <attribute name="href"/>
    </element>
  </define>

  <!--
  Second part: custom markup, building on top of the first one.

  The order of elements in this part follow a top-down approach.

  The output of the code generated from these elements is a token stream. There
  are two types of tokens: SPRM tokens and attribute ones. SPRM refers to
  Single PRoperty Modifier, in this context it means a token that contains other
  tokens. It's used to represent an XML element. That means that SPRM tokens
  can contain other SPRM tokens, and also attribute tokens, while attribute
  tokens only contain simple types (boolean, integer, string).

  More terminology: the types in the OOXML schema have two typical prefixes:

  - CT_something: complex type, used to describe an XML element
  - ST_something: simple type, used to describe the contents of an attribute

  For tokens the following abbreviations are used:

  - NS_something: namespace
  - LN_something: local name
  -->

  <!--
  The model element is the toplevel container for the XML element /
  attribute mapping definition. It contains namespace aliases, direct token
  definitions and mapping definitions for each namespace.
  -->
  <define name="model-element">
    <element name="model">
      <oneOrMore>
        <ref name="token-element"/>
      </oneOrMore>
      <oneOrMore>
        <ref name="namespace-element"/>
      </oneOrMore>
    </element>
  </define>

  <!--
  A token element can explicitly define a token. This allows generating
  such a token in the tokenizers and handling it in the domain mapper. Ideally
  tokens are *not* defined this way, they are mapped to an XML element or
  attribute from the OOXML specification.
  -->
  <define name="token-element">
    <element name="token">
      <!--
      The token name must be ooxml:something, then in C++ it'll be the
      NS_ooxml::LN_something ("OOXML namespace, something local name")
      constant.
      -->
      <attribute name="tokenid"/>
    </element>
  </define>

  <!--
  A namespace element is a container for a subset of the relax-ng grammar
  of a part of the OOXML specification. It also contains the resource
  definitions, which specify how XML elements and attributes are mapped to
  tokens.
  -->
  <define name="namespace-element">
    <element name="namespace">
      <attribute name="name"/>
      <zeroOrMore>
        <ref name="start-element"/>
      </zeroOrMore>
      <ref name="grammar-element"/>
      <zeroOrMore>
        <ref name="resource-element"/>
      </zeroOrMore>
    </element>
  </define>

  <!--
  A start element is similar to the relax-ng start element, but this one has a
  name attribute to refer to a define, while the relax-ng one has a ref child
  element to do the same.
  -->
  <define name="start-element">
    <element name="start">
      <attribute name="name"/>
    </element>
  </define>

  <!--
  A resource element always matches (by its name attribute) a define from the
  grammar of the namespace. It describes how that (simple or complex) type is
  parsed during import.

  Example:

  <resource name="CT_Font" resource="Properties">
    ...
  </resource>

  or

  <resource name="CT_OMathPara" resource="Stream"/>
  -->
  <define name="resource-element">
    <element name="resource">
      <!-- There should be a define element with the same name attribute. -->
      <attribute name="name"/>
      <!--
      This means the resource element will be handled by the
      OOXMLFastContextHandler<resource> class.

      The two most important resources:

      - Properties: this maps elements/attributes to SPRM/attribute tokens
      - Stream: If the element itself does not require any special handling,
        but the subelements are interesting, use this resource.  If no
        explicit resource element is available, then a null context will be
        created and the element and all its subelements will be ignored.
      -->
      <attribute name="resource"/>
      <optional>
        <attribute name="tokenid"/>
      </optional>
      <zeroOrMore>
        <choice>
          <ref name="resource-element-element"/>
          <ref name="resource-attribute-element"/>
          <ref name="resource-value-element"/>
          <ref name="resource-action-element"/>
        </choice>
      </zeroOrMore>
    </element>
  </define>

  <!--
  The <element> child of a <resource> defines what element name will be handled
  via what token.

  Example:

  <element name="charset" tokenid="ooxml:CT_Font_charset"/>

  Means the <charset> element will be handled in the sprm() function of the handler
  class as a NS_ooxml::LN_CT_Font_charset case. (sprm() is a logging wrapper
  around lcl_sprm(), which is the real implementation.)
  -->
  <define name="resource-element-element">
    <element name="element">
      <attribute name="name"/>
      <attribute name="tokenid"/>
    </element>
  </define>

  <!--
  The <attribute> child of a <resource> defines what attribute name will be
  handled via what token.

  Example:

  <attribute name="name" tokenid="ooxml:CT_Font_name"/>

  Means the <name> attribute will be handled in the attribute() (real
  implementation in lcl_attribute()) function of the handler class as a
  NS_ooxml::LN_CT_Font_name case.
  -->
  <define name="resource-attribute-element">
    <element name="attribute">
      <attribute name="name"/>
      <optional>
        <attribute name="tokenid"/>
      </optional>
      <optional>
        <attribute name="action"/>
      </optional>
    </element>
  </define>

  <!--
  A <value> inside a <resource> defines how to map the string data of a value
  to a token. The tokenid attribute defines the token name, the text of the
  element defines the string. This is useful in case the value of an attribute
  is a choice from a predefined list.
  -->
  <define name="resource-value-element">
    <element name="value">
      <attribute name="tokenid"/>
      <text/>
    </element>
  </define>

  <!--
  An <action> inside a <resource> can perform additional actions in the
  following situations:

  - start of the element
  - end of the element
  - character data of the element

  The tokenid attribute restricts the action to a particular element.

  Example:

  <resource name="CT_TxbxContent" resource="Stream">
    <action name="start" action="startTxbxContent"/>
    <action name="end" action="endTxbxContent"/>
  </resource>

  That means that when:

  - <txbxContent> starts, OOXMLFastContextHandler::startTxbxContent() will be called
  - <txbxContent> ends, OOXMLFastContextHandler::endTxbxContent() will be called
  -->
  <define name="resource-action-element">
    <element name="action">
      <attribute name="name"/>
      <attribute name="action"/>
      <optional>
        <attribute name="tokenid"/>
      </optional>
      <optional>
        <attribute name="sendtokenid"/>
      </optional>
      <optional>
        <ref name="resource-action-cond-element"/>
      </optional>
    </element>
  </define>

  <!--
  Some actions take parameters, which can be defined by the <cond> element.

  Example:

  <resource name="CT_FldChar" resource="Stream">
    <action name="start" action="fieldstart">
      <cond tokenid="ooxml:CT_FldChar_fldCharType" value="ooxml:Value_ST_FldCharType_begin"/>
    </action>
  </resource>

  That means:

  - if the <fldChar> starts with an fldCharType attribute being "begin"
  - then perform the "fieldstart" action.
  -->
  <define name="resource-action-cond-element">
    <element name="cond">
      <attribute name="tokenid"/>
      <attribute name="value"/>
    </element>
  </define>

  <!-- The entry point of the schema. -->
  <start>
    <ref name="model-element"/>
  </start>
</grammar>
<!-- vim: ft=xml shiftwidth=2 softtabstop=2 expandtab:
-->