diff --git a/doc/ref/sxml.texi b/doc/ref/sxml.texi index 50c10ae63..66584bf89 100644 --- a/doc/ref/sxml.texi +++ b/doc/ref/sxml.texi @@ -55,13 +55,14 @@ to text. (use-modules (sxml simple)) @end example -@deffn {Scheme Procedure} xml->sxml [port] [#:namespaces='()] @ +@deffn {Scheme Procedure} xml->sxml [string-or-port] [#:namespaces='()] @ [#:declare-namespaces?=#t] [#:trim-whitespace?=#f] @ [#:entities='()] [#:default-entity-handler=#f] Use SSAX to parse an XML document into SXML. Takes one optional -argument, @var{port}, which defaults to the current input port. Returns -the resulting SXML document, and leaves @var{port} pointing at the next -available character in the port. +argument, @var{string-or-port}, which defaults to the current input +port. Returns the resulting SXML document. If @var{string-or-port} is +a port, it will be left pointing at the next available character in the +port. @end deffn 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. @example -(xml->sxml (open-input-string "")) +(xml->sxml "") @result{} (*TOP* (foo)) -(xml->sxml (open-input-string "text")) +(xml->sxml "text") @result{} (*TOP* (foo "text")) -(xml->sxml (open-input-string "text")) +(xml->sxml "text") @result{} (*TOP* (foo (@@ (kind "bar")) "text")) -(xml->sxml (open-input-string "")) +(xml->sxml "") @result{} (*TOP* (*PI* xml "version=\"1.0\"") (foo)) @end example @@ -88,19 +89,13 @@ for certain namespaces with the @code{#:namespaces} keyword argument to @code{xml->sxml}. @example -(xml->sxml - (open-input-string - "text")) +(xml->sxml "text") @result{} (*TOP* (http://example.org/ns1:foo "text")) -(xml->sxml - (open-input-string - "text") - #:namespaces '((ns1 . "http://example.org/ns1"))) +(xml->sxml "text" + #:namespaces '((ns1 . "http://example.org/ns1"))) @result{} (*TOP* (ns1:foo "text")) -(xml->sxml - (open-input-string - "") - #:namespaces '((ns2 . "http://example.org/ns2"))) +(xml->sxml "" + #:namespaces '((ns2 . "http://example.org/ns2"))) @result{} (*TOP* (foo (ns2:baz))) @end example @@ -109,10 +104,10 @@ user-given @code{#:namespaces} to be treated as if they were declared on the root element. @example -(xml->sxml (open-input-string "") +(xml->sxml "" #:namespaces '((ns2 . "http://example.org/ns2"))) @result{} error: undeclared namespace: `bar' -(xml->sxml (open-input-string "") +(xml->sxml "" #:namespaces '((ns2 . "http://example.org/ns2")) #:declare-namespaces? #t) @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. @example -(xml->sxml (open-input-string - "\n Alfie the parrot! \n")) +(xml->sxml "\n Alfie the parrot! \n") @result{} (*TOP* (foo "\n" (bar " Alfie the parrot! ") "\n") -(xml->sxml (open-input-string - "\n Alfie the parrot! \n") +(xml->sxml "\n Alfie the parrot! \n" #:trim-whitespace? #t) @result{} (*TOP* (foo (bar " Alfie the parrot! ")) @end example @@ -141,16 +134,16 @@ default, only the standard @code{<}, @code{>}, @code{&}, numeric character entities. @example -(xml->sxml (open-input-string "&")) +(xml->sxml "&") @result{} (*TOP* (foo "&")) -(xml->sxml (open-input-string " ")) +(xml->sxml " ") @result{} error: undefined entity: nbsp -(xml->sxml (open-input-string " ")) +(xml->sxml " ") @result{} (*TOP* (foo "\xa0")) -(xml->sxml (open-input-string " ") +(xml->sxml " " #:entities '((nbsp . "\xa0"))) @result{} (*TOP* (foo "\xa0")) -(xml->sxml (open-input-string "  &foo;") +(xml->sxml "  &foo;" #:default-entity-handler (lambda (port name) (case name diff --git a/module/sxml/simple.scm b/module/sxml/simple.scm index 4d06ff687..606975df9 100644 --- a/module/sxml/simple.scm +++ b/module/sxml/simple.scm @@ -76,14 +76,15 @@ ;; ;; * Parse external DTDs ;; -(define* (xml->sxml #:optional (port (current-input-port)) #:key +(define* (xml->sxml #:optional (string-or-port (current-input-port)) #:key (namespaces '()) (declare-namespaces? #t) (trim-whitespace? #f) (entities '()) (default-entity-handler #f)) "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 ;; that the user wants on elements of a given namespace in the ;; 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)) 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 (let ((*good-cache* (make-hash-table)))