File: 10-xml-expressions-and-patterns.md

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (146 lines) | stat: -rw-r--r-- 5,299 bytes parent folder | download | duplicates (3)
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
---
title: XML
layout: default
chapter: 10
---

# XML Expressions and Patterns

__By Burak Emir__

This chapter describes the syntactic structure of XML expressions and patterns.
It follows as closely as possible the XML 1.0 specification,
changes being mandated by the possibility of embedding Scala code fragments.

## XML expressions

XML expressions are expressions generated by the following production, where the
opening bracket `<` of the first element must be in a position to start the lexical
[XML mode](01-lexical-syntax.html#xml-mode).

```ebnf
XmlExpr ::= XmlContent {Element}
```

Well-formedness constraints of the XML specification apply, which
means for instance that start tags and end tags must match, and
attributes may only be defined once, with the exception of constraints
related to entity resolution.

The following productions describe Scala's extensible markup language,
designed as close as possible to the W3C extensible markup language
standard. Only the productions for attribute values and character data are changed.
Scala does not support declarations, CDATA sections or processing instructions.
Entity references are not resolved at runtime.

```ebnf
Element       ::=    EmptyElemTag
                |    STag Content ETag

EmptyElemTag  ::=    ‘<’ Name {S Attribute} [S] ‘/>’

STag          ::=    ‘<’ Name {S Attribute} [S] ‘>’
ETag          ::=    ‘</’ Name [S] ‘>’
Content       ::=    [CharData] {Content1 [CharData]}
Content1      ::=    XmlContent
                |    Reference
                |    ScalaExpr
XmlContent    ::=    Element
                |    CDSect
                |    PI
                |    Comment
```

If an XML expression is a single element, its value is a runtime
representation of an XML node (an instance of a subclass of
`scala.xml.Node`). If the XML expression consists of more
than one element, then its value is a runtime representation of a
sequence of XML nodes (an instance of a subclass of
`scala.Seq[scala.xml.Node]`).

If an XML expression is an entity reference, CDATA section, processing
instruction, or a comment, it is represented by an instance of the
corresponding Scala runtime class.

By default, beginning and trailing whitespace in element content is removed,
and consecutive occurrences of whitespace are replaced by a single space
character `\u0020`. This behavior can be changed to preserve all whitespace
with a compiler option.

```ebnf
Attribute  ::=    Name Eq AttValue

AttValue      ::=    ‘"’ {CharQ | CharRef} ‘"’
                |    ‘'’ {CharA | CharRef} ‘'’
                |    ScalaExpr

ScalaExpr     ::=    Block

CharData      ::=   { CharNoRef } $\textit{ without}$ {CharNoRef}`{'CharB {CharNoRef}
                                  $\textit{ and without}$ {CharNoRef}`]]>'{CharNoRef}
```

<!-- {% raw  %} stupid liquid borks on the double brace below; brace yourself, liquid! -->
XML expressions may contain Scala expressions as attribute values or
within nodes. In the latter case, these are embedded using a single opening
brace `{` and ended by a closing brace `}`. To express a single opening braces
within XML text as generated by CharData, it must be doubled.
Thus, `{{` represents the XML text `{` and does not introduce an embedded Scala expression.
<!-- {% endraw %} -->

```ebnf
BaseChar, Char, Comment, CombiningChar, Ideographic, NameChar, S, Reference
              ::=  $\textit{“as in W3C XML”}$

Char1         ::=  Char $\textit{ without}$ ‘<’ | ‘&’
CharQ         ::=  Char1 $\textit{ without}$ ‘"’
CharA         ::=  Char1 $\textit{ without}$ ‘'’
CharB         ::=  Char1 $\textit{ without}$ ‘{’

Name          ::=  XNameStart {NameChar}

XNameStart    ::= ‘_’ | BaseChar | Ideographic
                 $\textit{ (as in W3C XML, but without }$ ‘:’$)$
```

## XML patterns

XML patterns are patterns generated by the following production, where
the opening bracket `<` of the element patterns must be in a position
to start the lexical [XML mode](01-lexical-syntax.html#xml-mode).

```ebnf
XmlPattern  ::= ElementPattern
```

Well-formedness constraints of the XML specification apply.

An XML pattern has to be a single element pattern. It
matches exactly those runtime
representations of an XML tree
that have the same structure as described by the pattern.
XML patterns may contain [Scala patterns](08-pattern-matching.html#pattern-matching-expressions).

Whitespace is treated the same way as in XML expressions.

By default, beginning and trailing whitespace in element content is removed,
and consecutive occurrences of whitespace are replaced by a single space
character `\u0020`. This behavior can be changed to preserve all whitespace
with a compiler option.

```ebnf
ElemPattern   ::=    EmptyElemTagP
                |    STagP ContentP ETagP

EmptyElemTagP ::=    ‘<’  Name [S] ‘/>’
STagP         ::=    ‘<’  Name [S] ‘>’
ETagP         ::=    ‘</’ Name [S] ‘>’
ContentP      ::=    [CharData] {(ElemPattern|ScalaPatterns) [CharData]}
ContentP1     ::=    ElemPattern
                |    Reference
                |    CDSect
                |    PI
                |    Comment
                |    ScalaPatterns
ScalaPatterns ::=    ‘{’ Patterns ‘}’
```