Lasso Soft Inc. > Home

[xml_tree(9)]

Linkxml_tree(9)
AuthorEric Knibbe
CategoryXML
Version9.x
LicensePublic Domain
Posted02 Jun 2012
Updated02 Jun 2012
More by this author...

Description

This trait 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 methods:


->attr - 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.

 

Sample Usage

var('testxml') = '\

    
        blah
        moo
    
';
 
var('test') = xml_tree($testxml);
 
$test->record->thing(2)->contents;
 
-> moo

Source Code

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.

// 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)

Related Tags

Comments

02 Jun 2012, Eric Knibbe

Actual credit

This was in fact written by Kyle; I just took the liberty of copying it here from the mailing list.

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft