1 ;;; xml.el --- XML parser
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Emmanuel Briot <briot@gnat.com>
6 ;; Maintainer: Emmanuel Briot <briot@gnat.com>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This file contains a full XML parser. It parses a file, and returns a list
29 ;; that can be used internally by any other lisp file.
30 ;; See some example in todo.el
34 ;; It does not parse the DTD, if present in the XML file, but knows how to
35 ;; ignore it. The XML file is assumed to be well-formed. In case of error, the
36 ;; parsing stops and the XML file is shown where the parsing stopped.
38 ;; It also knows how to ignore comments, as well as the special ?xml? tag
41 ;; The XML file should have the following format:
42 ;; <node1 attr1="name1" attr2="name2" ...>value
43 ;; <node2 attr3="name3" attr4="name4">value2</node2>
44 ;; <node3 attr5="name5" attr6="name6">value3</node3>
46 ;; Of course, the name of the nodes and attributes can be anything. There can
47 ;; be any number of attributes (or none), as well as any number of children
50 ;; There can be only top level node, but with any number of children below.
54 ;; The functions `xml-parse-file' and `xml-parse-tag' return a list with
55 ;; the following format:
57 ;; xml-list ::= (node node ...)
58 ;; node ::= (tag_name attribute-list . child_node_list)
59 ;; child_node_list ::= child_node child_node ...
60 ;; child_node ::= node | string
61 ;; tag_name ::= string
62 ;; attribute_list ::= (("attribute" . "value") ("attribute" . "value") ...)
66 ;; Some macros are provided to ease the parsing of this list
70 ;;*******************************************************************
72 ;;** Macros to parse the list
74 ;;*******************************************************************
76 (defsubst xml-node-name (node)
77 "Return the tag associated with NODE.
78 The tag is a lower-case symbol."
81 (defsubst xml-node-attributes (node)
82 "Return the list of attributes of NODE.
86 (defsubst xml-node-children (node)
87 "Return the list of children of NODE.
88 This is a list of nodes, and it can be nil."
91 (defun xml-get-children (node child-name)
92 "Return the children of NODE whose tag is CHILD-NAME.
93 CHILD-NAME should be a lower case symbol."
95 (dolist (child (xml-node-children node))
97 (if (equal (xml-node-name child) child-name)
101 (defun xml-get-attribute (node attribute)
102 "Get from NODE the value of ATTRIBUTE.
103 An empty string is returned if the attribute was not found."
104 (if (xml-node-attributes node)
105 (let ((value (assoc attribute (xml-node-attributes node))))
111 ;;*******************************************************************
113 ;;** Creating the list
115 ;;*******************************************************************
117 (defun xml-parse-file (file &optional parse-dtd)
118 "Parse the well-formed XML FILE.
119 If FILE is already edited, this will keep the buffer alive.
120 Returns the top node with all its children.
121 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped."
123 (if (get-file-buffer file)
125 (set-buffer (get-file-buffer file))
129 (let ((xml (xml-parse-region (point-min)
135 (kill-buffer (current-buffer)))
138 (defun xml-parse-region (beg end &optional buffer parse-dtd)
139 "Parse the region from BEG to END in BUFFER.
140 If BUFFER is nil, it defaults to the current buffer.
141 Returns the XML list for the region, or raises an error if the region
142 is not a well-formed XML file.
143 If PARSE-DTD is non-nil, the DTD is parsed rather than skipped,
144 and returned as the first element of the list"
145 (let (xml result dtd)
150 (while (< (point) end)
151 (if (search-forward "<" end t)
156 (setq result (xml-parse-tag end parse-dtd))
159 ((listp (car result))
160 (setq dtd (car result))
161 (add-to-list 'xml (cdr result)))
163 (add-to-list 'xml result))))
165 ;; translation of rule [1] of XML specifications
166 (error "XML files can have only one toplevel tag")))
169 (cons dtd (reverse xml))
173 (defun xml-parse-tag (end &optional parse-dtd)
174 "Parse the tag that is just in front of point.
175 The end tag must be found before the position END in the current buffer.
176 If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and
177 returned as the first element in the list.
179 - a list : the matching node
180 - nil : the point is not looking at a tag.
181 - a cons cell: the first element is the DTD, the second is the node"
183 ;; Processing instructions (like the <?xml version="1.0"?> tag at the
184 ;; beginning of a document)
186 (search-forward "?>" end)
187 (skip-chars-forward " \t\n")
189 ;; Character data (CDATA) sections, in which no tag should be interpreted
190 ((looking-at "<!\\[CDATA\\[")
191 (let ((pos (match-end 0)))
192 (unless (search-forward "]]>" end t)
193 (error "CDATA section does not end anywhere in the document"))
194 (buffer-substring-no-properties pos (match-beginning 0))))
195 ;; DTD for the document
196 ((looking-at "<!DOCTYPE")
199 (setq dtd (xml-parse-dtd end))
201 (skip-chars-forward " \t\n")
203 (cons dtd (xml-parse-tag end))
204 (xml-parse-tag end))))
207 (search-forward "-->" end)
213 ((looking-at "<\\([^/> \t\n]+\\)")
214 (goto-char (match-end 1))
215 (let* ((case-fold-search nil) ;; XML is case-sensitive.
216 (node-name (match-string 1))
217 ;; Parse the attribute list.
218 (children (list (xml-parse-attlist end) (intern node-name)))
221 ;; is this an empty element ?
222 (if (looking-at "/>")
225 (nreverse (cons '("") children)))
227 ;; is this a valid start tag ?
228 (if (eq (char-after) ?>)
231 ;; Now check that we have the right end-tag. Note that this
232 ;; one might contain spaces after the tag name
233 (while (not (looking-at (concat "</" node-name "[ \t\n]*>")))
237 "XML: invalid syntax -- invalid end tag (expecting "
239 ") at pos " (number-to-string (point)))))
241 (let ((tag (xml-parse-tag end)))
243 (push tag children))))
246 (search-forward "<" end)
248 (let ((string (buffer-substring-no-properties pos (point)))
251 ;; Clean up the string (no newline characters)
252 ;; Not done, since as per XML specifications, the XML processor
253 ;; should always pass the whole string to the application.
254 ;; (while (string-match "\\s +" string pos)
255 ;; (setq string (replace-match " " t t string))
256 ;; (setq pos (1+ (match-beginning 0))))
258 (setq string (xml-substitute-special string))
260 (if (stringp (car children))