diff options
Diffstat (limited to 'lisp/xml.el')
| -rw-r--r-- | lisp/xml.el | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/lisp/xml.el b/lisp/xml.el index 408c13ab39b..ab87125356d 100644 --- a/lisp/xml.el +++ b/lisp/xml.el | |||
| @@ -27,13 +27,13 @@ | |||
| 27 | 27 | ||
| 28 | ;; This file contains a somewhat incomplete non-validating XML parser. It | 28 | ;; This file contains a somewhat incomplete non-validating XML parser. It |
| 29 | ;; parses a file, and returns a list that can be used internally by | 29 | ;; parses a file, and returns a list that can be used internally by |
| 30 | ;; any other lisp libraries. | 30 | ;; any other Lisp libraries. |
| 31 | 31 | ||
| 32 | ;;; FILE FORMAT | 32 | ;;; FILE FORMAT |
| 33 | 33 | ||
| 34 | ;; The document type declaration may either be ignored or (optionally) | 34 | ;; The document type declaration may either be ignored or (optionally) |
| 35 | ;; parsed, but currently the parsing will only accept element | 35 | ;; parsed, but currently the parsing will only accept element |
| 36 | ;; declarations. The XML file is assumed to be well-formed. In case | 36 | ;; declarations. The XML file is assumed to be well-formed. In case |
| 37 | ;; of error, the parsing stops and the XML file is shown where the | 37 | ;; of error, the parsing stops and the XML file is shown where the |
| 38 | ;; parsing stopped. | 38 | ;; parsing stopped. |
| 39 | ;; | 39 | ;; |
| @@ -44,7 +44,7 @@ | |||
| 44 | ;; <node2 attr3="name3" attr4="name4">value2</node2> | 44 | ;; <node2 attr3="name3" attr4="name4">value2</node2> |
| 45 | ;; <node3 attr5="name5" attr6="name6">value3</node3> | 45 | ;; <node3 attr5="name5" attr6="name6">value3</node3> |
| 46 | ;; </node1> | 46 | ;; </node1> |
| 47 | ;; Of course, the name of the nodes and attributes can be anything. There can | 47 | ;; Of course, the name of the nodes and attributes can be anything. There can |
| 48 | ;; be any number of attributes (or none), as well as any number of children | 48 | ;; be any number of attributes (or none), as well as any number of children |
| 49 | ;; below the nodes. | 49 | ;; below the nodes. |
| 50 | ;; | 50 | ;; |
| @@ -86,7 +86,18 @@ | |||
| 86 | 86 | ||
| 87 | (defsubst xml-node-name (node) | 87 | (defsubst xml-node-name (node) |
| 88 | "Return the tag associated with NODE. | 88 | "Return the tag associated with NODE. |
| 89 | The tag is a lower-case symbol." | 89 | Without namespace-aware parsing, the tag is a symbol. |
| 90 | |||
| 91 | With namespace-aware parsing, the tag is a cons of a string | ||
| 92 | representing the uri of the namespace with the local name of the | ||
| 93 | tag. For example, | ||
| 94 | |||
| 95 | <foo> | ||
| 96 | |||
| 97 | would be represented by | ||
| 98 | |||
| 99 | '(\"\" . \"foo\")." | ||
| 100 | |||
| 90 | (car node)) | 101 | (car node)) |
| 91 | 102 | ||
| 92 | (defsubst xml-node-attributes (node) | 103 | (defsubst xml-node-attributes (node) |
| @@ -101,17 +112,17 @@ This is a list of nodes, and it can be nil." | |||
| 101 | 112 | ||
| 102 | (defun xml-get-children (node child-name) | 113 | (defun xml-get-children (node child-name) |
| 103 | "Return the children of NODE whose tag is CHILD-NAME. | 114 | "Return the children of NODE whose tag is CHILD-NAME. |
| 104 | CHILD-NAME should be a lower case symbol." | 115 | CHILD-NAME should match the value returned by `xml-node-name'." |
| 105 | (let ((match ())) | 116 | (let ((match ())) |
| 106 | (dolist (child (xml-node-children node)) | 117 | (dolist (child (xml-node-children node)) |
| 107 | (if child | 118 | (if (and (listp child) |
| 108 | (if (equal (xml-node-name child) child-name) | 119 | (equal (xml-node-name child) child-name)) |
| 109 | (push child match)))) | 120 | (push child match))) |
| 110 | (nreverse match))) | 121 | (nreverse match))) |
| 111 | 122 | ||
| 112 | (defun xml-get-attribute-or-nil (node attribute) | 123 | (defun xml-get-attribute-or-nil (node attribute) |
| 113 | "Get from NODE the value of ATTRIBUTE. | 124 | "Get from NODE the value of ATTRIBUTE. |
| 114 | Return `nil' if the attribute was not found. | 125 | Return nil if the attribute was not found. |
| 115 | 126 | ||
| 116 | See also `xml-get-attribute'." | 127 | See also `xml-get-attribute'." |
| 117 | (cdr (assoc attribute (xml-node-attributes node)))) | 128 | (cdr (assoc attribute (xml-node-attributes node)))) |
| @@ -236,7 +247,8 @@ If PARSE-NS is non-nil, then QNAMES are expanded." | |||
| 236 | (nreverse xml))))))) | 247 | (nreverse xml))))))) |
| 237 | 248 | ||
| 238 | (defun xml-maybe-do-ns (name default xml-ns) | 249 | (defun xml-maybe-do-ns (name default xml-ns) |
| 239 | "Perform any namespace expansion. NAME is the name to perform the expansion on. | 250 | "Perform any namespace expansion. |
| 251 | NAME is the name to perform the expansion on. | ||
| 240 | DEFAULT is the default namespace. XML-NS is a cons of namespace | 252 | DEFAULT is the default namespace. XML-NS is a cons of namespace |
| 241 | names to uris. When namespace-aware parsing is off, then XML-NS | 253 | names to uris. When namespace-aware parsing is off, then XML-NS |
| 242 | is nil. | 254 | is nil. |
| @@ -325,10 +337,8 @@ Returns one of: | |||
| 325 | (push (cons (cdar attr) (intern (concat ":" (cdr attr)))) | 337 | (push (cons (cdar attr) (intern (concat ":" (cdr attr)))) |
| 326 | xml-ns)))) | 338 | xml-ns)))) |
| 327 | 339 | ||
| 328 | ;; expand element names | 340 | (setq children (list attrs (xml-maybe-do-ns node-name "" xml-ns))) |
| 329 | (setq node-name (list (xml-maybe-do-ns node-name "" xml-ns))) | ||
| 330 | 341 | ||
| 331 | (setq children (list attrs node-name)) | ||
| 332 | ;; is this an empty element ? | 342 | ;; is this an empty element ? |
| 333 | (if (looking-at "/>") | 343 | (if (looking-at "/>") |
| 334 | (progn | 344 | (progn |
| @@ -383,8 +393,8 @@ Returns one of: | |||
| 383 | (error "XML: Invalid character"))))) | 393 | (error "XML: Invalid character"))))) |
| 384 | 394 | ||
| 385 | (defun xml-parse-attlist (&optional xml-ns) | 395 | (defun xml-parse-attlist (&optional xml-ns) |
| 386 | "Return the attribute-list after point. Leave point at the | 396 | "Return the attribute-list after point. |
| 387 | first non-blank character after the tag." | 397 | Leave point at the first non-blank character after the tag." |
| 388 | (let ((attlist ()) | 398 | (let ((attlist ()) |
| 389 | end-pos name) | 399 | end-pos name) |
| 390 | (skip-syntax-forward " ") | 400 | (skip-syntax-forward " ") |
| @@ -575,7 +585,7 @@ This follows the rule [28] in the XML specifications." | |||
| 575 | 585 | ||
| 576 | ;; Fixme: Take declared entities from the DTD when they're available. | 586 | ;; Fixme: Take declared entities from the DTD when they're available. |
| 577 | (defun xml-substitute-entity (match) | 587 | (defun xml-substitute-entity (match) |
| 578 | "Subroutine of xml-substitute-special." | 588 | "Subroutine of `xml-substitute-special'." |
| 579 | (save-match-data | 589 | (save-match-data |
| 580 | (let ((match1 (match-string 1 str))) | 590 | (let ((match1 (match-string 1 str))) |
| 581 | (cond ((string= match1 "lt") "<") | 591 | (cond ((string= match1 "lt") "<") |