Fix use of eval-and-compile in regexps in xml.el.
[emacs.git] / lisp / xml.el
bloba9e1b2c283063de9bbce86ea87f302b93387229f
1 ;;; xml.el --- XML parser
3 ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Emmanuel Briot <briot@gnat.com>
6 ;; Maintainer: Mark A. Hershberger <mah@everybody.org>
7 ;; Keywords: xml, data
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; 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
28 ;; any other Lisp libraries.
30 ;;; FILE FORMAT
32 ;; The document type declaration may either be ignored or (optionally)
33 ;; parsed, but currently the parsing will only accept element
34 ;; declarations. The XML file is assumed to be well-formed. In case
35 ;; of error, the parsing stops and the XML file is shown where the
36 ;; parsing stopped.
38 ;; It also knows how to ignore comments and processing instructions.
40 ;; The XML file should have the following format:
41 ;; <node1 attr1="name1" attr2="name2" ...>value
42 ;; <node2 attr3="name3" attr4="name4">value2</node2>
43 ;; <node3 attr5="name5" attr6="name6">value3</node3>
44 ;; </node1>
45 ;; Of course, the name of the nodes and attributes can be anything. There can
46 ;; be any number of attributes (or none), as well as any number of children
47 ;; below the nodes.
49 ;; There can be only top level node, but with any number of children below.
51 ;;; LIST FORMAT
53 ;; The functions `xml-parse-file', `xml-parse-region' and
54 ;; `xml-parse-tag' return a list with the following format:
56 ;; xml-list ::= (node node ...)
57 ;; node ::= (qname attribute-list . child_node_list)
58 ;; child_node_list ::= child_node child_node ...
59 ;; child_node ::= node | string
60 ;; qname ::= (:namespace-uri . "name") | "name"
61 ;; attribute_list ::= ((qname . "value") (qname . "value") ...)
62 ;; | nil
63 ;; string ::= "..."
65 ;; 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.
69 ;; TODO:
70 ;; * xml:base, xml:space support
71 ;; * more complete DOCTYPE parsing
72 ;; * pi support
74 ;;; Code:
76 ;; 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.
83 ;;*******************************************************************
84 ;;**
85 ;;** Macros to parse the list
86 ;;**
87 ;;*******************************************************************
89 (defconst xml-undefined-entity "?"
90 "What to substitute for undefined entities")
92 (defvar xml-entity-alist
93 '(("lt" . "<")
94 ("gt" . ">")
95 ("apos" . "'")
96 ("quot" . "\"")
97 ("amp" . "&"))
98 "Alist of defined XML entities.")
100 (defvar xml-parameter-entity-alist nil
101 "Alist of defined XML parametric entities.")
103 (defvar xml-sub-parser nil
104 "Non-nil when the XML parser is parsing an XML fragment.")
106 (defvar xml-validating-parser nil
107 "Set to non-nil to get validity checking.")
109 (defsubst xml-node-name (node)
110 "Return the tag associated with NODE.
111 Without namespace-aware parsing, the tag is a symbol.
113 With namespace-aware parsing, the tag is a cons of a string
114 representing the uri of the namespace with the local name of the
115 tag. For example,
117 <foo>
119 would be represented by
121 '(\"\" . \"foo\")."
123 (car node))
125 (defsubst xml-node-attributes (node)
126 "Return the list of attributes of NODE.
127 The list can be nil."
128 (nth 1 node))
130 (defsubst xml-node-children (node)
131 "Return the list of children of NODE.
132 This is a list of nodes, and it can be nil."
133 (cddr node))
135 (defun xml-get-children (node child-name)
136 "Return the children of NODE whose tag is CHILD-NAME.
137 CHILD-NAME should match the value returned by `xml-node-name'."
138 (let ((match ()))
139 (dolist (child (xml-node-children node))
140 (if (and (listp child)
141 (equal (xml-node-name child) child-name))
142 (push child match)))
143 (nreverse match)))
145 (defun xml-get-attribute-or-nil (node attribute)
146 "Get from NODE the value of ATTRIBUTE.
147 Return nil if the attribute was not found.
149 See also `xml-get-attribute'."
150 (cdr (assoc attribute (xml-node-attributes node))))
152 (defsubst xml-get-attribute (node attribute)
153 "Get from NODE the value of ATTRIBUTE.
154 An empty string is returned if the attribute was not found.
156 See also `xml-get-attribute-or-nil'."
157 (or (xml-get-attribute-or-nil node attribute) ""))
159 ;;*******************************************************************
160 ;;**
161 ;;** Creating the list
162 ;;**
163 ;;*******************************************************************
165 ;;;###autoload
166 (defun xml-parse-file (file &optional parse-dtd parse-ns)
167 "Parse the well-formed XML file FILE.
168 If FILE is already visited, use its buffer and don't kill it.
169 Returns the top node with all its children.
170 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped.
171 If PARSE-NS is non-nil, then QNAMES are expanded."
172 (if (get-file-buffer file)
173 (with-current-buffer (get-file-buffer file)
174 (save-excursion
175 (xml-parse-region (point-min)
176 (point-max)
177 (current-buffer)
178 parse-dtd parse-ns)))
179 (with-temp-buffer
180 (insert-file-contents file)
181 (xml-parse-region (point-min)
182 (point-max)
183 (current-buffer)
184 parse-dtd parse-ns))))
186 (eval-and-compile
187 (let* ((start-chars (concat "[:alpha:]:_"))
188 (name-chars (concat "-[:digit:]." start-chars))
189 ;;[3] S ::= (#x20 | #x9 | #xD | #xA)+
190 (whitespace "[ \t\n\r]"))
191 ;; [4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6]
192 ;; | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF]
193 ;; | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF]
194 ;; | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD]
195 ;; | [#x10000-#xEFFFF]
196 (defconst xml-name-start-char-re (concat "[" start-chars "]"))
197 ;; [4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7
198 ;; | [#x0300-#x036F] | [#x203F-#x2040]
199 (defconst xml-name-char-re (concat "[" name-chars "]"))
200 ;; [5] Name ::= NameStartChar (NameChar)*
201 (defconst xml-name-re (concat xml-name-start-char-re xml-name-char-re "*"))
202 ;; [6] Names ::= Name (#x20 Name)*
203 (defconst xml-names-re (concat xml-name-re "\\(?: " xml-name-re "\\)*"))
204 ;; [7] Nmtoken ::= (NameChar)+
205 (defconst xml-nmtoken-re (concat xml-name-char-re "+"))
206 ;; [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
207 (defconst xml-nmtokens-re (concat xml-nmtoken-re "\\(?: " xml-name-re "\\)*"))
208 ;; [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
209 (defconst xml-char-ref-re "\\(?:&#[0-9]+;\\|&#x[0-9a-fA-F]+;\\)")
210 ;; [68] EntityRef ::= '&' Name ';'
211 (defconst xml-entity-ref (concat "&" xml-name-re ";"))
212 ;; [69] PEReference ::= '%' Name ';'
213 (defconst xml-pe-reference-re (concat "%" xml-name-re ";"))
214 ;; [67] Reference ::= EntityRef | CharRef
215 (defconst xml-reference-re (concat "\\(?:" xml-entity-ref "\\|" xml-char-ref-re "\\)"))
216 ;; [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
217 (defconst xml-att-value-re (concat "\\(?:\"\\(?:[^&\"]\\|" xml-reference-re "\\)*\"\\|"
218 "'\\(?:[^&']\\|" xml-reference-re "\\)*'\\)"))
219 ;; [56] TokenizedType ::= 'ID' [VC: ID] [VC: One ID / Element Type] [VC: ID Attribute Default]
220 ;; | 'IDREF' [VC: IDREF]
221 ;; | 'IDREFS' [VC: IDREF]
222 ;; | 'ENTITY' [VC: Entity Name]
223 ;; | 'ENTITIES' [VC: Entity Name]
224 ;; | 'NMTOKEN' [VC: Name Token]
225 ;; | 'NMTOKENS' [VC: Name Token]
226 (defconst xml-tokenized-type-re (concat "\\(?:ID\\|IDREF\\|IDREFS\\|ENTITY\\|"
227 "ENTITIES\\|NMTOKEN\\|NMTOKENS\\)"))
228 ;; [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
229 (defconst xml-notation-type-re
230 (concat "\\(?:NOTATION" whitespace "(" whitespace "*" xml-name-re
231 "\\(?:" whitespace "*|" whitespace "*" xml-name-re "\\)*"
232 whitespace "*)\\)"))
233 ;; [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
234 ;; [VC: Enumeration] [VC: No Duplicate Tokens]
235 (defconst xml-enumeration-re (concat "\\(?:(" whitespace "*" xml-nmtoken-re
236 "\\(?:" whitespace "*|" whitespace "*"
237 xml-nmtoken-re "\\)*"
238 whitespace ")\\)"))
239 ;; [57] EnumeratedType ::= NotationType | Enumeration
240 (defconst xml-enumerated-type-re (concat "\\(?:" xml-notation-type-re
241 "\\|" xml-enumeration-re "\\)"))
242 ;; [54] AttType ::= StringType | TokenizedType | EnumeratedType
243 ;; [55] StringType ::= 'CDATA'
244 (defconst xml-att-type-re (concat "\\(?:CDATA\\|" xml-tokenized-type-re
245 "\\|" xml-notation-type-re
246 "\\|" xml-enumerated-type-re "\\)"))
247 ;; [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
248 (defconst xml-default-decl-re (concat "\\(?:#REQUIRED\\|#IMPLIED\\|\\(?:#FIXED"
249 whitespace "\\)*" xml-att-value-re "\\)"))
250 ;; [53] AttDef ::= S Name S AttType S DefaultDecl
251 (defconst xml-att-def-re (concat "\\(?:" whitespace "*" xml-name-re
252 whitespace "*" xml-att-type-re
253 whitespace "*" xml-default-decl-re "\\)"))
254 ;; [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"'
255 ;; | "'" ([^%&'] | PEReference | Reference)* "'"
256 (defconst xml-entity-value-re (concat "\\(?:\"\\(?:[^%&\"]\\|" xml-pe-reference-re
257 "\\|" xml-reference-re
258 "\\)*\"\\|'\\(?:[^%&']\\|"
259 xml-pe-reference-re "\\|"
260 xml-reference-re "\\)*'\\)"))))
262 ;; [75] ExternalID ::= 'SYSTEM' S SystemLiteral
263 ;; | 'PUBLIC' S PubidLiteral S SystemLiteral
264 ;; [76] NDataDecl ::= S 'NDATA' S
265 ;; [73] EntityDef ::= EntityValue| (ExternalID NDataDecl?)
266 ;; [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
267 ;; [74] PEDef ::= EntityValue | ExternalID
268 ;; [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
269 ;; [70] EntityDecl ::= GEDecl | PEDecl
271 ;; Note that this is setup so that we can do whitespace-skipping with
272 ;; `(skip-syntax-forward " ")', inter alia. Previously this was slow
273 ;; compared with `re-search-forward', but that has been fixed. Also
274 ;; note that the standard syntax table contains other characters with
275 ;; whitespace syntax, like NBSP, but they are invalid in contexts in
276 ;; which we might skip whitespace -- specifically, they're not
277 ;; NameChars [XML 4].
279 (defvar xml-syntax-table
280 (let ((table (make-syntax-table)))
281 ;; Get space syntax correct per XML [3].
282 (dotimes (c 31)
283 (modify-syntax-entry c "." table)) ; all are space in standard table
284 (dolist (c '(?\t ?\n ?\r)) ; these should be space
285 (modify-syntax-entry c " " table))
286 ;; For skipping attributes.
287 (modify-syntax-entry ?\" "\"" table)
288 (modify-syntax-entry ?' "\"" table)
289 ;; Non-alnum name chars should be symbol constituents (`-' and `_'
290 ;; are OK by default).
291 (modify-syntax-entry ?. "_" table)
292 (modify-syntax-entry ?: "_" table)
293 ;; XML [89]
294 (unless (featurep 'xemacs)
295 (dolist (c '(#x00B7 #x02D0 #x02D1 #x0387 #x0640 #x0E46 #x0EC6 #x3005
296 #x3031 #x3032 #x3033 #x3034 #x3035 #x309D #x309E #x30FC
297 #x30FD #x30FE))
298 (modify-syntax-entry (decode-char 'ucs c) "w" table)))
299 ;; Fixme: rest of [4]
300 table)
301 "Syntax table used by `xml-parse-region'.")
303 ;; XML [5]
304 ;; Note that [:alpha:] matches all multibyte chars with word syntax.
305 (eval-and-compile
306 (defconst xml-name-regexp "[[:alpha:]_:][[:alnum:]._:-]*"))
308 ;; Fixme: This needs re-writing to deal with the XML grammar properly, i.e.
309 ;; document ::= prolog element Misc*
310 ;; prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
312 ;;;###autoload
313 (defun xml-parse-region (beg end &optional buffer parse-dtd parse-ns)
314 "Parse the region from BEG to END in BUFFER.
315 If BUFFER is nil, it defaults to the current buffer.
316 Returns the XML list for the region, or raises an error if the region
317 is not well-formed XML.
318 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped,
319 and returned as the first element of the list.
320 If PARSE-NS is non-nil, then QNAMES are expanded."
321 ;; Use fixed syntax table to ensure regexp char classes and syntax
322 ;; specs DTRT.
323 (with-syntax-table (standard-syntax-table)
324 (let ((case-fold-search nil) ; XML is case-sensitive.
325 ;; Prevent entity definitions from changing the defaults
326 (xml-entity-alist xml-entity-alist)
327 (xml-parameter-entity-alist xml-parameter-entity-alist)
328 xml result dtd)
329 (save-excursion
330 (if buffer
331 (set-buffer buffer))
332 (save-restriction
333 (narrow-to-region beg end)
334 (goto-char (point-min))
335 (while (not (eobp))
336 (if (search-forward "<" nil t)
337 (progn
338 (forward-char -1)
339 (setq result (xml-parse-tag parse-dtd parse-ns))
340 (cond
341 ((null result)
342 ;; Not looking at an xml start tag.
343 (unless (eobp)
344 (forward-char 1)))
345 ((and xml (not xml-sub-parser))
346 ;; Translation of rule [1] of XML specifications
347 (error "XML: (Not Well-Formed) Only one root tag allowed"))
348 ((and (listp (car result))
349 parse-dtd)
350 (setq dtd (car result))
351 (if (cdr result) ; possible leading comment
352 (add-to-list 'xml (cdr result))))
354 (add-to-list 'xml result))))
355 (goto-char (point-max))))
356 (if parse-dtd
357 (cons dtd (nreverse xml))
358 (nreverse xml)))))))
360 (defun xml-maybe-do-ns (name default xml-ns)
361 "Perform any namespace expansion.
362 NAME is the name to perform the expansion on.
363 DEFAULT is the default namespace. XML-NS is a cons of namespace
364 names to uris. When namespace-aware parsing is off, then XML-NS
365 is nil.
367 During namespace-aware parsing, any name without a namespace is
368 put into the namespace identified by DEFAULT. nil is used to
369 specify that the name shouldn't be given a namespace."
370 (if (consp xml-ns)
371 (let* ((nsp (string-match ":" name))
372 (lname (if nsp (substring name (match-end 0)) name))
373 (prefix (if nsp (substring name 0 (match-beginning 0)) default))
374 (special (and (string-equal lname "xmlns") (not prefix)))
375 ;; Setting default to nil will insure that there is not
376 ;; matching cons in xml-ns. In which case we
377 (ns (or (cdr (assoc (if special "xmlns" prefix)
378 xml-ns))
379 "")))
380 (cons ns (if special "" lname)))
381 (intern name)))
383 (defun xml-parse-fragment (&optional parse-dtd parse-ns)
384 "Parse xml-like fragments."
385 (let ((xml-sub-parser t)
386 ;; Prevent entity definitions from changing the defaults
387 (xml-entity-alist xml-entity-alist)
388 (xml-parameter-entity-alist xml-parameter-entity-alist)
389 children)
390 (while (not (eobp))
391 (let ((bit (xml-parse-tag
392 parse-dtd parse-ns)))
393 (if children
394 (setq children (append (list bit) children))
395 (if (stringp bit)
396 (setq children (list bit))
397 (setq children bit)))))
398 (reverse children)))
400 (defun xml-parse-tag (&optional parse-dtd parse-ns)
401 "Parse the tag at point.
402 If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and
403 returned as the first element in the list.
404 If PARSE-NS is non-nil, then QNAMES are expanded.
405 Returns one of:
406 - a list : the matching node
407 - nil : the point is not looking at a tag.
408 - a pair : the first element is the DTD, the second is the node."
409 (let ((xml-validating-parser (or parse-dtd xml-validating-parser))
410 (xml-ns (if (consp parse-ns)
411 parse-ns
412 (if parse-ns
413 (list
414 ;; Default for empty prefix is no namespace
415 (cons "" "")
416 ;; "xml" namespace
417 (cons "xml" "http://www.w3.org/XML/1998/namespace")
418 ;; We need to seed the xmlns namespace
419 (cons "xmlns" "http://www.w3.org/2000/xmlns/"))))))
420 (cond
421 ;; Processing instructions (like the <?xml version="1.0"?> tag at the
422 ;; beginning of a document).
423 ((looking-at "<\\?")
424 (search-forward "?>")
425 (skip-syntax-forward " ")
426 (xml-parse-tag parse-dtd xml-ns))
427 ;; Character data (CDATA) sections, in which no tag should be interpreted
428 ((looking-at "<!\\[CDATA\\[")
429 (let ((pos (match-end 0)))
430 (unless (search-forward "]]>" nil t)
431 (error "XML: (Not Well Formed) CDATA section does not end anywhere in the document"))
432 (concat
433 (buffer-substring-no-properties pos (match-beginning 0))
434 (xml-parse-string))))
435 ;; DTD for the document
436 ((looking-at "<!DOCTYPE[ \t\n\r]")
437 (let ((dtd (xml-parse-dtd parse-ns)))
438 (skip-syntax-forward " ")
439 (if xml-validating-parser
440 (cons dtd (xml-parse-tag nil xml-ns))
441 (xml-parse-tag nil xml-ns))))
442 ;; skip comments
443 ((looking-at "<!--")
444 (search-forward "-->")
445 (skip-syntax-forward " ")
446 (unless (eobp)
447 (let ((xml-sub-parser t))
448 (xml-parse-tag parse-dtd xml-ns))))
449 ;; end tag
450 ((looking-at "</")
451 '())
452 ;; opening tag
453 ((looking-at "<\\([^/>[:space:]]+\\)")
454 (goto-char (match-end 1))
456 ;; Parse this node
457 (let* ((node-name (match-string-no-properties 1))
458 ;; Parse the attribute list.
459 (attrs (xml-parse-attlist xml-ns))
460 children)
462 ;; add the xmlns:* attrs to our cache
463 (when (consp xml-ns)
464 (dolist (attr attrs)
465 (when (and (consp (car attr))
466 (equal "http://www.w3.org/2000/xmlns/"
467 (caar attr)))
468 (push (cons (cdar attr) (cdr attr))
469 xml-ns))))
471 (setq children (list attrs (xml-maybe-do-ns node-name "" xml-ns)))
473 ;; is this an empty element ?
474 (if (looking-at "/>")
475 (progn
476 (forward-char 2)
477 (nreverse children))
479 ;; is this a valid start tag ?
480 (if (eq (char-after) ?>)
481 (progn
482 (forward-char 1)
483 ;; Now check that we have the right end-tag. Note that this
484 ;; one might contain spaces after the tag name
485 (let ((end (concat "</" node-name "\\s-*>")))
486 (while (not (looking-at end))
487 (cond
488 ((looking-at "</")
489 (error "XML: (Not Well-Formed) Invalid end tag (expecting %s) at pos %d"
490 node-name (point)))
491 ((= (char-after) ?<)
492 (let ((tag (xml-parse-tag nil xml-ns)))
493 (when tag
494 (push tag children))))
496 (let ((expansion (xml-parse-string)))
497 (setq children
498 (if (stringp expansion)
499 (if (stringp (car children))
500 ;; The two strings were separated by a comment.
501 (setq children (append (list (concat (car children) expansion))
502 (cdr children)))
503 (setq children (append (list expansion) children)))
504 (setq children (append expansion children))))))))
506 (goto-char (match-end 0))
507 (nreverse children)))
508 ;; This was an invalid start tag (Expected ">", but didn't see it.)
509 (error "XML: (Well-Formed) Couldn't parse tag: %s"
510 (buffer-substring-no-properties (- (point) 10) (+ (point) 1)))))))
511 (t ;; (Not one of PI, CDATA, Comment, End tag, or Start tag)
512 (unless xml-sub-parser ; Usually, we error out.
513 (error "XML: (Well-Formed) Invalid character"))
515 ;; However, if we're parsing incrementally, then we need to deal
516 ;; with stray CDATA.
517 (xml-parse-string)))))
519 (defun xml-parse-string ()
520 "Parse the next whatever. Could be a string, or an element."
521 (let* ((pos (point))
522 (string (progn (skip-chars-forward "^<")
523 (buffer-substring-no-properties pos (point)))))
524 ;; Clean up the string. As per XML specifications, the XML
525 ;; processor should always pass the whole string to the
526 ;; application. But \r's should be replaced:
527 ;; http://www.w3.org/TR/2000/REC-xml-20001006#sec-line-ends
528 (setq pos 0)
529 (while (string-match "\r\n?" string pos)
530 (setq string (replace-match "\n" t t string))
531 (setq pos (1+ (match-beginning 0))))
533 (xml-substitute-special string)))
535 (defun xml-parse-attlist (&optional xml-ns)
536 "Return the attribute-list after point.
537 Leave point at the first non-blank character after the tag."
538 (let ((attlist ())
539 end-pos name)
540 (skip-syntax-forward " ")
541 (while (looking-at (eval-when-compile
542 (concat "\\(" xml-name-regexp "\\)\\s-*=\\s-*")))
543 (setq end-pos (match-end 0))
544 (setq name (xml-maybe-do-ns (match-string-no-properties 1) nil xml-ns))
545 (goto-char end-pos)
547 ;; See also: http://www.w3.org/TR/2000/REC-xml-20001006#AVNormalize
549 ;; Do we have a string between quotes (or double-quotes),
550 ;; or a simple word ?
551 (if (looking-at "\"\\([^\"]*\\)\"")
552 (setq end-pos (match-end 0))
553 (if (looking-at "'\\([^']*\\)'")
554 (setq end-pos (match-end 0))
555 (error "XML: (Not Well-Formed) Attribute values must be given between quotes")))
557 ;; Each attribute must be unique within a given element
558 (if (assoc name attlist)
559 (error "XML: (Not Well-Formed) Each attribute must be unique within an element"))
561 ;; Multiple whitespace characters should be replaced with a single one
562 ;; in the attributes
563 (let ((string (match-string-no-properties 1)))
564 (replace-regexp-in-string "\\s-\\{2,\\}" " " string)
565 (let ((expansion (xml-substitute-special string)))
566 (unless (stringp expansion)
567 ; We say this is the constraint. It is actually that neither
568 ; external entities nor "<" can be in an attribute value.
569 (error "XML: (Not Well-Formed) Entities in attributes cannot expand into elements"))
570 (push (cons name expansion) attlist)))
572 (goto-char end-pos)
573 (skip-syntax-forward " "))
574 (nreverse attlist)))
576 ;;*******************************************************************
577 ;;**
578 ;;** The DTD (document type declaration)
579 ;;** The following functions know how to skip or parse the DTD of
580 ;;** a document
581 ;;**
582 ;;*******************************************************************
584 ;; Fixme: This fails at least if the DTD contains conditional sections.
586 (defun xml-skip-dtd ()
587 "Skip the DTD at point.
588 This follows the rule [28] in the XML specifications."
589 (let ((xml-validating-parser nil))
590 (xml-parse-dtd)))
592 (defun xml-parse-dtd (&optional parse-ns)
593 "Parse the DTD at point."
594 (forward-char (eval-when-compile (length "<!DOCTYPE")))
595 (skip-syntax-forward " ")
596 (if (and (looking-at ">")
597 xml-validating-parser)
598 (error "XML: (Validity) Invalid DTD (expecting name of the document)"))
600 ;; Get the name of the document
601 (looking-at xml-name-regexp)
602 (let ((dtd (list (match-string-no-properties 0) 'dtd))
603 (xml-parameter-entity-alist xml-parameter-entity-alist))
604 (goto-char (match-end 0))
605 (skip-syntax-forward " ")
607 ;; External subset (XML [75])
608 (cond ((looking-at "PUBLIC\\s-+")
609 (goto-char (match-end 0))
610 (unless (or (re-search-forward
611 "\\=\"\\([[:space:][:alnum:]-'()+,./:=?;!*#@$_%]*\\)\""
612 nil t)
613 (re-search-forward
614 "\\='\\([[:space:][:alnum:]-()+,./:=?;!*#@$_%]*\\)'"
615 nil t))
616 (error "XML: Missing Public ID"))
617 (let ((pubid (match-string-no-properties 1)))
618 (skip-syntax-forward " ")
619 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t)
620 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t))
621 (error "XML: Missing System ID"))
622 (push (list pubid (match-string-no-properties 1) 'public) dtd)))
623 ((looking-at "SYSTEM\\s-+")
624 (goto-char (match-end 0))
625 (unless (or (re-search-forward "\\='\\([^']*\\)'" nil t)
626 (re-search-forward "\\=\"\\([^\"]*\\)\"" nil t))
627 (error "XML: Missing System ID"))
628 (push (list (match-string-no-properties 1) 'system) dtd)))
629 (skip-syntax-forward " ")
631 (if (eq (char-after) ?>)
633 ;; No internal subset
634 (forward-char)
636 ;; Internal subset (XML [28b])
637 (unless (eq (char-after) ?\[)
638 (error "XML: Bad DTD"))
639 (forward-char)
641 ;; Parse the rest of the DTD
642 ;; Fixme: Deal with NOTATION, PIs.
643 (while (not (looking-at "\\s-*\\]"))
644 (skip-syntax-forward " ")
645 (cond
646 ;; Element declaration [45]:
647 ((looking-at "<!ELEMENT\\s-+\\([[:alnum:].%;]+\\)\\s-+\\([^>]+\\)>")
648 (let ((element (match-string-no-properties 1))
649 (type (match-string-no-properties 2))
650 (end-pos (match-end 0)))
651 ;; Translation of rule [46] of XML specifications
652 (cond
653 ((string-match "^EMPTY[ \t\n\r]*$" type) ; empty declaration
654 (setq type 'empty))
655 ((string-match "^ANY[ \t\n\r]*$" type) ; any type of contents
656 (setq type 'any))
657 ((string-match "^(\\(.*\\))[ \t\n\r]*$" type) ; children ([47])
658 (setq type (xml-parse-elem-type (match-string-no-properties 1 type))))
659 ((string-match "^%[^;]+;[ \t\n\r]*$" type) ; substitution
660 nil)
661 (xml-validating-parser
662 (error "XML: (Validity) Invalid element type in the DTD")))
664 ;; rule [45]: the element declaration must be unique
665 (and (assoc element dtd)
666 xml-validating-parser
667 (error "XML: (Validity) DTD element declarations must be unique (<%s>)"
668 element))
670 ;; Store the element in the DTD
671 (push (list element type) dtd)
672 (goto-char end-pos)))
674 ;; Attribute-list declaration [52] (currently unsupported):
675 ((looking-at (concat "<!ATTLIST[ \t\n\r]*\\(" xml-name-re
676 "\\)[ \t\n\r]*\\(" xml-att-def-re
677 "\\)*[ \t\n\r]*>"))
678 (goto-char (match-end 0)))
680 ;; Comments (skip to end):
681 ((looking-at "<!--")
682 (search-forward "-->"))
684 ;; Internal entity declarations:
685 ((looking-at (concat "<!ENTITY[ \t\n\r]+\\(%[ \t\n\r]+\\)?\\("
686 xml-name-re "\\)[ \t\n\r]*\\("
687 xml-entity-value-re "\\)[ \t\n\r]*>"))
688 (let* ((name (prog1 (match-string-no-properties 2)
689 (goto-char (match-end 0))))
690 (alist (if (match-string 1)
691 'xml-parameter-entity-alist
692 'xml-entity-alist))
693 ;; Retrieve the deplacement text:
694 (value (xml--entity-replacement-text
695 ;; Entity value, sans quotation marks:
696 (substring (match-string-no-properties 3) 1 -1))))
697 ;; If the same entity is declared more than once, the
698 ;; first declaration is binding.
699 (unless (assoc name (symbol-value alist))
700 (set alist (cons (cons name value) (symbol-value alist))))))
702 ;; External entity declarations (currently unsupported):
703 ((or (looking-at (concat "<!ENTITY[ \t\n\r]+\\(%[ \t\n\r]+\\)?\\("
704 xml-name-re "\\)[ \t\n\r]+SYSTEM[ \t\n\r]+"
705 "\\(\"[^\"]*\"\\|'[^']*'\\)[ \t\n\r]*>"))
706 (looking-at (concat "<!ENTITY[ \t\n\r]+\\(%[ \t\n\r]+\\)?\\("
707 xml-name-re "\\)[ \t\n\r]+PUBLIC[ \t\n\r]+"
708 "\"[- \r\na-zA-Z0-9'()+,./:=?;!*#@$_%]*\""
709 "\\|'[- \r\na-zA-Z0-9()+,./:=?;!*#@$_%]*'"
710 "[ \t\n\r]+\\(\"[^\"]*\"\\|'[^']*'\\)"
711 "[ \t\n\r]*>")))
712 (goto-char (match-end 0)))
714 ;; Parameter entity:
715 ((looking-at (concat "%\\(" xml-name-re "\\);"))
716 (goto-char (match-end 0))
717 (let* ((entity (match-string 1))
718 (end (point-marker))
719 (elt (assoc entity xml-parameter-entity-alist)))
720 (when elt
721 (replace-match (cdr elt) t t)
722 (goto-char end))))
724 ;; Anything else:
725 (xml-validating-parser
726 (error "XML: (Validity) Invalid DTD item"))))
728 (if (looking-at "\\s-*]>")
729 (goto-char (match-end 0))))
730 (nreverse dtd)))
732 (defun xml--entity-replacement-text (string)
733 "Return the replacement text for the entity value STRING.
734 The replacement text is obtained by replacing character
735 references and parameter-entity references."
736 (let ((ref-re (eval-when-compile
737 (concat "\\(?:&#\\([0-9]+\\)\\|&#x\\([0-9a-fA-F]+\\)\\|%\\("
738 xml-name-re "\\)\\);")))
739 children)
740 (while (string-match ref-re string)
741 (push (substring string 0 (match-beginning 0)) children)
742 (let ((remainder (substring string (match-end 0)))
743 ref val)
744 (cond ((setq ref (match-string 1 string))
745 ;; Decimal character reference
746 (setq val (decode-char 'ucs (string-to-number ref)))
747 (if val (push (string val) children)))
748 ;; Hexadecimal character reference
749 ((setq ref (match-string 2 string))
750 (setq val (decode-char 'ucs (string-to-number ref 16)))
751 (if val (push (string val) children)))
752 ;; Parameter entity reference
753 ((setq ref (match-string 3 string))
754 (setq val (assoc ref xml-parameter-entity-alist))
755 (if val
756 (push (cdr val) children)
757 (push (concat "%" ref ";") children))))
758 (setq string remainder)))
759 (mapconcat 'identity (nreverse (cons string children)) "")))
761 (defun xml-parse-elem-type (string)
762 "Convert element type STRING into a Lisp structure."
764 (let (elem modifier)
765 (if (string-match "(\\([^)]+\\))\\([+*?]?\\)" string)
766 (progn
767 (setq elem (match-string-no-properties 1 string)
768 modifier (match-string-no-properties 2 string))
769 (if (string-match "|" elem)
770 (setq elem (cons 'choice
771 (mapcar 'xml-parse-elem-type
772 (split-string elem "|"))))
773 (if (string-match "," elem)
774 (setq elem (cons 'seq
775 (mapcar 'xml-parse-elem-type
776 (split-string elem ",")))))))
777 (if (string-match "[ \t\n\r]*\\([^+*?]+\\)\\([+*?]?\\)" string)
778 (setq elem (match-string-no-properties 1 string)
779 modifier (match-string-no-properties 2 string))))
781 (if (and (stringp elem) (string= elem "#PCDATA"))
782 (setq elem 'pcdata))
784 (cond
785 ((string= modifier "+")
786 (list '+ elem))
787 ((string= modifier "*")
788 (list '* elem))
789 ((string= modifier "?")
790 (list '\? elem))
792 elem))))
794 ;;*******************************************************************
795 ;;**
796 ;;** Substituting special XML sequences
797 ;;**
798 ;;*******************************************************************
800 (defun xml-substitute-special (string)
801 "Return STRING, after substituting entity references."
802 ;; This originally made repeated passes through the string from the
803 ;; beginning, which isn't correct, since then either "&amp;amp;" or
804 ;; "&#38;amp;" won't DTRT.
806 (let ((point 0)
807 children end-point)
808 (while (string-match "&\\([^;]*\\);" string point)
809 (setq end-point (match-end 0))
810 (let* ((this-part (match-string-no-properties 1 string))
811 (prev-part (substring string point (match-beginning 0)))
812 (entity (assoc this-part xml-entity-alist))
813 (expansion
814 (cond ((string-match "#\\([0-9]+\\)" this-part)
815 (let ((c (decode-char
816 'ucs
817 (string-to-number (match-string-no-properties 1 this-part)))))
818 (if c (string c))))
819 ((string-match "#x\\([[:xdigit:]]+\\)" this-part)
820 (let ((c (decode-char
821 'ucs
822 (string-to-number (match-string-no-properties 1 this-part) 16))))
823 (if c (string c))))
824 (entity
825 (cdr entity))
826 ((eq (length this-part) 0)
827 (error "XML: (Not Well-Formed) No entity given"))
829 (if xml-validating-parser
830 (error "XML: (Validity) Undefined entity `%s'"
831 this-part)
832 xml-undefined-entity)))))
834 (cond ((null children)
835 ;; FIXME: If we have an entity that expands into XML, this won't work.
836 (setq children
837 (concat prev-part expansion)))
838 ((stringp children)
839 (if (stringp expansion)
840 (setq children (concat children prev-part expansion))
841 (setq children (list expansion (concat prev-part children)))))
842 ((and (stringp expansion)
843 (stringp (car children)))
844 (setcar children (concat prev-part expansion (car children))))
845 ((stringp expansion)
846 (setq children (append (concat prev-part expansion)
847 children)))
848 ((stringp (car children))
849 (setcar children (concat (car children) prev-part))
850 (setq children (append expansion children)))
852 (setq children (list expansion
853 prev-part
854 children))))
855 (setq point end-point)))
856 (cond ((stringp children)
857 (concat children (substring string point)))
858 ((stringp (car (last children)))
859 (concat (car (last children)) (substring string point)))
860 ((null children)
861 string)
863 (concat (mapconcat 'identity
864 (nreverse children)
866 (substring string point))))))
868 (defun xml-substitute-numeric-entities (string)
869 "Substitute SGML numeric entities by their respective utf characters.
870 This function replaces numeric entities in the input STRING and
871 returns the modified string. For example \"&#42;\" gets replaced
872 by \"*\"."
873 (if (and string (stringp string))
874 (let ((start 0))
875 (while (string-match "&#\\([0-9]+\\);" string start)
876 (condition-case nil
877 (setq string (replace-match
878 (string (read (substring string
879 (match-beginning 1)
880 (match-end 1))))
881 nil nil string))
882 (error nil))
883 (setq start (1+ (match-beginning 0))))
884 string)
885 nil))
887 ;;*******************************************************************
888 ;;**
889 ;;** Printing a tree.
890 ;;** This function is intended mainly for debugging purposes.
891 ;;**
892 ;;*******************************************************************
894 (defun xml-debug-print (xml &optional indent-string)
895 "Outputs the XML in the current buffer.
896 XML can be a tree or a list of nodes.
897 The first line is indented with the optional INDENT-STRING."
898 (setq indent-string (or indent-string ""))
899 (dolist (node xml)
900 (xml-debug-print-internal node indent-string)))
902 (defalias 'xml-print 'xml-debug-print)
904 (defun xml-escape-string (string)
905 "Return STRING with entity substitutions made from `xml-entity-alist'."
906 (mapconcat (lambda (byte)
907 (let ((char (char-to-string byte)))
908 (if (rassoc char xml-entity-alist)
909 (concat "&" (car (rassoc char xml-entity-alist)) ";")
910 char)))
911 string ""))
913 (defun xml-debug-print-internal (xml indent-string)
914 "Outputs the XML tree in the current buffer.
915 The first line is indented with INDENT-STRING."
916 (let ((tree xml)
917 attlist)
918 (insert indent-string ?< (symbol-name (xml-node-name tree)))
920 ;; output the attribute list
921 (setq attlist (xml-node-attributes tree))
922 (while attlist
923 (insert ?\ (symbol-name (caar attlist)) "=\""
924 (xml-escape-string (cdar attlist)) ?\")
925 (setq attlist (cdr attlist)))
927 (setq tree (xml-node-children tree))
929 (if (null tree)
930 (insert ?/ ?>)
931 (insert ?>)
933 ;; output the children
934 (dolist (node tree)
935 (cond
936 ((listp node)
937 (insert ?\n)
938 (xml-debug-print-internal node (concat indent-string " ")))
939 ((stringp node)
940 (insert (xml-escape-string node)))
942 (error "Invalid XML tree"))))
944 (when (not (and (null (cdr tree))
945 (stringp (car tree))))
946 (insert ?\n indent-string))
947 (insert ?< ?/ (symbol-name (xml-node-name xml)) ?>))))
949 (provide 'xml)
951 ;;; xml.el ends here