[xml_nodemap]

Description

Link: [xml_nodemap]
Author: Jason Huck
Category: XML
Version: 8.5.x
License: Public Domain
Posted: Jun. 01, 2007
Updated: Jan. 01, 0001
More by this author...
When working with basic XML documents that do not have complex nested structures, it can sometimes be handy to extract a map of all the node names and their text values. This tag is a shortcut for that. It returns a map of every node that contains at least one non-empty text value.

Parameters

none


Sample Usage

[//lasso
	var('data') = string('\
<SOAP-ENV:Envelope 
	xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" 
	xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" 
	xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	
	<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
		<Item>
			<Quantity>1</Quantity>
			<Color>red</Color>
			<Size>small</Size>
			<Price>9.99</Price>
		</Item>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>\
	');
	
	xml_nodemap($data);
]

Returns:

map: (Price)=(9.99), (Size)=(small), (Quantity)=(1), (Color)=(red)
						

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
define_tag(
	'nodemap',
	-namespace='xml_',
	-req='in', -copy,
	-priority='replace',
	-description='Returns a flattened map of node values from XML.'
);		
	#in->type != 'xml' ? #in = xml(string(#in));	
	local('out' = map);
	
	iterate(#in->extract('//*'), local('i'));
		if(#i->extract('text()')->size);
			local(
				'name' = @#i->extract('local-name()'),
				'value' = string(#i->extract('text()')->first)->trim&
			);
			
			#value->size ? #out->insert(#name = #value);
		/if;	
	/iterate;
	
	return(#out);
/define_tag;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular