[REBOL] Re: Rebol & XML
From: AJMartin:orcon at: 6-Aug-2003 22:44
A better version of 'Load-XML:
Load-XML: function [
[catch] "Loads XML as a Rebol compatible block of values."
XML [string! file!] "The XML string or file."
] [
Content Stack Attribute Value Attribute_Value^ Namespace_Name^
Text^ Declaration^ Name Text Element^
] [
Content: make block! 10
Stack: make block! 10
Namespace_Name^: [Alpha any AlphaDigit opt [#":" Alpha any Alphadigit]]
Attribute_Value^: [
WS* copy Attribute Namespace_Name^ {="} copy Value to #"^"" skip (
insert tail Content reduce [
to issue! Attribute
Value
]
)
]
Text^: complement charset #"<"
Declaration^: [
"<?xml" (Name: 'xml) any Attribute_Value^ WS? "?>" (
Content: reduce [Name Content]
)
]
Element^: [
"<!--" thru "-->" | #"<" Z: copy Name [
Namespace_Name^ (
push/only Stack reduce [
to word! Name Content
]
Content: make block! 6
)
] any [Attribute_Value^] WS? [
"/>" | #">" (push Stack Name) [
some [
copy Text some Text^ (
if not empty? trim/lines Text [
insert tail Content Text
]
)
| Element^
]
] "</" (Name: pop Stack) Name WS? #">"
] (
insert tail last first Stack reduce [
to word! Name
either 1 = length? Content [first Content] [Content]
]
set [Name Content] pop Stack
)
]
if file? XML [
XML: read XML
]
all [
parse/all/case XML [
WS? Declaration^
WS? Element^
WS? end
]
Content
]
]
With Bryan's test XML (modified by me!):
XML: {
<?xml version="1.0"?>
<doc>
here's some text
<p>para 1</p>
<p>para 2</p>
here's some more text
<p:p xmlns:p="http://www.uris.org/p">not a para</p:p>
</doc>
}
I'm getting:
[
xml [#version "1.0"] doc [
"here's some text" p "para 1"
p "para 2" "here's some more text"
p:p [#xmlns:p "http://www.uris.org/p" "not a para"]]]
Note that the "p:p" is converted to a word! value, currently. I feel it
should be a path! value, like this:
p/p [xmlns/p "http://www.uris.org/p" "not a para"]
As too should the attribute names as well.
What do people think?
Andrew J Martin
ICQ: 26227169
http://www.rebol.it/Valley/
http://valley.orcon.net.nz/
http://Valley.150m.com/