Fix a bug in recording a macro while flyspell-mode is active
[emacs.git] / lisp / xml.el
Commit [+]AuthorDateLineData
1cd7adc6 Pavel Janík2001-07-14 11:21:08 +00001;;; xml.el --- XML parser
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +00004
5;; Author: Emmanuel Briot <briot@gnat.com>
720058f2 Richard M. Stallman2003-06-30 10:34:50 +00006;; Maintainer: Mark A. Hershberger <mah@everybody.org>
a98e819b Dave Love2003-05-19 16:03:45 +00007;; Keywords: xml, data
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +00008
9;; This file is part of GNU Emacs.
10
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000011;; GNU Emacs is free software: you can redistribute it and/or modify
47db06aa Gerd Moellmann2000-07-19 15:52:13 +000012;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000013;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000015
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000022;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000023
24;;; Commentary:
25
a98e819b
DL
Dave Love2003-05-19 16:03:45 +000026;; This file contains a somewhat incomplete non-validating XML parser. It
27;; parses a file, and returns a list that can be used internally by
a1dfa9a3 Stefan Monnier2004-04-16 22:46:26 +000028;; any other Lisp libraries.
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000029
30;;; FILE FORMAT
31
a98e819b
DL
Dave Love2003-05-19 16:03:45 +000032;; The document type declaration may either be ignored or (optionally)
33;; parsed, but currently the parsing will only accept element
a1dfa9a3 Stefan Monnier2004-04-16 22:46:26 +000034;; declarations. The XML file is assumed to be well-formed. In case
a98e819b
DL
Dave Love2003-05-19 16:03:45 +000035;; of error, the parsing stops and the XML file is shown where the
36;; parsing stopped.
47db06aa Gerd Moellmann2000-07-19 15:52:13 +000037;;
a98e819b Dave Love2003-05-19 16:03:45 +000038;; It also knows how to ignore comments and processing instructions.
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000039;;
40;; The XML file should have the following format:
653558a1
GM
Gerd Moellmann2000-12-22 12:20:46 +000041;; <node1 attr1="name1" attr2="name2" ...>value
42;; <node2 attr3="name3" attr4="name4">value2</node2>
43;; <node3 attr5="name5" attr6="name6">value3</node3>
47db06aa Gerd Moellmann2000-07-19 15:52:13 +000044;; </node1>
a1dfa9a3 Stefan Monnier2004-04-16 22:46:26 +000045;; Of course, the name of the nodes and attributes can be anything. There can
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000046;; be any number of attributes (or none), as well as any number of children
47;; below the nodes.
48;;
49;; There can be only top level node, but with any number of children below.
50
51;;; LIST FORMAT
52
c7f8d055
SM
Stefan Monnier2004-04-14 18:36:14 +000053;; The functions `xml-parse-file', `xml-parse-region' and
54;; `xml-parse-tag' return a list with the following format:
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000055;;
56;; xml-list ::= (node node ...)
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +000057;; node ::= (qname attribute-list . child_node_list)
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000058;; child_node_list ::= child_node child_node ...
59;; child_node ::= node | string
c7f8d055
SM
Stefan Monnier2004-04-14 18:36:14 +000060;; qname ::= (:namespace-uri . "name") | "name"
61;; attribute_list ::= ((qname . "value") (qname . "value") ...)
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000062;; | nil
63;; string ::= "..."
64;;
a98e819b
DL
Dave Love2003-05-19 16:03:45 +000065;; Some macros are provided to ease the parsing of this list.
66;; Whitespace is preserved. Fixme: There should be a tree-walker that
67;; can remove it.
47db06aa Gerd Moellmann2000-07-19 15:52:13 +000068
c7f8d055
SM
Stefan Monnier2004-04-14 18:36:14 +000069;; TODO:
70;; * xml:base, xml:space support
71;; * more complete DOCTYPE parsing
72;; * pi support
73
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +000074;;; Code:
75
f6fcdfff
CY
Chong Yidong2007-03-17 18:55:52 +000076;; Note that buffer-substring and match-string were formerly used in
77;; several places, because the -no-properties variants remove
78;; composition info. However, after some discussion on emacs-devel,
79;; the consensus was that the speed of the -no-properties variants was
80;; a worthwhile tradeoff especially since we're usually parsing files
81;; instead of hand-crafted XML.
a98e819b Dave Love2003-05-19 16:03:45 +000082
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +080083;;; Macros to parse the list
47db06aa Gerd Moellmann2000-07-19 15:52:13 +000084
f8ab034e
MH
Mark A. Hershberger2005-05-26 14:35:47 +000085(defconst xml-undefined-entity "?"
86 "What to substitute for undefined entities")
87
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +080088(defconst xml-default-ns '(("" . "")
89 ("xml" . "http://www.w3.org/XML/1998/namespace")
90 ("xmlns" . "http://www.w3.org/2000/xmlns/"))
91 "Alist mapping default XML namespaces to their URIs.")
92
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +000093(defvar xml-entity-alist
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +080094 '(("lt" . "&#60;")
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +000095 ("gt" . ">")
96 ("apos" . "'")
97 ("quot" . "\"")
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +080098 ("amp" . "&#38;"))
99 "Alist mapping XML entities to their replacement text.")
7f3fbd5d Chong Yidong2012-06-30 19:33:22 +0800100
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800101(defvar xml-entity-expansion-limit 20000
102 "The maximum size of entity reference expansions.
103If the size of the buffer increases by this many characters while
104expanding entity references in a segment of character data, the
105XML parser signals an error. Setting this to nil removes the
106limit (making the parser vulnerable to XML bombs).")
107
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800108(defvar xml-parameter-entity-alist nil
109 "Alist of defined XML parametric entities.")
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000110
111(defvar xml-sub-parser nil
7f3fbd5d Chong Yidong2012-06-30 19:33:22 +0800112 "Non-nil when the XML parser is parsing an XML fragment.")
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000113
114(defvar xml-validating-parser nil
115 "Set to non-nil to get validity checking.")
116
971489ea Stefan Monnier2001-12-14 22:12:30 +0000117(defsubst xml-node-name (node)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000118 "Return the tag associated with NODE.
a1dfa9a3
SM
Stefan Monnier2004-04-16 22:46:26 +0000119Without namespace-aware parsing, the tag is a symbol.
120
121With namespace-aware parsing, the tag is a cons of a string
122representing the uri of the namespace with the local name of the
123tag. For example,
124
125 <foo>
126
127would be represented by
128
049a0936
DE
David Engster2012-07-28 11:19:53 +0200129 '(\"\" . \"foo\").
130
131If you'd just like a plain symbol instead, use 'symbol-qnames in
132the PARSE-NS argument."
a1dfa9a3 Stefan Monnier2004-04-16 22:46:26 +0000133
971489ea Stefan Monnier2001-12-14 22:12:30 +0000134 (car node))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000135
971489ea Stefan Monnier2001-12-14 22:12:30 +0000136(defsubst xml-node-attributes (node)
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000137 "Return the list of attributes of NODE.
138The list can be nil."
971489ea Stefan Monnier2001-12-14 22:12:30 +0000139 (nth 1 node))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000140
971489ea Stefan Monnier2001-12-14 22:12:30 +0000141(defsubst xml-node-children (node)
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000142 "Return the list of children of NODE.
143This is a list of nodes, and it can be nil."
971489ea Stefan Monnier2001-12-14 22:12:30 +0000144 (cddr node))
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000145
146(defun xml-get-children (node child-name)
147 "Return the children of NODE whose tag is CHILD-NAME.
a1dfa9a3 Stefan Monnier2004-04-16 22:46:26 +0000148CHILD-NAME should match the value returned by `xml-node-name'."
971489ea
SM
Stefan Monnier2001-12-14 22:12:30 +0000149 (let ((match ()))
150 (dolist (child (xml-node-children node))
a1dfa9a3
SM
Stefan Monnier2004-04-16 22:46:26 +0000151 (if (and (listp child)
152 (equal (xml-node-name child) child-name))
153 (push child match)))
971489ea Stefan Monnier2001-12-14 22:12:30 +0000154 (nreverse match)))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000155
9bcd6a7e Eli Zaretskii2003-12-29 12:13:27 +0000156(defun xml-get-attribute-or-nil (node attribute)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000157 "Get from NODE the value of ATTRIBUTE.
a1dfa9a3 Stefan Monnier2004-04-16 22:46:26 +0000158Return nil if the attribute was not found.
9bcd6a7e
EZ
Eli Zaretskii2003-12-29 12:13:27 +0000159
160See also `xml-get-attribute'."
2e9bdf15 Stefan Monnier2004-03-02 21:45:06 +0000161 (cdr (assoc attribute (xml-node-attributes node))))
9bcd6a7e
EZ
Eli Zaretskii2003-12-29 12:13:27 +0000162
163(defsubst xml-get-attribute (node attribute)
164 "Get from NODE the value of ATTRIBUTE.
165An empty string is returned if the attribute was not found.
166
167See also `xml-get-attribute-or-nil'."
168 (or (xml-get-attribute-or-nil node attribute) ""))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000169
566df3fc Chong Yidong2012-07-05 00:14:05 +0800170;;; Regular expressions for XML components
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000171
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800172;; The following regexps are used as subexpressions in regexps that
173;; are `eval-when-compile'd for efficiency, so they must be defined at
174;; compile time.
b3218de1 Chong Yidong2012-06-30 22:21:24 +0800175(eval-and-compile
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800176
177;; [4] NameStartChar
178;; See the definition of word syntax in `xml-syntax-table'.
179(defconst xml-name-start-char-re (concat "[[:word:]:_]"))
180
181;; [4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7
182;; | [#x0300-#x036F] | [#x203F-#x2040]
183(defconst xml-name-char-re (concat "[-0-9.[:word:]:_·̀-ͯ‿-⁀]"))
184
185;; [5] Name ::= NameStartChar (NameChar)*
186(defconst xml-name-re (concat xml-name-start-char-re xml-name-char-re "*"))
187
188;; [6] Names ::= Name (#x20 Name)*
189(defconst xml-names-re (concat xml-name-re "\\(?: " xml-name-re "\\)*"))
190
191;; [7] Nmtoken ::= (NameChar)+
192(defconst xml-nmtoken-re (concat xml-name-char-re "+"))
193
194;; [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
195(defconst xml-nmtokens-re (concat xml-nmtoken-re "\\(?: " xml-name-re "\\)*"))
196
197;; [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
198(defconst xml-char-ref-re "\\(?:&#[0-9]+;\\|&#x[0-9a-fA-F]+;\\)")
199
200;; [68] EntityRef ::= '&' Name ';'
201(defconst xml-entity-ref (concat "&" xml-name-re ";"))
202
4d4ddaa7 Chong Yidong2013-01-23 14:25:50 +0800203(defconst xml-entity-or-char-ref-re (concat "&\\(?:#\\(x\\)?\\([0-9a-fA-F]+\\)\\|\\("
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800204 xml-name-re "\\)\\);"))
205
206;; [69] PEReference ::= '%' Name ';'
207(defconst xml-pe-reference-re (concat "%\\(" xml-name-re "\\);"))
208
209;; [67] Reference ::= EntityRef | CharRef
210(defconst xml-reference-re (concat "\\(?:" xml-entity-ref "\\|" xml-char-ref-re "\\)"))
211
212;; [10] AttValue ::= '"' ([^<&"] | Reference)* '"'
213;; | "'" ([^<&'] | Reference)* "'"
214(defconst xml-att-value-re (concat "\\(?:\"\\(?:[^&\"]\\|"
215 xml-reference-re "\\)*\"\\|"
216 "'\\(?:[^&']\\|" xml-reference-re
217 "\\)*'\\)"))
218
219;; [56] TokenizedType ::= 'ID'
220;; [VC: ID] [VC: One ID / Element Type] [VC: ID Attribute Default]
221;; | 'IDREF' [VC: IDREF]
222;; | 'IDREFS' [VC: IDREF]
223;; | 'ENTITY' [VC: Entity Name]
224;; | 'ENTITIES' [VC: Entity Name]
225;; | 'NMTOKEN' [VC: Name Token]
226;; | 'NMTOKENS' [VC: Name Token]
227(defconst xml-tokenized-type-re (concat "\\(?:ID\\|IDREF\\|IDREFS\\|ENTITY\\|"
228 "ENTITIES\\|NMTOKEN\\|NMTOKENS\\)"))
229
230;; [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
231(defconst xml-notation-type-re
232 (concat "\\(?:NOTATION\\s-+(\\s-*" xml-name-re
233 "\\(?:\\s-*|\\s-*" xml-name-re "\\)*\\s-*)\\)"))
234
235;; [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
236;; [VC: Enumeration] [VC: No Duplicate Tokens]
237(defconst xml-enumeration-re (concat "\\(?:(\\s-*" xml-nmtoken-re
238 "\\(?:\\s-*|\\s-*" xml-nmtoken-re
239 "\\)*\\s-+)\\)"))
240
241;; [57] EnumeratedType ::= NotationType | Enumeration
242(defconst xml-enumerated-type-re (concat "\\(?:" xml-notation-type-re
243 "\\|" xml-enumeration-re "\\)"))
244
245;; [54] AttType ::= StringType | TokenizedType | EnumeratedType
246;; [55] StringType ::= 'CDATA'
247(defconst xml-att-type-re (concat "\\(?:CDATA\\|" xml-tokenized-type-re
248 "\\|" xml-notation-type-re
249 "\\|" xml-enumerated-type-re "\\)"))
250
251;; [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
252(defconst xml-default-decl-re (concat "\\(?:#REQUIRED\\|#IMPLIED\\|"
253 "\\(?:#FIXED\\s-+\\)*"
254 xml-att-value-re "\\)"))
255
256;; [53] AttDef ::= S Name S AttType S DefaultDecl
257(defconst xml-att-def-re (concat "\\(?:\\s-*" xml-name-re
258 "\\s-*" xml-att-type-re
259 "\\s-*" xml-default-decl-re "\\)"))
260
261;; [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"'
262;; | "'" ([^%&'] | PEReference | Reference)* "'"
263(defconst xml-entity-value-re (concat "\\(?:\"\\(?:[^%&\"]\\|"
264 xml-pe-reference-re
265 "\\|" xml-reference-re
266 "\\)*\"\\|'\\(?:[^%&']\\|"
267 xml-pe-reference-re "\\|"
268 xml-reference-re "\\)*'\\)"))
269) ; End of `eval-when-compile'
270
b3218de1
CY
Chong Yidong2012-06-30 22:21:24 +0800271
272;; [75] ExternalID ::= 'SYSTEM' S SystemLiteral
273;; | 'PUBLIC' S PubidLiteral S SystemLiteral
274;; [76] NDataDecl ::= S 'NDATA' S
275;; [73] EntityDef ::= EntityValue| (ExternalID NDataDecl?)
276;; [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
277;; [74] PEDef ::= EntityValue | ExternalID
278;; [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
279;; [70] EntityDecl ::= GEDecl | PEDecl
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000280
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000281;; Note that this is setup so that we can do whitespace-skipping with
282;; `(skip-syntax-forward " ")', inter alia. Previously this was slow
566df3fc Chong Yidong2012-07-05 00:14:05 +0800283;; compared with `re-search-forward', but that has been fixed.
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000284
285(defvar xml-syntax-table
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800286 ;; By default, characters have symbol syntax.
287 (let ((table (make-char-table 'syntax-table '(3))))
288 ;; The XML space chars [3], and nothing else, have space syntax.
289 (dolist (c '(?\s ?\t ?\r ?\n))
a98e819b Dave Love2003-05-19 16:03:45 +0000290 (modify-syntax-entry c " " table))
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800291 ;; The characters in NameStartChar [4], aside from ':' and '_',
292 ;; have word syntax. This is used by `xml-name-start-char-re'.
293 (modify-syntax-entry '(?A . ?Z) "w" table)
294 (modify-syntax-entry '(?a . ?z) "w" table)
295 (modify-syntax-entry '(#xC0 . #xD6) "w" table)
296 (modify-syntax-entry '(#xD8 . #XF6) "w" table)
297 (modify-syntax-entry '(#xF8 . #X2FF) "w" table)
298 (modify-syntax-entry '(#x370 . #X37D) "w" table)
299 (modify-syntax-entry '(#x37F . #x1FFF) "w" table)
300 (modify-syntax-entry '(#x200C . #x200D) "w" table)
301 (modify-syntax-entry '(#x2070 . #x218F) "w" table)
302 (modify-syntax-entry '(#x2C00 . #x2FEF) "w" table)
303 (modify-syntax-entry '(#x3001 . #xD7FF) "w" table)
304 (modify-syntax-entry '(#xF900 . #xFDCF) "w" table)
305 (modify-syntax-entry '(#xFDF0 . #xFFFD) "w" table)
306 (modify-syntax-entry '(#x10000 . #xEFFFF) "w" table)
a98e819b Dave Love2003-05-19 16:03:45 +0000307 table)
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800308 "Syntax table used by the XML parser.
309In this syntax table, the XML space characters [ \\t\\r\\n], and
310only those characters, have whitespace syntax.")
a98e819b Dave Love2003-05-19 16:03:45 +0000311
566df3fc Chong Yidong2012-07-05 00:14:05 +0800312;;; Entry points:
a98e819b Dave Love2003-05-19 16:03:45 +0000313
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800314;;;###autoload
315(defun xml-parse-file (file &optional parse-dtd parse-ns)
316 "Parse the well-formed XML file FILE.
317Return the top node with all its children.
318If PARSE-DTD is non-nil, the DTD is parsed rather than skipped.
049a0936
DE
David Engster2012-07-28 11:19:53 +0200319
320If PARSE-NS is non-nil, then QNAMES are expanded. By default,
321the variable `xml-default-ns' is the mapping from namespaces to
322URIs, and expanded names will be returned as a cons
323
324 (\"namespace:\" . \"foo\").
325
326If PARSE-NS is an alist, it will be used as the mapping from
327namespace to URIs instead.
328
760701eb
PE
Paul Eggert2015-08-20 17:33:48 -0700329If it is the symbol `symbol-qnames', expanded names will be
330returned as a plain symbol `namespace:foo' instead of a cons.
049a0936
DE
David Engster2012-07-28 11:19:53 +0200331
332Both features can be combined by providing a cons cell
333
334 (symbol-qnames . ALIST)."
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800335 (with-temp-buffer
336 (insert-file-contents file)
337 (xml--parse-buffer parse-dtd parse-ns)))
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000338
339;;;###autoload
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800340(defun xml-parse-region (&optional beg end buffer parse-dtd parse-ns)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000341 "Parse the region from BEG to END in BUFFER.
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800342Return the XML parse tree, or raise an error if the region does
343not contain well-formed XML.
344
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800345If BEG is nil, it defaults to `point-min'.
346If END is nil, it defaults to `point-max'.
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000347If BUFFER is nil, it defaults to the current buffer.
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800348If PARSE-DTD is non-nil, parse the DTD and return it as the first
349element of the list.
049a0936
DE
David Engster2012-07-28 11:19:53 +0200350If PARSE-NS is non-nil, then QNAMES are expanded. By default,
351the variable `xml-default-ns' is the mapping from namespaces to
352URIs, and expanded names will be returned as a cons
353
354 (\"namespace:\" . \"foo\").
355
356If PARSE-NS is an alist, it will be used as the mapping from
357namespace to URIs instead.
358
760701eb
PE
Paul Eggert2015-08-20 17:33:48 -0700359If it is the symbol `symbol-qnames', expanded names will be
360returned as a plain symbol `namespace:foo' instead of a cons.
049a0936
DE
David Engster2012-07-28 11:19:53 +0200361
362Both features can be combined by providing a cons cell
363
364 (symbol-qnames . ALIST)."
39d58fc0
MH
Mark A. Hershberger2006-02-02 01:07:11 +0000365 ;; Use fixed syntax table to ensure regexp char classes and syntax
366 ;; specs DTRT.
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800367 (unless buffer
368 (setq buffer (current-buffer)))
369 (with-temp-buffer
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800370 (insert-buffer-substring-no-properties buffer beg end)
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800371 (xml--parse-buffer parse-dtd parse-ns)))
372
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800373;; XML [5]
374
375;; Fixme: This needs re-writing to deal with the XML grammar properly, i.e.
376;; document ::= prolog element Misc*
377;; prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
378
fbf2e7ad Chong Yidong2012-07-01 15:17:05 +0800379(defun xml--parse-buffer (parse-dtd parse-ns)
566df3fc Chong Yidong2012-07-05 00:14:05 +0800380 (with-syntax-table xml-syntax-table
39d58fc0 Mark A. Hershberger2006-02-02 01:07:11 +0000381 (let ((case-fold-search nil) ; XML is case-sensitive.
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800382 ;; Prevent entity definitions from changing the defaults
383 (xml-entity-alist xml-entity-alist)
384 (xml-parameter-entity-alist xml-parameter-entity-alist)
39d58fc0 Mark A. Hershberger2006-02-02 01:07:11 +0000385 xml result dtd)
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800386 (goto-char (point-min))
387 (while (not (eobp))
388 (if (search-forward "<" nil t)
389 (progn
390 (forward-char -1)
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800391 (setq result (xml-parse-tag-1 parse-dtd parse-ns))
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800392 (cond
393 ((null result)
394 ;; Not looking at an xml start tag.
395 (unless (eobp)
396 (forward-char 1)))
397 ((and xml (not xml-sub-parser))
398 ;; Translation of rule [1] of XML specifications
399 (error "XML: (Not Well-Formed) Only one root tag allowed"))
400 ((and (listp (car result))
401 parse-dtd)
402 (setq dtd (car result))
403 (if (cdr result) ; possible leading comment
404 (add-to-list 'xml (cdr result))))
405 (t
406 (add-to-list 'xml result))))
407 (goto-char (point-max))))
408 (if parse-dtd
409 (cons dtd (nreverse xml))
410 (nreverse xml)))))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000411
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +0000412(defun xml-maybe-do-ns (name default xml-ns)
a1dfa9a3
SM
Stefan Monnier2004-04-16 22:46:26 +0000413 "Perform any namespace expansion.
414NAME is the name to perform the expansion on.
c7f8d055
SM
Stefan Monnier2004-04-14 18:36:14 +0000415DEFAULT is the default namespace. XML-NS is a cons of namespace
416names to uris. When namespace-aware parsing is off, then XML-NS
417is nil.
418
419During namespace-aware parsing, any name without a namespace is
420put into the namespace identified by DEFAULT. nil is used to
049a0936
DE
David Engster2012-07-28 11:19:53 +0200421specify that the name shouldn't be given a namespace.
422Expanded names will by default be returned as a cons. If you
423would like to get plain symbols instead, provide a cons cell
424
425 (symbol-qnames . ALIST)
426
427in the XML-NS argument."
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +0000428 (if (consp xml-ns)
049a0936
DE
David Engster2012-07-28 11:19:53 +0200429 (let* ((symbol-qnames (eq (car-safe xml-ns) 'symbol-qnames))
430 (nsp (string-match ":" name))
c7f8d055
SM
Stefan Monnier2004-04-14 18:36:14 +0000431 (lname (if nsp (substring name (match-end 0)) name))
432 (prefix (if nsp (substring name 0 (match-beginning 0)) default))
433 (special (and (string-equal lname "xmlns") (not prefix)))
434 ;; Setting default to nil will insure that there is not
435 ;; matching cons in xml-ns. In which case we
436 (ns (or (cdr (assoc (if special "xmlns" prefix)
049a0936 David Engster2012-07-28 11:19:53 +0200437 (if symbol-qnames (cdr xml-ns) xml-ns)))
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000438 "")))
049a0936
DE
David Engster2012-07-28 11:19:53 +0200439 (if (and symbol-qnames
440 (not (string= prefix "xmlns")))
441 (intern (concat ns lname))
442 (cons ns (if special "" lname))))
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +0000443 (intern name)))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000444
2d42509a Juanma Barranquero2003-07-14 20:45:43 +0000445(defun xml-parse-tag (&optional parse-dtd parse-ns)
a98e819b Dave Love2003-05-19 16:03:45 +0000446 "Parse the tag at point.
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000447If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and
448returned as the first element in the list.
049a0936
DE
David Engster2012-07-28 11:19:53 +0200449If PARSE-NS is non-nil, expand QNAMES; for further details, see
450`xml-parse-region'.
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800451
452Return one of:
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000453 - a list : the matching node
454 - nil : the point is not looking at a tag.
455 - a pair : the first element is the DTD, the second is the node."
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800456 (let* ((case-fold-search nil)
457 ;; Prevent entity definitions from changing the defaults
458 (xml-entity-alist xml-entity-alist)
459 (xml-parameter-entity-alist xml-parameter-entity-alist)
460 (buf (current-buffer))
461 (pos (point)))
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800462 (with-temp-buffer
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800463 (with-syntax-table xml-syntax-table
464 (insert-buffer-substring-no-properties buf pos)
465 (goto-char (point-min))
466 (xml-parse-tag-1 parse-dtd parse-ns)))))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800467
468(defun xml-parse-tag-1 (&optional parse-dtd parse-ns)
469 "Like `xml-parse-tag', but possibly modify the buffer while working."
049a0936
DE
David Engster2012-07-28 11:19:53 +0200470 (let* ((xml-validating-parser (or parse-dtd xml-validating-parser))
471 (xml-ns
472 (cond ((eq parse-ns 'symbol-qnames)
473 (cons 'symbol-qnames xml-default-ns))
474 ((or (consp (car-safe parse-ns))
475 (and (eq (car-safe parse-ns) 'symbol-qnames)
476 (listp (cdr parse-ns))))
477 parse-ns)
478 (parse-ns
479 xml-default-ns))))
2d42509a Juanma Barranquero2003-07-14 20:45:43 +0000480 (cond
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800481 ;; Processing instructions, like <?xml version="1.0"?>.
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200482 ((looking-at-p "<\\?")
2d42509a
JB
Juanma Barranquero2003-07-14 20:45:43 +0000483 (search-forward "?>")
484 (skip-syntax-forward " ")
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800485 (xml-parse-tag-1 parse-dtd xml-ns))
486 ;; Character data (CDATA) sections, in which no tag should be interpreted
2d42509a
JB
Juanma Barranquero2003-07-14 20:45:43 +0000487 ((looking-at "<!\\[CDATA\\[")
488 (let ((pos (match-end 0)))
489 (unless (search-forward "]]>" nil t)
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000490 (error "XML: (Not Well Formed) CDATA section does not end anywhere in the document"))
ae026110 Mark A. Hershberger2004-12-01 04:45:08 +0000491 (concat
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000492 (buffer-substring-no-properties pos (match-beginning 0))
ae026110 Mark A. Hershberger2004-12-01 04:45:08 +0000493 (xml-parse-string))))
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800494 ;; DTD for the document
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200495 ((looking-at-p "<!DOCTYPE[ \t\n\r]")
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000496 (let ((dtd (xml-parse-dtd parse-ns)))
497 (skip-syntax-forward " ")
498 (if xml-validating-parser
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800499 (cons dtd (xml-parse-tag-1 nil xml-ns))
500 (xml-parse-tag-1 nil xml-ns))))
501 ;; skip comments
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200502 ((looking-at-p "<!--")
2d42509a Juanma Barranquero2003-07-14 20:45:43 +0000503 (search-forward "-->")
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800504 ;; FIXME: This loses the skipped-over spaces.
a268160b Alex Harsanyi2012-01-27 16:46:10 +0800505 (skip-syntax-forward " ")
18edb22d Chong Yidong2012-04-21 17:53:37 +0800506 (unless (eobp)
772b2e2c Chong Yidong2012-06-24 23:06:24 +0800507 (let ((xml-sub-parser t))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800508 (xml-parse-tag-1 parse-dtd xml-ns))))
509 ;; end tag
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200510 ((looking-at-p "</")
2d42509a Juanma Barranquero2003-07-14 20:45:43 +0000511 '())
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800512 ;; opening tag
513 ((looking-at (eval-when-compile (concat "<\\(" xml-name-re "\\)")))
2d42509a Juanma Barranquero2003-07-14 20:45:43 +0000514 (goto-char (match-end 1))
34638996 Eli Zaretskii2003-11-01 17:56:08 +0000515 ;; Parse this node
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000516 (let* ((node-name (match-string-no-properties 1))
5178753d
MH
Mark A. Hershberger2006-02-02 01:02:31 +0000517 ;; Parse the attribute list.
518 (attrs (xml-parse-attlist xml-ns))
06b60517 Juanma Barranquero2011-04-19 15:44:55 +0200519 children)
5178753d
MH
Mark A. Hershberger2006-02-02 01:02:31 +0000520 ;; add the xmlns:* attrs to our cache
521 (when (consp xml-ns)
c7f8d055
SM
Stefan Monnier2004-04-14 18:36:14 +0000522 (dolist (attr attrs)
523 (when (and (consp (car attr))
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000524 (equal "http://www.w3.org/2000/xmlns/"
525 (caar attr)))
526 (push (cons (cdar attr) (cdr attr))
049a0936
DE
David Engster2012-07-28 11:19:53 +0200527 (if (symbolp (car xml-ns))
528 (cdr xml-ns)
529 xml-ns)))))
5178753d Mark A. Hershberger2006-02-02 01:02:31 +0000530 (setq children (list attrs (xml-maybe-do-ns node-name "" xml-ns)))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800531 (cond
532 ;; is this an empty element ?
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200533 ((looking-at-p "/>")
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800534 (forward-char 2)
535 (nreverse children))
536 ;; is this a valid start tag ?
537 ((eq (char-after) ?>)
538 (forward-char 1)
539 ;; Now check that we have the right end-tag.
540 (let ((end (concat "</" node-name "\\s-*>")))
01f1a9ab Juanma Barranquero2013-08-12 17:11:35 +0200541 (while (not (looking-at end))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800542 (cond
543 ((eobp)
a76e6535 Chong Yidong2012-07-03 13:28:42 +0800544 (error "XML: (Not Well-Formed) End of document while reading element `%s'"
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800545 node-name))
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200546 ((looking-at-p "</")
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800547 (forward-char 2)
548 (error "XML: (Not Well-Formed) Invalid end tag `%s' (expecting `%s')"
549 (let ((pos (point)))
550 (buffer-substring pos (if (re-search-forward "\\s-*>" nil t)
551 (match-beginning 0)
552 (point-max))))
553 node-name))
554 ;; Read a sub-element and push it onto CHILDREN.
555 ((= (char-after) ?<)
556 (let ((tag (xml-parse-tag-1 nil xml-ns)))
557 (when tag
558 (push tag children))))
559 ;; Read some character data.
560 (t
561 (let ((expansion (xml-parse-string)))
562 (push (if (stringp (car children))
563 ;; If two strings were separated by a
564 ;; comment, concat them.
565 (concat (pop children) expansion)
566 expansion)
567 children)))))
568 ;; Move point past the end-tag.
569 (goto-char (match-end 0))
570 (nreverse children)))
571 ;; Otherwise this was an invalid start tag (expected ">" not found.)
572 (t
573 (error "XML: (Well-Formed) Couldn't parse tag: %s"
574 (buffer-substring-no-properties (- (point) 10) (+ (point) 1)))))))
575
576 ;; (Not one of PI, CDATA, Comment, End tag, or Start tag)
577 (t
578 (unless xml-sub-parser ; Usually, we error out.
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000579 (error "XML: (Well-Formed) Invalid character"))
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000580 ;; However, if we're parsing incrementally, then we need to deal
581 ;; with stray CDATA.
582 (xml-parse-string)))))
583
584(defun xml-parse-string ()
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800585 "Parse character data at point, and return it as a string.
586Leave point at the start of the next thing to parse. This
587function can modify the buffer by expanding entity and character
588references."
589 (let ((start (point))
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800590 ;; Keep track of the size of the rest of the buffer:
591 (old-remaining-size (- (buffer-size) (point)))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800592 ref val)
593 (while (and (not (eobp))
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200594 (not (looking-at-p "<")))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800595 ;; Find the next < or & character.
596 (skip-chars-forward "^<&")
597 (when (eq (char-after) ?&)
598 ;; If we find an entity or character reference, expand it.
566df3fc Chong Yidong2012-07-05 00:14:05 +0800599 (unless (looking-at xml-entity-or-char-ref-re)
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800600 (error "XML: (Not Well-Formed) Invalid entity reference"))
601 ;; For a character reference, the next entity or character
602 ;; reference must be after the replacement. [4.6] "Numerical
603 ;; character references are expanded immediately when
604 ;; recognized and MUST be treated as character data."
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800605 (if (setq ref (match-string 2))
606 (progn ; Numeric char reference
607 (setq val (save-match-data
608 (decode-char 'ucs (string-to-number
609 ref (if (match-string 1) 16)))))
610 (and (null val)
611 xml-validating-parser
612 (error "XML: (Validity) Invalid character reference `%s'"
613 (match-string 0)))
a1d23eb5 Chong Yidong2013-02-13 15:24:11 +0800614 (replace-match (if val (string val) xml-undefined-entity) t t))
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800615 ;; For an entity reference, search again from the start of
616 ;; the replaced text, since the replacement can contain
617 ;; entity or character references, or markup.
618 (setq ref (match-string 3)
619 val (assoc ref xml-entity-alist))
620 (and (null val)
621 xml-validating-parser
622 (error "XML: (Validity) Undefined entity `%s'" ref))
a1d23eb5 Chong Yidong2013-02-13 15:24:11 +0800623 (replace-match (or (cdr val) xml-undefined-entity) t t)
566df3fc Chong Yidong2012-07-05 00:14:05 +0800624 (goto-char (match-beginning 0)))
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800625 ;; Check for XML bombs.
626 (and xml-entity-expansion-limit
627 (> (- (buffer-size) (point))
628 (+ old-remaining-size xml-entity-expansion-limit))
629 (error "XML: Entity reference expansion \
630surpassed `xml-entity-expansion-limit'"))))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800631 ;; [2.11] Clean up line breaks.
632 (let ((end-marker (point-marker)))
633 (goto-char start)
634 (while (re-search-forward "\r\n?" end-marker t)
635 (replace-match "\n" t t))
636 (goto-char end-marker)
637 (buffer-substring start (point)))))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000638
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +0000639(defun xml-parse-attlist (&optional xml-ns)
a1dfa9a3
SM
Stefan Monnier2004-04-16 22:46:26 +0000640 "Return the attribute-list after point.
641Leave point at the first non-blank character after the tag."
971489ea Stefan Monnier2001-12-14 22:12:30 +0000642 (let ((attlist ())
34638996 Eli Zaretskii2003-11-01 17:56:08 +0000643 end-pos name)
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000644 (skip-syntax-forward " ")
645 (while (looking-at (eval-when-compile
30eabd7a Stefan Monnier2012-07-03 23:31:34 -0400646 (concat "\\(" xml-name-re "\\)\\s-*=\\s-*")))
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +0000647 (setq end-pos (match-end 0))
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000648 (setq name (xml-maybe-do-ns (match-string-no-properties 1) nil xml-ns))
c7f8d055 Stefan Monnier2004-04-14 18:36:14 +0000649 (goto-char end-pos)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000650
a158ff81
JB
Juanma Barranquero2003-03-16 10:48:34 +0000651 ;; See also: http://www.w3.org/TR/2000/REC-xml-20001006#AVNormalize
652
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000653 ;; Do we have a string between quotes (or double-quotes),
654 ;; or a simple word ?
a158ff81 Juanma Barranquero2003-03-16 10:48:34 +0000655 (if (looking-at "\"\\([^\"]*\\)\"")
34638996 Eli Zaretskii2003-11-01 17:56:08 +0000656 (setq end-pos (match-end 0))
f0ec1711 Juanma Barranquero2003-03-20 18:01:58 +0000657 (if (looking-at "'\\([^']*\\)'")
34638996 Eli Zaretskii2003-11-01 17:56:08 +0000658 (setq end-pos (match-end 0))
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000659 (error "XML: (Not Well-Formed) Attribute values must be given between quotes")))
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000660
661 ;; Each attribute must be unique within a given element
662 (if (assoc name attlist)
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000663 (error "XML: (Not Well-Formed) Each attribute must be unique within an element"))
524425ae Thien-Thi Nguyen2003-01-04 09:32:17 +0000664
a158ff81
JB
Juanma Barranquero2003-03-16 10:48:34 +0000665 ;; Multiple whitespace characters should be replaced with a single one
666 ;; in the attributes
06b60517 Juanma Barranquero2011-04-19 15:44:55 +0200667 (let ((string (match-string-no-properties 1)))
a98e819b Dave Love2003-05-19 16:03:45 +0000668 (replace-regexp-in-string "\\s-\\{2,\\}" " " string)
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000669 (let ((expansion (xml-substitute-special string)))
670 (unless (stringp expansion)
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800671 ;; We say this is the constraint. It is actually that
672 ;; neither external entities nor "<" can be in an
673 ;; attribute value.
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000674 (error "XML: (Not Well-Formed) Entities in attributes cannot expand into elements"))
675 (push (cons name expansion) attlist)))
a158ff81 Juanma Barranquero2003-03-16 10:48:34 +0000676
34638996 Eli Zaretskii2003-11-01 17:56:08 +0000677 (goto-char end-pos)
a98e819b Dave Love2003-05-19 16:03:45 +0000678 (skip-syntax-forward " "))
971489ea Stefan Monnier2001-12-14 22:12:30 +0000679 (nreverse attlist)))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000680
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800681;;; DTD (document type declaration)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000682
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800683;; The following functions know how to skip or parse the DTD of a
684;; document. FIXME: it fails at least if the DTD contains conditional
685;; sections.
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000686
687(defun xml-skip-dtd ()
688 "Skip the DTD at point.
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000689This follows the rule [28] in the XML specifications."
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000690 (let ((xml-validating-parser nil))
691 (xml-parse-dtd)))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000692
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200693(defun xml-parse-dtd (&optional _parse-ns)
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000694 "Parse the DTD at point."
695 (forward-char (eval-when-compile (length "<!DOCTYPE")))
696 (skip-syntax-forward " ")
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200697 (if (and (looking-at-p ">")
6d12a4df
MH
Mark A. Hershberger2004-07-09 14:22:33 +0000698 xml-validating-parser)
699 (error "XML: (Validity) Invalid DTD (expecting name of the document)"))
524425ae Thien-Thi Nguyen2003-01-04 09:32:17 +0000700
971489ea Stefan Monnier2001-12-14 22:12:30 +0000701 ;; Get the name of the document
30eabd7a Stefan Monnier2012-07-03 23:31:34 -0400702 (looking-at xml-name-re)
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000703 (let ((dtd (list (match-string-no-properties 0) 'dtd))
fbf2e7ad Chong Yidong2012-07-01 15:17:05 +0800704 (xml-parameter-entity-alist xml-parameter-entity-alist)
fbf2e7ad Chong Yidong2012-07-01 15:17:05 +0800705 next-parameter-entity)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000706 (goto-char (match-end 0))
a98e819b Dave Love2003-05-19 16:03:45 +0000707 (skip-syntax-forward " ")
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800708
709 ;; External subset (XML [75])
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000710 (cond ((looking-at "PUBLIC\\s-+")
711 (goto-char (match-end 0))
712 (unless (or (re-search-forward
713 "\\=\"\\([[:space:][:alnum:]-'()+,./:=?;!*#@$_%]*\\)\""
714 nil t)
715 (re-search-forward
716 "\\='\\([[:space:][:alnum:]-()+,./:=?;!*#@$_%]*\\)'"
717 nil t))
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000718 (error "XML: Missing Public ID"))
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000719 (let ((pubid (match-string-no-properties 1)))
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000720 (skip-syntax-forward " ")
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000721 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t)
722 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t))
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000723 (error "XML: Missing System ID"))
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000724 (push (list pubid (match-string-no-properties 1) 'public) dtd)))
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000725 ((looking-at "SYSTEM\\s-+")
726 (goto-char (match-end 0))
727 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t)
728 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t))
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000729 (error "XML: Missing System ID"))
f6fcdfff Chong Yidong2007-03-17 18:55:52 +0000730 (push (list (match-string-no-properties 1) 'system) dtd)))
a98e819b Dave Love2003-05-19 16:03:45 +0000731 (skip-syntax-forward " ")
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800732
733 (if (eq (char-after) ?>)
734
735 ;; No internal subset
a98e819b Dave Love2003-05-19 16:03:45 +0000736 (forward-char)
a98e819b Dave Love2003-05-19 16:03:45 +0000737
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800738 ;; Internal subset (XML [28b])
739 (unless (eq (char-after) ?\[)
740 (error "XML: Bad DTD"))
741 (forward-char)
742
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800743 ;; [2.8]: "markup declarations may be made up in whole or in
744 ;; part of the replacement text of parameter entities."
745
746 ;; Since parameter entities are valid only within the DTD, we
747 ;; first search for the position of the next possible parameter
748 ;; entity. Then, search for the next DTD element; if it ends
749 ;; before the next parameter entity, expand the parameter entity
750 ;; and try again.
751 (setq next-parameter-entity
752 (save-excursion
566df3fc Chong Yidong2012-07-05 00:14:05 +0800753 (if (re-search-forward xml-pe-reference-re nil t)
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800754 (match-beginning 0))))
755
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800756 ;; Parse the rest of the DTD
757 ;; Fixme: Deal with NOTATION, PIs.
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200758 (while (not (looking-at-p "\\s-*\\]"))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800759 (skip-syntax-forward " ")
760 (cond
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800761 ((eobp)
762 (error "XML: (Well-Formed) End of document while reading DTD"))
7f3fbd5d Chong Yidong2012-06-30 19:33:22 +0800763 ;; Element declaration [45]:
6fe566a7
CY
Chong Yidong2012-07-01 19:05:17 +0800764 ((and (looking-at (eval-when-compile
765 (concat "<!ELEMENT\\s-+\\(" xml-name-re
766 "\\)\\s-+\\([^>]+\\)>")))
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800767 (or (null next-parameter-entity)
768 (<= (match-end 0) next-parameter-entity)))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800769 (let ((element (match-string-no-properties 1))
770 (type (match-string-no-properties 2))
771 (end-pos (match-end 0)))
772 ;; Translation of rule [46] of XML specifications
a98e819b Dave Love2003-05-19 16:03:45 +0000773 (cond
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200774 ((string-match-p "\\`EMPTY\\s-*\\'" type) ; empty declaration
a98e819b Dave Love2003-05-19 16:03:45 +0000775 (setq type 'empty))
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200776 ((string-match-p "\\`ANY\\s-*$" type) ; any type of contents
a98e819b Dave Love2003-05-19 16:03:45 +0000777 (setq type 'any))
6fe566a7
CY
Chong Yidong2012-07-01 19:05:17 +0800778 ((string-match "\\`(\\(.*\\))\\s-*\\'" type) ; children ([47])
779 (setq type (xml-parse-elem-type
780 (match-string-no-properties 1 type))))
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200781 ((string-match-p "^%[^;]+;[ \t\n\r]*\\'" type) ; substitution
a98e819b Dave Love2003-05-19 16:03:45 +0000782 nil)
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800783 (xml-validating-parser
784 (error "XML: (Validity) Invalid element type in the DTD")))
27720433 Mark A. Hershberger2004-12-02 07:19:03 +0000785
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800786 ;; rule [45]: the element declaration must be unique
787 (and (assoc element dtd)
788 xml-validating-parser
789 (error "XML: (Validity) DTD element declarations must be unique (<%s>)"
790 element))
a98e819b
DL
Dave Love2003-05-19 16:03:45 +0000791
792 ;; Store the element in the DTD
793 (push (list element type) dtd)
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800794 (goto-char end-pos)))
795
796 ;; Attribute-list declaration [52] (currently unsupported):
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800797 ((and (looking-at (eval-when-compile
798 (concat "<!ATTLIST[ \t\n\r]*\\(" xml-name-re
799 "\\)[ \t\n\r]*\\(" xml-att-def-re
800 "\\)*[ \t\n\r]*>")))
801 (or (null next-parameter-entity)
802 (<= (match-end 0) next-parameter-entity)))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800803 (goto-char (match-end 0)))
804
fbf2e7ad Chong Yidong2012-07-01 15:17:05 +0800805 ;; Comments (skip to end, ignoring parameter entity):
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200806 ((looking-at-p "<!--")
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800807 (search-forward "-->")
808 (and next-parameter-entity
809 (> (point) next-parameter-entity)
810 (setq next-parameter-entity
811 (save-excursion
566df3fc Chong Yidong2012-07-05 00:14:05 +0800812 (if (re-search-forward xml-pe-reference-re nil t)
fbf2e7ad Chong Yidong2012-07-01 15:17:05 +0800813 (match-beginning 0))))))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800814
815 ;; Internal entity declarations:
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800816 ((and (looking-at (eval-when-compile
817 (concat "<!ENTITY[ \t\n\r]+\\(%[ \t\n\r]+\\)?\\("
818 xml-name-re "\\)[ \t\n\r]*\\("
819 xml-entity-value-re "\\)[ \t\n\r]*>")))
820 (or (null next-parameter-entity)
821 (<= (match-end 0) next-parameter-entity)))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800822 (let* ((name (prog1 (match-string-no-properties 2)
823 (goto-char (match-end 0))))
824 (alist (if (match-string 1)
825 'xml-parameter-entity-alist
826 'xml-entity-alist))
827 ;; Retrieve the deplacement text:
828 (value (xml--entity-replacement-text
829 ;; Entity value, sans quotation marks:
830 (substring (match-string-no-properties 3) 1 -1))))
831 ;; If the same entity is declared more than once, the
832 ;; first declaration is binding.
833 (unless (assoc name (symbol-value alist))
834 (set alist (cons (cons name value) (symbol-value alist))))))
835
836 ;; External entity declarations (currently unsupported):
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800837 ((and (or (looking-at (eval-when-compile
838 (concat "<!ENTITY[ \t\n\r]+\\(%[ \t\n\r]+\\)?\\("
839 xml-name-re "\\)[ \t\n\r]+SYSTEM[ \t\n\r]+"
840 "\\(\"[^\"]*\"\\|'[^']*'\\)[ \t\n\r]*>")))
841 (looking-at (eval-when-compile
842 (concat "<!ENTITY[ \t\n\r]+\\(%[ \t\n\r]+\\)?\\("
843 xml-name-re "\\)[ \t\n\r]+PUBLIC[ \t\n\r]+"
844 "\"[- \r\na-zA-Z0-9'()+,./:=?;!*#@$_%]*\""
845 "\\|'[- \r\na-zA-Z0-9()+,./:=?;!*#@$_%]*'"
846 "[ \t\n\r]+\\(\"[^\"]*\"\\|'[^']*'\\)"
847 "[ \t\n\r]*>"))))
848 (or (null next-parameter-entity)
849 (<= (match-end 0) next-parameter-entity)))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800850 (goto-char (match-end 0)))
851
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800852 ;; If a parameter entity is in the way, expand it.
853 (next-parameter-entity
854 (save-excursion
855 (goto-char next-parameter-entity)
566df3fc Chong Yidong2012-07-05 00:14:05 +0800856 (unless (looking-at xml-pe-reference-re)
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800857 (error "XML: Internal error"))
858 (let* ((entity (match-string 1))
fbf2e7ad
CY
Chong Yidong2012-07-01 15:17:05 +0800859 (elt (assoc entity xml-parameter-entity-alist)))
860 (if elt
861 (progn
862 (replace-match (cdr elt) t t)
863 ;; The replacement can itself be a parameter entity.
864 (goto-char next-parameter-entity))
865 (goto-char (match-end 0))))
866 (setq next-parameter-entity
566df3fc Chong Yidong2012-07-05 00:14:05 +0800867 (if (re-search-forward xml-pe-reference-re nil t)
fbf2e7ad Chong Yidong2012-07-01 15:17:05 +0800868 (match-beginning 0)))))
7f3fbd5d Chong Yidong2012-06-30 19:33:22 +0800869
a76e6535 Chong Yidong2012-07-03 13:28:42 +0800870 ;; Anything else is garbage (ignored if not validating).
7f3fbd5d Chong Yidong2012-06-30 19:33:22 +0800871 (xml-validating-parser
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800872 (error "XML: (Validity) Invalid DTD item"))
873 (t
874 (skip-chars-forward "^]"))))
7f3fbd5d Chong Yidong2012-06-30 19:33:22 +0800875
6d12a4df Mark A. Hershberger2004-07-09 14:22:33 +0000876 (if (looking-at "\\s-*]>")
23d519e4 Mark A. Hershberger2005-06-10 15:37:37 +0000877 (goto-char (match-end 0))))
461f3ad0 Andreas Schwab2003-11-07 16:05:26 +0000878 (nreverse dtd)))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000879
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800880(defun xml--entity-replacement-text (string)
881 "Return the replacement text for the entity value STRING.
882The replacement text is obtained by replacing character
883references and parameter-entity references."
b3218de1
CY
Chong Yidong2012-06-30 22:21:24 +0800884 (let ((ref-re (eval-when-compile
885 (concat "\\(?:&#\\([0-9]+\\)\\|&#x\\([0-9a-fA-F]+\\)\\|%\\("
886 xml-name-re "\\)\\);")))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800887 children)
888 (while (string-match ref-re string)
889 (push (substring string 0 (match-beginning 0)) children)
890 (let ((remainder (substring string (match-end 0)))
891 ref val)
892 (cond ((setq ref (match-string 1 string))
893 ;; Decimal character reference
894 (setq val (decode-char 'ucs (string-to-number ref)))
895 (if val (push (string val) children)))
896 ;; Hexadecimal character reference
897 ((setq ref (match-string 2 string))
898 (setq val (decode-char 'ucs (string-to-number ref 16)))
899 (if val (push (string val) children)))
900 ;; Parameter entity reference
901 ((setq ref (match-string 3 string))
902 (setq val (assoc ref xml-parameter-entity-alist))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800903 (and (null val)
904 xml-validating-parser
905 (error "XML: (Validity) Undefined parameter entity `%s'" ref))
906 (push (or (cdr val) xml-undefined-entity) children)))
7f3fbd5d
CY
Chong Yidong2012-06-30 19:33:22 +0800907 (setq string remainder)))
908 (mapconcat 'identity (nreverse (cons string children)) "")))
909
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000910(defun xml-parse-elem-type (string)
a98e819b Dave Love2003-05-19 16:03:45 +0000911 "Convert element type STRING into a Lisp structure."
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000912
913 (let (elem modifier)
914 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string)
915 (progn
f6fcdfff
CY
Chong Yidong2007-03-17 18:55:52 +0000916 (setq elem (match-string-no-properties 1 string)
917 modifier (match-string-no-properties 2 string))
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200918 (if (string-match-p "|" elem)
971489ea Stefan Monnier2001-12-14 22:12:30 +0000919 (setq elem (cons 'choice
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000920 (mapcar 'xml-parse-elem-type
921 (split-string elem "|"))))
9a4ebc74 Juanma Barranquero2013-08-11 02:07:48 +0200922 (if (string-match-p "," elem)
971489ea Stefan Monnier2001-12-14 22:12:30 +0000923 (setq elem (cons 'seq
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000924 (mapcar 'xml-parse-elem-type
a98e819b Dave Love2003-05-19 16:03:45 +0000925 (split-string elem ",")))))))
a158ff81 Juanma Barranquero2003-03-16 10:48:34 +0000926 (if (string-match "[ \t\n\r]*\\([^+*?]+\\)\\([+*?]?\\)" string)
f6fcdfff
CY
Chong Yidong2007-03-17 18:55:52 +0000927 (setq elem (match-string-no-properties 1 string)
928 modifier (match-string-no-properties 2 string))))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000929
971489ea
SM
Stefan Monnier2001-12-14 22:12:30 +0000930 (if (and (stringp elem) (string= elem "#PCDATA"))
931 (setq elem 'pcdata))
524425ae Thien-Thi Nguyen2003-01-04 09:32:17 +0000932
971489ea
SM
Stefan Monnier2001-12-14 22:12:30 +0000933 (cond
934 ((string= modifier "+")
935 (list '+ elem))
936 ((string= modifier "*")
937 (list '* elem))
938 ((string= modifier "?")
0fa6f70c Juanma Barranquero2003-02-14 09:58:04 +0000939 (list '\? elem))
971489ea
SM
Stefan Monnier2001-12-14 22:12:30 +0000940 (t
941 elem))))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +0000942
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800943;;; Substituting special XML sequences
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +0000944
945(defun xml-substitute-special (string)
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800946 "Return STRING, after substituting entity and character references.
947STRING is assumed to occur in an XML attribute value."
566df3fc Chong Yidong2012-07-05 00:14:05 +0800948 (let ((strlen (length string))
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800949 children)
566df3fc Chong Yidong2012-07-05 00:14:05 +0800950 (while (string-match xml-entity-or-char-ref-re string)
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800951 (push (substring string 0 (match-beginning 0)) children)
952 (let* ((remainder (substring string (match-end 0)))
566df3fc
CY
Chong Yidong2012-07-05 00:14:05 +0800953 (is-hex (match-string 1 string)) ; Is it a hex numeric reference?
954 (ref (match-string 2 string))) ; Numeric part of reference
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800955 (if ref
956 ;; [4.6] Character references are included as
957 ;; character data.
566df3fc Chong Yidong2012-07-05 00:14:05 +0800958 (let ((val (decode-char 'ucs (string-to-number ref (if is-hex 16)))))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800959 (push (cond (val (string val))
960 (xml-validating-parser
961 (error "XML: (Validity) Undefined character `x%s'" ref))
962 (t xml-undefined-entity))
963 children)
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800964 (setq string remainder
965 strlen (length string)))
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800966 ;; [4.4.5] Entity references are "included in literal".
967 ;; Note that we don't need do anything special to treat
968 ;; quotes as normal data characters.
566df3fc Chong Yidong2012-07-05 00:14:05 +0800969 (setq ref (match-string 3 string)) ; entity name
a7aef6f5
CY
Chong Yidong2012-07-03 00:21:54 +0800970 (let ((val (or (cdr (assoc ref xml-entity-alist))
971 (if xml-validating-parser
972 (error "XML: (Validity) Undefined entity `%s'" ref)
973 xml-undefined-entity))))
a76e6535
CY
Chong Yidong2012-07-03 13:28:42 +0800974 (setq string (concat val remainder)))
975 (and xml-entity-expansion-limit
976 (> (length string) (+ strlen xml-entity-expansion-limit))
977 (error "XML: Passed `xml-entity-expansion-limit' while expanding `&%s;'"
978 ref)))))
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800979 (mapconcat 'identity (nreverse (cons string children)) "")))
a3110b5d Mark A. Hershberger2004-10-06 00:58:29 +0000980
571855b6
UJ
Ulf Jasper2009-12-06 18:13:19 +0000981(defun xml-substitute-numeric-entities (string)
982 "Substitute SGML numeric entities by their respective utf characters.
983This function replaces numeric entities in the input STRING and
984returns the modified string. For example \"&#42;\" gets replaced
985by \"*\"."
986 (if (and string (stringp string))
987 (let ((start 0))
988 (while (string-match "&#\\([0-9]+\\);" string start)
9a4ebc74
JB
Juanma Barranquero2013-08-11 02:07:48 +0200989 (ignore-errors
990 (setq string (replace-match
991 (string (read (substring string
992 (match-beginning 1)
993 (match-end 1))))
994 nil nil string)))
571855b6
UJ
Ulf Jasper2009-12-06 18:13:19 +0000995 (setq start (1+ (match-beginning 0))))
996 string)
997 nil))
998
a7aef6f5 Chong Yidong2012-07-03 00:21:54 +0800999;;; Printing a parse tree (mainly for debugging).
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00001000
27240aa4
AS
Alex Schroeder2004-04-30 20:00:19 +00001001(defun xml-debug-print (xml &optional indent-string)
1002 "Outputs the XML in the current buffer.
1003XML can be a tree or a list of nodes.
1004The first line is indented with the optional INDENT-STRING."
1005 (setq indent-string (or indent-string ""))
971489ea Stefan Monnier2001-12-14 22:12:30 +00001006 (dolist (node xml)
27240aa4
AS
Alex Schroeder2004-04-30 20:00:19 +00001007 (xml-debug-print-internal node indent-string)))
1008
1009(defalias 'xml-print 'xml-debug-print)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00001010
7731c9f4 Mark A. Hershberger2007-12-18 03:22:05 +00001011(defun xml-escape-string (string)
17975d7f
CY
Chong Yidong2012-08-19 14:37:15 +08001012 "Convert STRING into a string containing valid XML character data.
1013Replace occurrences of &<>'\" in STRING with their default XML
1014entity references (e.g. replace each & with &amp;).
1015
1016XML character data must not contain & or < characters, nor the >
1017character under some circumstances. The XML spec does not impose
1018restriction on \" or ', but we just substitute for these too
1019\(as is permitted by the spec)."
1020 (with-temp-buffer
1021 (insert string)
1022 (dolist (substitution '(("&" . "&amp;")
1023 ("<" . "&lt;")
1024 (">" . "&gt;")
1025 ("'" . "&apos;")
1026 ("\"" . "&quot;")))
1027 (goto-char (point-min))
1028 (while (search-forward (car substitution) nil t)
1029 (replace-match (cdr substitution) t t nil)))
1030 (buffer-string)))
7731c9f4 Mark A. Hershberger2007-12-18 03:22:05 +00001031
971489ea Stefan Monnier2001-12-14 22:12:30 +00001032(defun xml-debug-print-internal (xml indent-string)
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00001033 "Outputs the XML tree in the current buffer.
a98e819b Dave Love2003-05-19 16:03:45 +00001034The first line is indented with INDENT-STRING."
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +00001035 (let ((tree xml)
1036 attlist)
a98e819b Dave Love2003-05-19 16:03:45 +00001037 (insert indent-string ?< (symbol-name (xml-node-name tree)))
524425ae Thien-Thi Nguyen2003-01-04 09:32:17 +00001038
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00001039 ;; output the attribute list
971489ea Stefan Monnier2001-12-14 22:12:30 +00001040 (setq attlist (xml-node-attributes tree))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00001041 (while attlist
7731c9f4
MH
Mark A. Hershberger2007-12-18 03:22:05 +00001042 (insert ?\ (symbol-name (caar attlist)) "=\""
1043 (xml-escape-string (cdar attlist)) ?\")
971489ea Stefan Monnier2001-12-14 22:12:30 +00001044 (setq attlist (cdr attlist)))
524425ae Thien-Thi Nguyen2003-01-04 09:32:17 +00001045
971489ea Stefan Monnier2001-12-14 22:12:30 +00001046 (setq tree (xml-node-children tree))
47db06aa Gerd Moellmann2000-07-19 15:52:13 +00001047
27240aa4
AS
Alex Schroeder2004-04-30 20:00:19 +00001048 (if (null tree)
1049 (insert ?/ ?>)
1050 (insert ?>)
1051
1052 ;; output the children
1053 (dolist (node tree)
1054 (cond
1055 ((listp node)
1056 (insert ?\n)
1057 (xml-debug-print-internal node (concat indent-string " ")))
7731c9f4
MH
Mark A. Hershberger2007-12-18 03:22:05 +00001058 ((stringp node)
1059 (insert (xml-escape-string node)))
27240aa4
AS
Alex Schroeder2004-04-30 20:00:19 +00001060 (t
1061 (error "Invalid XML tree"))))
1062
1063 (when (not (and (null (cdr tree))
1064 (stringp (car tree))))
1065 (insert ?\n indent-string))
1066 (insert ?< ?/ (symbol-name (xml-node-name xml)) ?>))))
47db06aa
GM
Gerd Moellmann2000-07-19 15:52:13 +00001067
1068(provide 'xml)
1069
1070;;; xml.el ends here