Lasso Soft Inc. > Home

[xml_tree]

Linkxml_tree
AuthorJason Huck
CategoryXML
Version8.x
LicensePublic Domain
Posted08 May 2006
Updated11 Oct 2010
More by this author...

Description

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.

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.

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;

Related Tags

Comments

11 Oct 2010, Jason Huck

Added -array parameter to normalize results of ->getnode.

->getnode will now accept an -array parameter, which causes the result to always be an array, even if there is only one matching node. Thanks to Johan Sölve for the contribution.

30 Aug 2009, Jason Huck

Bug fix for ->getnodes().

Corrected an issue with ->getnodes(). Thanks to Steve Piercy for discovering the bug and providing the workaround.

17 Jun 2006, Jason Huck

Update

Modified ->getnode() member tag to use an XPath expression instead of ->children for better performance/memory management, per a suggestion by Kyle Jessup.

01 Jun 2006, Jason Huck

Update

The _unknowntag callback now searches for matching attributes if no matching child nodes are found.

30 May 2006, Jason Huck

Updates

Added ->getnodes member tag and made more things return via reference.

17 May 2006, Jason Huck

Added ->getnode() member tag.

You can now use ->getnode() to access nodes with otherwise conflicting names.

08 May 2006, Jason Huck

Bug Fix

Added error trapping on ->attribute member tag.

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