[REBOL] Re: Rebol & XML
From: andrew:martin:colenso:school at: 7-Aug-2003 14:07
This version handles name spaces better.
[
Rebol [
Name: 'Load-XML
File: %Load-XML.r
Title: "Load XML"
Author: "A J Martin"
eMail: [Rebol--orcon--net--nz]
Web: http://www.rebol.it/Valley/
Owner: "Aztecnology"
Rights: "Copyright (c) 2003 A J Martin, Aztecnology."
Tabs: 4
Purpose: "Loads XML as a Rebol compatible block of values."
Language: 'English
Acknowledgements: [
"bryan" [bry--itnisk--com]
"Volker Nitsch" [nitsch-lists--netcologne--de]
"Brett Handley" [brett--codeconscious--com]
]
Needs: [%"Common Parse Values.r" %Push.r %Pop.r]
Date: 7/August/2003
Version: 1.5.0
]
make object! [
Namespace_Name: func [Name [string! word! path!] Type
[datatype!]] [
either string? :Name [
either found? find Name #":" [
to path! map parse/all Name ":" func
[Word [string!]] [
to word! Word
]
] [
to Type Name
]
] [
:Name
]
]
NameChar: union AlphaDigit charset ".-_:"
Name^: [[Alpha | #"-" | #"_"] any NameChar]
Comment^: ["<!--" thru "-->"]
PI^: ["<?" thru "?>"]
Miscellaneous^: [any [PI^ | Comment^]]
set '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^
Text^ Declaration^ Name Text Element^ Miscellaneous^
] [
Content: make block! 4
Stack: make block! 10
Attribute_Value^: [
WS* copy Attribute Name^ #"=" [
#"^"" copy Value to #"^"" skip
| #"'" copy Value to #"'" skip
] (
insert tail Content reduce [
Namespace_Name Attribute issue!
Value
]
)
] =09
Text^: complement charset #"<"
Declaration^: [
"<?xml" (Name: "xml") any Attribute_Value^ WS?
?>
(
Content: reduce [
Namespace_Name Name word!
Content
]
)
]
Element^: [
#"<" copy Name [
Name^ (
push/only Stack reduce [
Namespace_Name Name
word!
Content
]
Content: make block! 6
)
] any [Attribute_Value^] WS? [
"/>" | #">" (push Stack Name) [
some [
Comment^ | PI^ |
Element^
| "<![CDATA[" copy Text
to "]]>" 3 skip (
if not empty?
trim Text [
insert
tail Content Text
]
)
| copy Text some Text^ (
if not empty?
trim/lines Text [
insert
tail Content Text
]
)
]
] "</" (Name: pop Stack) Name
WS? #">"
] (
insert tail last first Stack reduce [
Namespace_Name Name word!
either 1 = length? Content
[first Content] [Content]
]
set [Name Content] pop Stack
)
]
=09
if file? XML [
XML: read XML
]
all [
parse/all/case XML [
WS? Declaration^
WS? Miscellaneous^
WS? Element^
WS? Miscellaneous^
WS? end
]
Content
]
]
]
]
XML: {<?xml version="1.0"?>
<document>
<h>Heading</h>
<p class="Initial">Hi this is a <b>bold</b> paragraph.</p>
<p>this is a <span>stuff</span> paragraph</p>
<![CDATA[<greeting>Hello, world!</greeting>]]>
here's some more text <!-- A comment! -->
<p:p xmlns:p="http://www.uris.org/p">not a para</p:p>
<PERSON foo="bar">
<NAME bar="blech">Fred</NAME>
<AGE>24</AGE>
<ADDRESS>
<STREET>123 Main Street</STREET>
<CITY>Ukiah</CITY>
<STATE>CA</STATE>
</ADDRESS>
</PERSON>
</document>
}
[xml [#version "1.0"] document [h "Heading" p [#class "Initial" "Hi this
is
a" b "bold" "paragraph."] p ["this is a" span "stuff" "paragraph"]
<greet
ing>Hello, world!</greeting>
"here's some more text" p/p [xmlns/p
http://
www.uris.org/p
"not a para"] PERSON [#foo "bar" NAME [#bar "blech"
Fred
]
AGE "24" ADDRESS [STREET "123 Main Street" CITY "Ukiah" STATE "CA"]]]]
Andrew J Martin
Attendance Officer &
Information Systems Trouble Shooter
Colenso High School
Arnold Street, Napier.
Tel: 64-6-8310180 ext 826
Fax: 64-6-8336759
http://colenso.net/scripts/Wiki.r?AJM
http://www.colenso.school.nz/
DISCLAIMER: Colenso High School and its Board of Trustees is not responsible (or legally
liable) for materials distributed to or acquired from user e-mail accounts. You can report
any
misuse of an e-mail account to our ICT Manager and the complaint will be investigated.
(Misuse can come in many forms, but can be viewed as any material sent/received that
indicate or suggest pornography, unethical or illegal solicitation, racism, sexism, inappropriate
language and/or other issues described in our Acceptable Use Policy.)
All outgoing messages are certified virus-free by McAfee GroupShield Exchange 5.10.285.0
Phone: +64 6 843 5095 or Fax: +64 6 833 6759 or E-mail: [postmaster--colenso--school--nz]