Link | xml_tree |
Author | Jason Huck |
Category | XML |
Version | 8.x |
License | Public Domain |
Posted | 08 May 2006 |
Updated | 11 Oct 2010 |
More by this author... |
This type provides additional functionality for the built-in XML type, making it easier to retrieve values from XML documents when the structure is known. It adds the following new member tags:
->atts - Returns a map of the attributes for the current node, instead of an array of pairs.
->attribute(string) - Returns the value of the given attribute for the current node.
->nodename(index) - Returns the given child node by name. If there are multiple nodes of the same name, you can return a specific node by passing an index. If no matching child nodes are found, it will look for an attribute by that name. Returns an array if there is more than one matching node at the current level, or if an optional -array parameter is passed.
->getnode(string) - Same as ->nodename above. Useful if the node name conflicts with an existing member tag, such as "name."
->getnodes - Returns the children of the current node, minus the empty ones that ->children generates on its own.
var('testxml') = '\'; var('test') = xml_tree($testxml); $test->record->thing(2)->contents; -> moo blah moo
Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
define_type( 'tree', 'xml', -namespace='xml_', -priority='replace', -description='Extends and simplifies the built-in XML type.' ); define_tag('atts'); local('out' = map); iterate(self->attributes, local('i')); #out->insert(@#i->first = @#i->second); /iterate; return(@#out); /define_tag; define_tag('attribute', -req='name'); if(self->attributes->size && self->attributes->find(#name)->size); return(@self->attributes->find(#name)->first->second); else; return(''); /if; /define_tag; define_tag('getNode', -req='nodename', -opt='count', -opt='array'); local('matches') = @self->extract('*[translate(local-name(), \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\') = translate(\'' + #nodename + '\', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')]'); local('returnarray'=local_defined('array') && #array != false); if(!#matches->size); return(@self->attribute(#nodename)); else(#matches->size == 1 && !#returnarray); return(@#matches->first); else; if(local_defined('count')); protect; return(@#matches->get(integer(#count))); handle_error; return; /handle_error; /protect; else; return(@#matches); /if; /if; /define_tag; define_tag('getnodes'); local('out') = self->children; // remove any element that contains only whitespace #out->removeall(match_notregexp('\\S')); return(@#out); /define_tag; define_tag('_unknowntag', -opt='count', -opt='array'); local('returnarray'=local_defined('array') && #array != false); if(local_defined('count')); return(@self->getnode(tag_name, -count=#count, -array=#returnarray)); else; return(@self->getnode(tag_name, -array=#returnarray)); /if; /define_tag; /define_type;
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft
Added -array parameter to normalize results of ->getnode.