From: mah@everybody.org (Mark A. Hershberger)
Cc: "Jose M. Vidal" <vidal@sc.edu>
Subject: Re: Adding namespace support to xml.el
Date: Thu, 12 Jun 2003 13:28:35 -0500 [thread overview]
Message-ID: <87u1avnoa4.fsf@superman.everybody.org> (raw)
Here is a cleaned up copy of the patch. I took out a couple of
irrelevant changes and added "-b" so it'd be easier to see the actual
differences. Also fixed a comment and docstring or two.
--- xml.el 30 May 2003 16:02:26 -0000 1.21
+++ xml.el 12 Jun 2003 18:24:30 -0000
@@ -121,11 +121,12 @@
;;*******************************************************************
;;;###autoload
-(defun xml-parse-file (file &optional parse-dtd)
+(defun xml-parse-file (file &optional parse-dtd parse-ns)
"Parse the well-formed XML file FILE.
If FILE is already visited, use its buffer and don't kill it.
Returns the top node with all its children.
-If PARSE-DTD is non-nil, the DTD is parsed rather than skipped."
+If PARSE-DTD is non-nil, the DTD is parsed rather than skipped.
+If PARSE-NS is non-nil, then QNAMES are expanded."
(let ((keep))
(if (get-file-buffer file)
(progn
@@ -137,7 +138,7 @@
(let ((xml (xml-parse-region (point-min)
(point-max)
(current-buffer)
- parse-dtd)))
+ parse-dtd parse-ns)))
(if keep
(goto-char keep)
(kill-buffer (current-buffer)))
@@ -184,13 +185,14 @@
;; prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
;;;###autoload
-(defun xml-parse-region (beg end &optional buffer parse-dtd)
+(defun xml-parse-region (beg end &optional buffer parse-dtd parse-ns)
"Parse the region from BEG to END in BUFFER.
If BUFFER is nil, it defaults to the current buffer.
Returns the XML list for the region, or raises an error if the region
-is not a well-formed XML file.
+is not well-formed XML.
If PARSE-DTD is non-nil, the DTD is parsed rather than skipped,
-and returned as the first element of the list."
+and returned as the first element of the list.
+If PARSE-NS is non-nil, then QNAMES are expanded."
(save-restriction
(narrow-to-region beg end)
;; Use fixed syntax table to ensure regexp char classes and syntax
@@ -209,7 +211,7 @@
(if xml
;; translation of rule [1] of XML specifications
(error "XML files can have only one toplevel tag")
- (setq result (xml-parse-tag parse-dtd))
+ (setq result (xml-parse-tag parse-dtd parse-ns))
(cond
((null result))
((listp (car result))
@@ -224,21 +226,28 @@
(nreverse xml)))))))
-(defun xml-parse-tag (&optional parse-dtd)
+(defun xml-parse-tag (&optional parse-dtd parse-ns)
"Parse the tag at point.
If PARSE-DTD is non-nil, the DTD of the document, if any, is parsed and
returned as the first element in the list.
+If PARSE-NS is non-nil, then QNAMES are expanded.
Returns one of:
- a list : the matching node
- nil : the point is not looking at a tag.
- a pair : the first element is the DTD, the second is the node."
+ (let ((xml-ns (if (consp parse-ns)
+ parse-ns
+ (if parse-ns
+ (list
+ (cons "" "")
+ (cons "xmlns" "http://www.w3.org/2000/xmlns/"))))))
(cond
;; Processing instructions (like the <?xml version="1.0"?> tag at the
;; beginning of a document).
((looking-at "<\\?")
(search-forward "?>")
(skip-syntax-forward " ")
- (xml-parse-tag parse-dtd))
+ (xml-parse-tag parse-dtd xml-ns))
;; Character data (CDATA) sections, in which no tag should be interpreted
((looking-at "<!\\[CDATA\\[")
(let ((pos (match-end 0)))
@@ -253,8 +262,8 @@
(xml-skip-dtd))
(skip-syntax-forward " ")
(if dtd
- (cons dtd (xml-parse-tag))
- (xml-parse-tag))))
+ (cons dtd (xml-parse-tag nil xml-ns))
+ (xml-parse-tag nil xml-ns))))
;; skip comments
((looking-at "<!--")
(search-forward "-->")
@@ -270,6 +279,48 @@
(children (list (xml-parse-attlist) (intern node-name)))
pos)
+ ;; add the xmlns:* attrs to our cache
+ (when (consp xml-ns)
+ (mapcar
+ (lambda (attr)
+ (let* ((splitup (split-string (symbol-name (car attr)) ":"))
+ (prefix (nth 0 splitup))
+ (lname (nth 1 splitup)))
+ (when (string= "xmlns" prefix)
+ (setq xml-ns (append (list (cons (if lname
+ lname
+ "")
+ (cdr attr)))
+ xml-ns)))))
+ (car children))
+
+ ;; expand element names
+ (let* ((splitup (split-string (symbol-name (cadr children)) ":"))
+ (lname (or (nth 1 splitup)
+ (nth 0 splitup)))
+ (prefix (if (nth 1 splitup)
+ (nth 0 splitup)
+ "")))
+ (setcdr children (list
+ (intern (concat "{"
+ (cdr (assoc-string prefix xml-ns))
+ "}" lname)))))
+
+ ;; expand attribute names
+ (mapcar
+ (lambda (attr)
+ (let* ((splitup (split-string (symbol-name (car attr)) ":"))
+ (lname (or (nth 1 splitup)
+ (nth 0 splitup)))
+ (prefix (if (nth 1 splitup)
+ (nth 0 splitup)
+ (caar xml-ns))))
+
+ (setcar attr (intern (concat "{"
+ (cdr (assoc-string prefix xml-ns))
+ "}" lname)))))
+ (car children)))
+
;; is this an empty element ?
(if (looking-at "/>")
(progn
@@ -289,7 +340,7 @@
(error "XML: Invalid end tag (expecting %s) at pos %d"
node-name (point)))
((= (char-after) ?<)
- (let ((tag (xml-parse-tag)))
+ (let ((tag (xml-parse-tag nil xml-ns)))
(when tag
(push tag children))))
(t
@@ -321,7 +372,7 @@
;; This was an invalid start tag
(error "XML: Invalid attribute list")))))
(t ;; This is not a tag.
- (error "XML: Invalid character"))))
+ (error "XML: Invalid character")))))
(defun xml-parse-attlist ()
"Return the attribute-list after point.
next reply other threads:[~2003-06-12 18:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-12 18:28 Mark A. Hershberger [this message]
-- strict thread matches above, loose matches on Subject: below --
2003-06-12 16:07 Adding namespace support to xml.el Mark A. Hershberger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87u1avnoa4.fsf@superman.everybody.org \
--to=mah@everybody.org \
--cc=vidal@sc.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).