= Amrita and XML
== summary
amrita can be used with XML as well as HTML.
* amrita has REXML-based XML parser
amrita's HTML parser was developed very ad hoc way. I have found no
major problem with it yet , but I think it's good idea to have an
option for XML: a strictly defined standard.
* amrita can generate XHTML documents as well as HTML.
amrita produces xhtml output from xhtml template.
* amrita can treat XML documents as a template.
amrita has a little code depend upon HTML's DTD(tag structure), and
they can be detached easily. So any XML(not XHTML) document can be
used as a template with amrita.
* amrita can get model data from XML documents
REXML has very natural API for ruby. And it is easy to make model
datas for amrita from XML documents read by REXML.
* amx: yet anothor style-sheet for XML
see docs/Tour2
---
== XHTML Document
=== code and output
code:
:include: sample/tour/xhtml.rb
output:
xhtml sample
SAMPLE1
members of this HASH will be inserted here and title
=== description
Basicaly amrita produce xhtml output from xhtml template, html4.0 from
html4.0 template.So programers don't need tobother about comformation
for some paticular standards or browser or devices. Only designers
(template writers) do.
The only thing programers should do is set asxml flag to Template
object.If this was set a single tag like
will be printed
like
.
---
== XML template
=== code and output
code:
data = {
:table1=>[
{ :name=>"Ruby In A Nutshell", :author=>"Yukihiro Matsumoto, David L. Reynolds", :isbn=>"0596002149" }
{ :name=>"Programming Ruby", :author=>"David Thomas, Andrew Hunt", :isbn=>"0201710897" },
{ :name=>"The Ruby Way", :author=>"Hal Fulton", :isbn=>"0672320835" },
]
}
xml_tmpl = TemplateText.new <
END
xml_tmpl.xml = true # use REXML-based parser
puts "------------XML output ----------------------"
xml_tmpl.expand(STDOUT, data)
output:
Ruby In A Nutshell
Yukihiro Matsumoto, David L. Reynolds
0596002149
Programming Ruby
David Thomas, Andrew Hunt
..........
=== description
xml_tmpl.xml = true # use REXML-based parser
puts "------------XML output ----------------------"
xml_tmpl.expand(STDOUT, data)
amrita loads templates on demand. If +xml+ flag is set when +expand+
is called, amrita uses REXML based parser.
You can use single model data for two templates. So single code with
amrita produce both XML output and HTML output.For detail see
sample/tour/xml1.rb .
---
== Use XML document as a model data
XML document(data) + HTML template ==> HTML document
This may be some kind of style-sheet.
=== code and output
code:
:include: sample/tour/xml2.rb
output:
| title |
author |
ISBN |
| Ruby In A Nutshell |
Yukihiro Matsumoto David L. Reynolds
|
0596002149 |
| Programming Ruby |
David Thomas Andrew Hunt
|
0201710897 |
| The Ruby Way |
Hal Fulton
|
0672320835 |
=== description
table = doc.elements.to_a("booklist/book").collect do |book|
{
:xxx=>.....
}
end
This code generate an Array of Hash on element.
element is a REXML::Element data. So you can get any
node or attribute you need by REXML's API.
:title=>book.elements['title'],
book.elements['title'] is the first element
of element.
:authors=>book.elements.to_a('author').collect do |a|
{ :name=>a }
end,
In this sample, a book has one title but can have many authors. So
authors shuold be treated as an Array with +to_a+, generate Array
of Hash by Ruby's standard method +collect+.
:isbn=>e(:a, :href=>"http://www.amazon.com/exec/obidos/ASIN/#{book.attributes['isbn']}") {
book.attributes['isbn']
}
I want to insert a direct link to amazon.
e(...) { ... } generate a tag like
0596002149
and insert it into template.
This idea was extended for document processing in sample/tour/xml3.rb
and reached to amx. See docs/Tour2 for detaile.
---
== convert a Ruby object to a XML entry
When you map a ruby object to XML entry, some members are mapped to
attribute and others are to sub elements.
This sample shows how to do it by generate ls -l information
as an XML.
=== code and output
code:
:include: sample/tour/filelist.rb
output:
drwxr-xr-x
Tue Sep 03 11:07:10 JST 2002
Tue Sep 03 11:07:10 JST 2002
Thu Sep 05 07:30:39 JST 2002
652250
2596
-rw-r--r--
Mon Aug 26 09:12:11 JST 2002
Mon Aug 26 09:12:11 JST 2002
Thu Sep 05 09:26:48 JST 2002
310411
.....
=== description
def entry(name)
a(:name=>name, :type=>ftype) { self }
end
This method generate an AttrArray that put some value of self to XML
attribute and make sub-elements with itself.
def size_or_nil
size if ftype == "file"
end
If the file is not a normal file, this method returns nil and The
element will be deleted
---
== X
=== code and output
code:
output:
=== description