[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: sxml simple, sxml->xml and namespaces
From: |
Ricardo Wurmus |
Subject: |
Re: sxml simple, sxml->xml and namespaces |
Date: |
Tue, 21 Jun 2016 22:36:18 +0200 |
User-agent: |
mu4e 0.9.16; emacs 24.5.1 |
Andy Wingo <address@hidden> writes:
> On Mon 20 Jun 2016 12:52, Ricardo Wurmus <address@hidden> writes:
>
>> Andy Wingo <address@hidden> writes:
>>
>>> Apologies for the long delay here. I'm with you regarding namespaces
>>> and sxml->xml. In the past I made sure to always get the namespaces
>>> attached to the root element via the @ xmlns attributes, and then have
>>> namespaced uses just be local names, not qnames, and that way sxml->xml
>>> works fine. But, perhaps that doesn't cover all cases in a nice way.
>>> Do you still have thoughts on this patch? Is the right thing for you?
>>> In any case we need better documentation in the manual about how to deal
>>> with namespaces and SXML, in practice, with examples.
>>
>> Here is another proposal, mirroring what is done in “xml->sxml”:
>
> Neat! Can you elaborate on how it is supposed to work? In a final form
> it would need documentation, tests, and an update to the docstring, but
> I'd be interested in some xml->sxml->xml round trips as an example.
This is the same patch I sent to the discussion of bug#20339 about a
year ago.
The patch is not very ambitious: it only gives the user a way around the
error by letting them pass an alist of namespaces. The patched
“sxml->xml” does not attempt to be smart about anything. It will still
fail if it encounters an undeclared namespace. My primary goal was to
get around the error. Maybe “sxml->xml” really should be smarter than
that.
What follows is a copy of my original message:
>> Since xml->sxml accepts a namespace alist I suppose it would make sense
>> to extend sxml->xml to do the same.
Attached is a minimal patch to extend "sxml->xml" such that it accepts an
optional keyword argument "namespaces" with an alist of prefixes to
URLs, analogous to "xml->sxml".
When the namespaces alist is provided, "xmlns:prefix=url" attributes are
prepended to the element's list of attributes.
;; Define SVG document with namespaces
(define the-svg "<svg xmlns='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink'>
<rect x='5' y='5' width='20' height='20'
stroke-width='2' stroke='purple' fill='yellow'
id='rect1' />
<rect x='30' y='5' width='20' height='20'
ry='5' rx='8' stroke-width='2' stroke='purple' fill='blue'
xlink:href='#rect1' />
</svg>")
;; Define alist of namespaces
(define ns '((svg . "http://www.w3.org/2000/svg")
(xlink . "http://www.w3.org/1999/xlink")))
;; Convert to SXML, abbreviate namespaces according to ns alist
(define the-sxml (xml->sxml the-svg #:namespaces ns))
;; Convert back to XML
(sxml->xml the-sxml #:namespaces ns)
=> <svg:svg xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<svg:rect y="5" x="5"
width="20"
stroke-width="2"
stroke="purple"
id="rect1"
height="20"
fill="yellow" />
<svg:rect xlink:href="#rect1"
y="5" x="30"
width="20"
stroke-width="2"
stroke="purple"
ry="5" rx="8"
height="20"
fill="blue" />
</svg:svg>
~~ Ricardo