Skip to content

Instantly share code, notes, and snippets.

@EricFromCanada
Created April 2, 2014 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricFromCanada/9941779 to your computer and use it in GitHub Desktop.
Save EricFromCanada/9941779 to your computer and use it in GitHub Desktop.
Extends and simplifies the built-in XML type
// THE XML_TREE TRAIT
define xml_tree_trait => trait {
provide attr() => {
local(out = map)
with attr in .attributes
do #out->insert(#attr->name = #attr->value)
return #out
}
provide attribute(name::string) => .getAttribute(#name) || ''
provide getNode(nodename::string, count::integer = -1) => {
local(matches = .extract('*[translate(local-name(), \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\') = translate(\'' + #nodename + '\', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')]'))
#matches->size == 0?
return .attribute(#nodename)
#matches->size == 1?
return #matches->first
#count != -1?
protect => {
return #matches->get(integer(#count))
}
return #matches
}
provide getnodes() => (with node in .childNodes
where #node->type != ::xml_text || regexp(`\S`) == #node->nodeValue
select #node)->asStaticArray
provide _unknowntag(...params) => {
#params && params->size?
return .getnode(tag_name->asString, #params->first)
return .getnode(tag_name->asString)
}
}
// ADD THE XML_TREE_TRAIT TO XML_ELEMENTS
::xml_element->getType->addTrait(xml_tree_trait)
// THE XML_TREE CREATOR FOR COMPATIBILITY
define xml_tree(src::string) => xml(#src)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment