From: Ricardo Wurmus <rekado@elephly.net>
To: Andy Wingo <wingo@pobox.com>
Cc: guile-devel@gnu.org
Subject: Re: sxml simple, sxml->xml and namespaces
Date: Tue, 21 Jun 2016 22:36:18 +0200 [thread overview]
Message-ID: <87k2hi1dq5.fsf@elephly.net> (raw)
In-Reply-To: <87porchxfl.fsf@pobox.com>
Andy Wingo <wingo@pobox.com> writes:
> On Mon 20 Jun 2016 12:52, Ricardo Wurmus <rekado@elephly.net> writes:
>
>> Andy Wingo <wingo@pobox.com> 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
next prev parent reply other threads:[~2016-06-21 20:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-08 20:55 sxml simple, sxml->xml and namespaces tomas
2016-06-20 8:56 ` Andy Wingo
2016-06-20 10:52 ` Ricardo Wurmus
2016-06-20 11:20 ` tomas
2016-06-20 12:11 ` Andy Wingo
2016-06-21 20:36 ` Ricardo Wurmus [this message]
2016-06-21 20:58 ` Andy Wingo
2016-06-20 11:18 ` tomas
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/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87k2hi1dq5.fsf@elephly.net \
--to=rekado@elephly.net \
--cc=guile-devel@gnu.org \
--cc=wingo@pobox.com \
/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.
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).