61 lines
1.4 KiB
Plaintext
61 lines
1.4 KiB
Plaintext
#lang honu
|
|
|
|
require prefix xml_ xml;
|
|
require "linq.rkt";
|
|
|
|
class Xml(data){
|
|
Descendants(name){
|
|
[new Xml(element): element = find_descendants(data, name)]
|
|
}
|
|
|
|
Element(name){
|
|
new Xml(get_element(data, name))
|
|
}
|
|
|
|
Value(){
|
|
get_text(data)
|
|
}
|
|
|
|
getData(){ data }
|
|
}
|
|
|
|
function read_xml(){
|
|
xml_permissive_xexprs(true)
|
|
xml_xml_to_xexpr(xml_document_element(xml_read_xml()))
|
|
}
|
|
|
|
function loadXml(file){
|
|
withInputFromFile(file){
|
|
new Xml(read_xml())
|
|
}
|
|
}
|
|
|
|
function starts_with(start, what){
|
|
substring(what, 0, string_length(start)) string_equal start
|
|
}
|
|
|
|
var xml = loadXml("test.xml")
|
|
printf("xml ~a\n", xml)
|
|
printf("data: ~a\n", xml->getData())
|
|
printf("table1: ~a\n", xml->Descendants("Table1"))
|
|
|
|
struct test{name, address}
|
|
|
|
var addresses = linq from add in xml->Descendants("Table1")
|
|
where true
|
|
orderby add->Element("familyName")->Value()
|
|
select test(add->Element("familyName")->Value(),
|
|
add->Element("address")->Value())
|
|
|
|
printf("addresses ~a\n", addresses)
|
|
|
|
for add in addresses do {
|
|
printf("name ~a address ~a\n", add.name, add.address)
|
|
}
|
|
|
|
for xs in linq from foo in xml->Descendants("Table1")
|
|
where starts_with("x", foo->Element("familyName")->Value())
|
|
select foo->Element("familyName")->Value() do {
|
|
printf("only x: ~a\n", xs)
|
|
}
|