mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 11:50:28 +02:00
xml->sxml argument can be a port or a string
* module/sxml/simple.scm (xml->sxml): Allow the optional arg to be a port or a string. * doc/ref/sxml.texi (Reading and Writing XML): Update docs.
This commit is contained in:
parent
1488753a66
commit
a14b6e1825
2 changed files with 31 additions and 33 deletions
|
@ -55,13 +55,14 @@ to text.
|
||||||
(use-modules (sxml simple))
|
(use-modules (sxml simple))
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@deffn {Scheme Procedure} xml->sxml [port] [#:namespaces='()] @
|
@deffn {Scheme Procedure} xml->sxml [string-or-port] [#:namespaces='()] @
|
||||||
[#:declare-namespaces?=#t] [#:trim-whitespace?=#f] @
|
[#:declare-namespaces?=#t] [#:trim-whitespace?=#f] @
|
||||||
[#:entities='()] [#:default-entity-handler=#f]
|
[#:entities='()] [#:default-entity-handler=#f]
|
||||||
Use SSAX to parse an XML document into SXML. Takes one optional
|
Use SSAX to parse an XML document into SXML. Takes one optional
|
||||||
argument, @var{port}, which defaults to the current input port. Returns
|
argument, @var{string-or-port}, which defaults to the current input
|
||||||
the resulting SXML document, and leaves @var{port} pointing at the next
|
port. Returns the resulting SXML document. If @var{string-or-port} is
|
||||||
available character in the port.
|
a port, it will be left pointing at the next available character in the
|
||||||
|
port.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
As is normal in SXML, XML elements parse as tagged lists. Attributes,
|
As is normal in SXML, XML elements parse as tagged lists. Attributes,
|
||||||
|
@ -71,13 +72,13 @@ This tag will contain the root element of the XML, but also any prior
|
||||||
processing instructions.
|
processing instructions.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
(xml->sxml (open-input-string "<foo/>"))
|
(xml->sxml "<foo/>")
|
||||||
@result{} (*TOP* (foo))
|
@result{} (*TOP* (foo))
|
||||||
(xml->sxml (open-input-string "<foo>text</foo>"))
|
(xml->sxml "<foo>text</foo>")
|
||||||
@result{} (*TOP* (foo "text"))
|
@result{} (*TOP* (foo "text"))
|
||||||
(xml->sxml (open-input-string "<foo kind=\"bar\">text</foo>"))
|
(xml->sxml "<foo kind=\"bar\">text</foo>")
|
||||||
@result{} (*TOP* (foo (@@ (kind "bar")) "text"))
|
@result{} (*TOP* (foo (@@ (kind "bar")) "text"))
|
||||||
(xml->sxml (open-input-string "<?xml version=\"1.0\"?><foo/>"))
|
(xml->sxml "<?xml version=\"1.0\"?><foo/>")
|
||||||
@result{} (*TOP* (*PI* xml "version=\"1.0\"") (foo))
|
@result{} (*TOP* (*PI* xml "version=\"1.0\"") (foo))
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
|
@ -88,18 +89,12 @@ for certain namespaces with the @code{#:namespaces} keyword argument to
|
||||||
@code{xml->sxml}.
|
@code{xml->sxml}.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
(xml->sxml
|
(xml->sxml "<foo xmlns=\"http://example.org/ns1\">text</foo>")
|
||||||
(open-input-string
|
|
||||||
"<foo xmlns=\"http://example.org/ns1\">text</foo>"))
|
|
||||||
@result{} (*TOP* (http://example.org/ns1:foo "text"))
|
@result{} (*TOP* (http://example.org/ns1:foo "text"))
|
||||||
(xml->sxml
|
(xml->sxml "<foo xmlns=\"http://example.org/ns1\">text</foo>"
|
||||||
(open-input-string
|
|
||||||
"<foo xmlns=\"http://example.org/ns1\">text</foo>")
|
|
||||||
#:namespaces '((ns1 . "http://example.org/ns1")))
|
#:namespaces '((ns1 . "http://example.org/ns1")))
|
||||||
@result{} (*TOP* (ns1:foo "text"))
|
@result{} (*TOP* (ns1:foo "text"))
|
||||||
(xml->sxml
|
(xml->sxml "<foo xmlns:bar=\"http://example.org/ns2\"><bar:baz/></foo>"
|
||||||
(open-input-string
|
|
||||||
"<foo xmlns:bar=\"http://example.org/ns2\"><bar:baz/></foo>")
|
|
||||||
#:namespaces '((ns2 . "http://example.org/ns2")))
|
#:namespaces '((ns2 . "http://example.org/ns2")))
|
||||||
@result{} (*TOP* (foo (ns2:baz)))
|
@result{} (*TOP* (foo (ns2:baz)))
|
||||||
@end example
|
@end example
|
||||||
|
@ -109,10 +104,10 @@ user-given @code{#:namespaces} to be treated as if they were declared on
|
||||||
the root element.
|
the root element.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
(xml->sxml (open-input-string "<foo><ns2:baz/></foo>")
|
(xml->sxml "<foo><ns2:baz/></foo>"
|
||||||
#:namespaces '((ns2 . "http://example.org/ns2")))
|
#:namespaces '((ns2 . "http://example.org/ns2")))
|
||||||
@result{} error: undeclared namespace: `bar'
|
@result{} error: undeclared namespace: `bar'
|
||||||
(xml->sxml (open-input-string "<foo><ns2:baz/></foo>")
|
(xml->sxml "<foo><ns2:baz/></foo>"
|
||||||
#:namespaces '((ns2 . "http://example.org/ns2"))
|
#:namespaces '((ns2 . "http://example.org/ns2"))
|
||||||
#:declare-namespaces? #t)
|
#:declare-namespaces? #t)
|
||||||
@result{} (*TOP* (foo (ns2:baz)))
|
@result{} (*TOP* (foo (ns2:baz)))
|
||||||
|
@ -124,11 +119,9 @@ whitespace in front, behind and between elements, treating it as
|
||||||
``unsignificant''. Whitespace in text fragments is left alone.
|
``unsignificant''. Whitespace in text fragments is left alone.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
(xml->sxml (open-input-string
|
(xml->sxml "<foo>\n<bar> Alfie the parrot! </bar>\n</foo>")
|
||||||
"<foo>\n<bar> Alfie the parrot! </bar>\n</foo>"))
|
|
||||||
@result{} (*TOP* (foo "\n" (bar " Alfie the parrot! ") "\n")
|
@result{} (*TOP* (foo "\n" (bar " Alfie the parrot! ") "\n")
|
||||||
(xml->sxml (open-input-string
|
(xml->sxml "<foo>\n<bar> Alfie the parrot! </bar>\n</foo>"
|
||||||
"<foo>\n<bar> Alfie the parrot! </bar>\n</foo>")
|
|
||||||
#:trim-whitespace? #t)
|
#:trim-whitespace? #t)
|
||||||
@result{} (*TOP* (foo (bar " Alfie the parrot! "))
|
@result{} (*TOP* (foo (bar " Alfie the parrot! "))
|
||||||
@end example
|
@end example
|
||||||
|
@ -141,16 +134,16 @@ default, only the standard @code{<}, @code{>}, @code{&},
|
||||||
numeric character entities.
|
numeric character entities.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
(xml->sxml (open-input-string "<foo>&</foo>"))
|
(xml->sxml "<foo>&</foo>")
|
||||||
@result{} (*TOP* (foo "&"))
|
@result{} (*TOP* (foo "&"))
|
||||||
(xml->sxml (open-input-string "<foo> </foo>"))
|
(xml->sxml "<foo> </foo>")
|
||||||
@result{} error: undefined entity: nbsp
|
@result{} error: undefined entity: nbsp
|
||||||
(xml->sxml (open-input-string "<foo> </foo>"))
|
(xml->sxml "<foo> </foo>")
|
||||||
@result{} (*TOP* (foo "\xa0"))
|
@result{} (*TOP* (foo "\xa0"))
|
||||||
(xml->sxml (open-input-string "<foo> </foo>")
|
(xml->sxml "<foo> </foo>"
|
||||||
#:entities '((nbsp . "\xa0")))
|
#:entities '((nbsp . "\xa0")))
|
||||||
@result{} (*TOP* (foo "\xa0"))
|
@result{} (*TOP* (foo "\xa0"))
|
||||||
(xml->sxml (open-input-string "<foo> &foo;</foo>")
|
(xml->sxml "<foo> &foo;</foo>"
|
||||||
#:default-entity-handler
|
#:default-entity-handler
|
||||||
(lambda (port name)
|
(lambda (port name)
|
||||||
(case name
|
(case name
|
||||||
|
|
|
@ -76,14 +76,15 @@
|
||||||
;;
|
;;
|
||||||
;; * Parse external DTDs
|
;; * Parse external DTDs
|
||||||
;;
|
;;
|
||||||
(define* (xml->sxml #:optional (port (current-input-port)) #:key
|
(define* (xml->sxml #:optional (string-or-port (current-input-port)) #:key
|
||||||
(namespaces '())
|
(namespaces '())
|
||||||
(declare-namespaces? #t)
|
(declare-namespaces? #t)
|
||||||
(trim-whitespace? #f)
|
(trim-whitespace? #f)
|
||||||
(entities '())
|
(entities '())
|
||||||
(default-entity-handler #f))
|
(default-entity-handler #f))
|
||||||
"Use SSAX to parse an XML document into SXML. Takes one optional
|
"Use SSAX to parse an XML document into SXML. Takes one optional
|
||||||
argument, @var{port}, which defaults to the current input port."
|
argument, @var{string-or-port}, which defaults to the current input
|
||||||
|
port."
|
||||||
;; NAMESPACES: alist of PREFIX -> URI. Specifies the symbol prefix
|
;; NAMESPACES: alist of PREFIX -> URI. Specifies the symbol prefix
|
||||||
;; that the user wants on elements of a given namespace in the
|
;; that the user wants on elements of a given namespace in the
|
||||||
;; resulting SXML, regardless of the abbreviated namespaces defined in
|
;; resulting SXML, regardless of the abbreviated namespaces defined in
|
||||||
|
@ -178,7 +179,11 @@ argument, @var{port}, which defaults to the current input port."
|
||||||
(list '*PI* pi-tag (ssax:read-pi-body-as-string port))
|
(list '*PI* pi-tag (ssax:read-pi-body-as-string port))
|
||||||
seed))))))
|
seed))))))
|
||||||
|
|
||||||
`(*TOP* ,@(reverse (parser port '()))))
|
(let* ((port (if (string? string-or-port)
|
||||||
|
(open-input-string string-or-port)
|
||||||
|
string-or-port))
|
||||||
|
(elements (reverse (parser port '()))))
|
||||||
|
`(*TOP* ,@elements)))
|
||||||
|
|
||||||
(define check-name
|
(define check-name
|
||||||
(let ((*good-cache* (make-hash-table)))
|
(let ((*good-cache* (make-hash-table)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue