[REBOL] Re: generating XML ?
From: al:bri:xtra at: 15-Jun-2002 14:20
Jason wrote:
> What do you suggest for writing XML from REBOL, in particular exporting
REBOL blocks and nested blocks as XML?
I'd suggest using my ML and Build-Tag functions.
Here's an example of their use (XHTML):
Dialect: [
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
DTD/xhtml1-transitional.dtd
html [
head [
title (Title)
link/rel/type/href "stylesheet" "text/css" (Location %Stylesheet.css)
]
(Body-Attributes) [
table [
tr [
td/width (Margin) " "
td/width (Width) [
(eText-Dialect)
hr
a/href (Valley/Core/File) "To the Core."
]
td " "
]
]
]
]
]
Here's another example from my Wiki:
print ML compose/deep [
html [
head [
title (Heading)
link/rel/type/href "stylesheet" "text/css" %/Stylesheet.css
]
body [
(eText/Wiki/Base read Directory/:File join Action #"?")
hr
table/width "100%" [
tr [
td/align "left" [
form/method/action "GET" (Action) [
input/type/value "submit" "Wiki Index"
]
]
td/align "center" [
form/method/action "GET" (Action) [
label [
"Search: " input/type/name/value "text" (
first Search_Rule
) ""
]
input/type/value "submit" "Search"
]
]
td/align "right" [
form/method/action "GET" (Action) [
input/type/value "submit" "Edit"
input/type/name/value "hidden" (first Edit_Rule) (Title)
]
]
]
]
]
]
]
I hope that helps!
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
-><-
Watch out for line breaks!
[
Rebol [
Name: 'ML
Title: "ML"
File: %ML.r
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 8/June/2002
Comments: {
ML generates XML-like markup language from words, paths and blocks.
ML can generate HTML, XHTML, XML, WML and SVG markup.
}
]
ML: function [Dialect [block!]] [String Values_Rule Values Value Tag] [
String: make string! 40000
Values_Rule: [
; Caution! The 'none word below is replaced in the 'parse rule
below!
none [
set Value any-type! (
Tag: next next Tag
insert Tag Value
Value: none
)
]
; Caution! The 'opt word below is replaced in the 'parse rule below!
opt [
set Value [
decimal! | file! | block! | string! | char!
| money! | time! | issue! | tuple! | date!
| email! | pair! | logic! | integer! | url!
]
]
]
Values: make block! 10
parse Dialect [
any [
[
set Tag tag! (
Values_Rule/1: 0 ; Replace 'none word in 'Values_Rule
above.
if #"/" = last Tag [Values_Rule/3: 0] ; Replace 'opt
word...
)
| set Tag [path! | word!] (
Tag: to-block get 'Tag
Values_Rule/1: -1 + length? Tag ; Replace 'none word
in 'Values_Rule above.
Values_Rule/3: 'opt ; Replace 'opt word...
)
] (Value: none) Values_Rule (
Tag: head Tag
either none? Value [
if not tag? Tag [
Tag: build-tag Tag
]
if #"/" <> last Tag [
append Tag " /"
]
append String Tag
append String newline
] [
repend String [
either block? Value [newline] [""]
either tag? Tag [Tag] [Build-Tag Tag]
either block? Value [ML Value] [Value]
to-tag join #"/" first either tag? Tag [to-block
Tag] [Tag]
]
]
Values_Rule/1: none
)
| set Value any-type! (append String Value)
]
end
]
String
]
]
[
Rebol [
Name: 'Build-Tag
Title: "Build-Tag"
File: %Build-Tag.r
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 7/June/2002
Comments: {
Build-Tag is a replacement Build-Tag that handles XML attributes.
}
]
Build-Tag: function [
"Generates a tag from a composed block."
Values [block!] "Block of parens to evaluate and other data."
] [
Tag Value_Rule XML? Name Attribute Value
] [
Tag: make string! 7 * length? Values
Value_Rule: [
set Value issue! (Value: mold Value)
| set Value file! (Value: replace/all copy Value #" " " ")
| set Value any-type!
]
XML?: false
parse compose Values [
[
set Name ['?xml (XML?: true) | word!] (
append Tag Name
)
any [
set Attribute [word! | url!] Value_Rule (
Tag: rejoin [
Tag #" " Attribute {="} Value {"}
]
)
| Value_Rule (
Tag: reform [Tag Value]
)
]
end (
if XML? [
append Tag #"?"
]
)
]
| [
set Name refinement! any any-type! end (
Tag: mold Name
)
]
]
to tag! Tag
]
]
-- Attached file included as plaintext by Listar --
-- File: ML.r
[
Rebol [
Name: 'ML
Title: "ML"
File: %ML.r
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 8/June/2002
Comments: {
ML generates XML-like markup language from words, paths and blocks.
ML can generate HTML, XHTML, XML, WML and SVG markup.
}
]
ML: function [Dialect [block!]] [String Values_Rule Values Value Tag] [
String: make string! 40000
Values_Rule: [
; Caution! The 'none word below is replaced in the 'parse rule below!
none [
set Value any-type! (
Tag: next next Tag
insert Tag Value
Value: none
)
]
; Caution! The 'opt word below is replaced in the 'parse rule below!
opt [
set Value [
decimal! | file! | block! | string! | char!
| money! | time! | issue! | tuple! | date!
| email! | pair! | logic! | integer! | url!
]
]
]
Values: make block! 10
parse Dialect [
any [
[
set Tag tag! (
Values_Rule/1: 0 ; Replace 'none word in 'Values_Rule above.
if #"/" = last Tag [Values_Rule/3: 0] ; Replace 'opt word...
)
| set Tag [path! | word!] (
Tag: to-block get 'Tag
Values_Rule/1: -1 + length? Tag ; Replace 'none word in 'Values_Rule above.
Values_Rule/3: 'opt ; Replace 'opt word...
)
] (Value: none) Values_Rule (
Tag: head Tag
either none? Value [
if not tag? Tag [
Tag: build-tag Tag
]
if #"/" <> last Tag [
append Tag " /"
]
append String Tag
append String newline
] [
repend String [
either block? Value [newline] [""]
either tag? Tag [Tag] [Build-Tag Tag]
either block? Value [ML Value] [Value]
to-tag join #"/" first either tag? Tag [to-block Tag] [Tag]
]
]
Values_Rule/1: none
)
| set Value any-type! (append String Value)
]
end
]
String
]
]
-- Attached file included as plaintext by Listar --
-- File: Build-Tag.r
[
Rebol [
Name: 'Build-Tag
Title: "Build-Tag"
File: %Build-Tag.r
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 7/June/2002
Comments: {
Build-Tag is a replacement Build-Tag that handles XML attributes.
}
]
Build-Tag: function [
"Generates a tag from a composed block."
Values [block!] "Block of parens to evaluate and other data."
] [
Tag Value_Rule XML? Name Attribute Value
] [
Tag: make string! 7 * length? Values
Value_Rule: [
set Value issue! (Value: mold Value)
| set Value file! (Value: replace/all copy Value #" " " ")
| set Value any-type!
]
XML?: false
parse compose Values [
[
set Name ['?xml (XML?: true) | word!] (
append Tag Name
)
any [
set Attribute [word! | url!] Value_Rule (
Tag: rejoin [
Tag #" " Attribute {="} Value {"}
]
)
| Value_Rule (
Tag: reform [Tag Value]
)
]
end (
if XML? [
append Tag #"?"
]
)
]
| [
set Name refinement! any any-type! end (
Tag: mold Name
)
]
]
to tag! Tag
]
]