Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jasom
Created February 22, 2018 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasom/2aefd041ccafc5108040ec06f4959c1a to your computer and use it in GitHub Desktop.
Save jasom/2aefd041ccafc5108040ec06f4959c1a to your computer and use it in GitHub Desktop.
Lisp XML namespaces
(cxml:parse #P"foo.xml" (cxml-xmls:make-xmls-builder)) ;;=>
;(("stream" . "http://etherx.jabber.org/streams")
; ((("lang" . "http://www.w3.org/XML/1998/namespace") "en")
; (("stream" . "http://www.w3.org/2000/xmlns/")
; "http://etherx.jabber.org/streams")
; ("version" "1.0") ("to" "localhost")
; ((NIL . "http://www.w3.org/2000/xmlns/") "jabber:client"))
; "
; "
; (("iq" . "jabber:client")
; (("id" "b70fb10f-f68a-4be2-9d40-99ced9e338c6") ("type" "set")) "
; "
; (("bind" . "urn:ietf:params:xml:ns:xmpp-bind")
; (((NIL . "http://www.w3.org/2000/xmlns/")
; "urn:ietf:params:xml:ns:xmpp-bind"))
; "
; "
; (("resource" . "urn:ietf:params:xml:ns:xmpp-bind") NIL "Gajim") "
; ")
; "
; ")
; "
; "
; (("iq" . "jabber:client")
; (("id" "8350ebb5-4f16-4b34-8780-b7acaefe6c47") ("type" "set")
; ("from" "jmiller@localhost/Gajim"))
; "
; "
; (("session" . "urn:ietf:params:xml:ns:xmpp-session")
; (((NIL . "http://www.w3.org/2000/xmlns/")
; "urn:ietf:params:xml:ns:xmpp-session")))
; "
; ")
; "
;")
<?xml version='1.0'?>
<stream:stream xmlns="jabber:client" to="localhost" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="en" >
<iq type="set" id="b70fb10f-f68a-4be2-9d40-99ced9e338c6">
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
<resource>Gajim</resource>
</bind>
</iq>
<iq from="jmiller@localhost/Gajim" type="set" id="8350ebb5-4f16-4b34-8780-b7acaefe6c47">
<session xmlns="urn:ietf:params:xml:ns:xmpp-session" />
</iq>
</stream:stream>
(let ((h (cxml-xmls:make-xmls-builder)))
(klacks:with-open-source (s (cxml:make-source #P"foo.xml"))
(klacks:serialize-source s h))
(sax:end-document h))
;; =>
;(("stream" . "http://etherx.jabber.org/streams")
; ((("lang" . "http://www.w3.org/XML/1998/namespace") "en")
; (("stream" . "http://www.w3.org/2000/xmlns/")
; "http://etherx.jabber.org/streams")
; ("version" "1.0") ("to" "localhost")
; ((NIL . "http://www.w3.org/2000/xmlns/") "jabber:client"))
; "
; "
; (("iq" . "jabber:client")
; (("id" "b70fb10f-f68a-4be2-9d40-99ced9e338c6") ("type" "set")) "
; "
; (("bind" . "urn:ietf:params:xml:ns:xmpp-bind")
; (((NIL . "http://www.w3.org/2000/xmlns/")
; "urn:ietf:params:xml:ns:xmpp-bind"))
; "
; "
; (("resource" . "urn:ietf:params:xml:ns:xmpp-bind") NIL "Gajim") "
; ")
; "
; ")
; "
; "
; (("iq" . "urn:ietf:params:xml:ns:xmpp-bind")
; (("id" "8350ebb5-4f16-4b34-8780-b7acaefe6c47") ("type" "set")
; ("from" "jmiller@localhost/Gajim"))
; "
; "
; (("session" . "urn:ietf:params:xml:ns:xmpp-session")
; (((NIL . "http://www.w3.org/2000/xmlns/")
; "urn:ietf:params:xml:ns:xmpp-session")))
; "
; ")
; "
;")
(s-xml:parse-xml-file "foo.xml") ;=>
;((|stream|:|stream| :|xmlns| "jabber:client" :|to| "localhost" :|version| "1.0"
; :|xmlns:stream| "http://etherx.jabber.org/streams" XML:|lang| "en")
; ((|jabber.client|:|iq| :|type| "set" :|id|
; "b70fb10f-f68a-4be2-9d40-99ced9e338c6")
; ((NS-1:|bind| :|xmlns| "urn:ietf:params:xml:ns:xmpp-bind")
; (NS-1:|resource| "Gajim")))
; ((|jabber.client|:|iq| :|from| "jmiller@localhost/Gajim" :|type| "set" :|id|
; "8350ebb5-4f16-4b34-8780-b7acaefe6c47")
; ((NS-2:|session| :|xmlns| "urn:ietf:params:xml:ns:xmpp-session"))))
(with-open-file (s "foo.xml") (xmls:parse s)) ;=>
;(("stream" . "http://etherx.jabber.org/streams")
; (("lang" "en") ("version" "1.0") ("to" "localhost"))
; (("iq" . "jabber:client")
; (("id" "b70fb10f-f68a-4be2-9d40-99ced9e338c6") ("type" "set"))
; (("bind" . "urn:ietf:params:xml:ns:xmpp-bind") NIL
; (("resource" . "urn:ietf:params:xml:ns:xmpp-bind") NIL "Gajim")))
; (("iq" . "urn:ietf:params:xml:ns:xmpp-bind")
; (("id" "8350ebb5-4f16-4b34-8780-b7acaefe6c47") ("type" "set")
; ("from" "jmiller@localhost/Gajim"))
; (("session" . "urn:ietf:params:xml:ns:xmpp-session") NIL)))
@jasom
Copy link
Author

jasom commented Feb 22, 2018

The second "iq" element should be in the "jabber:client" ns as that is the xmlns declared by its parent element. With xmls and klacks however, the default namespace does not reset after the "bind" element closes. s-xml and cxml's SAX interface gets it right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment