Posted:
13-May-2005 16:14 I've been playing with E4X within the NetKernel scripting framework because I wanted to see if the tight language binding between Javascript and XML really made writing scripts to process XML easier and more compact. The conclusion: E4X really works well despite a few rough edges.Getting into E4X, though, was hard because there are not many good sources of example code out there to use. The specification is not that helpful to actually work out how to use it: http://www.ecma-international.org/publications/standards/Ecma-357.htmOne page this page of E4X tests which was very helpful: http://bclary.com/2004/10/03/js-tests/menubody.htmlHere is quick cookbook of some code snippets I've discovered: Cookbookcreate a literal document: doc = <a><b c="1"/><b c="2"/></a>; |
create cursor into document add/assign an attribute delete a node: iterate over nodes: for each (b in doc.b) { x = b.@c; } |
find all decendants: insert a new fragment: frag = <b c="3"/>; doc.insertChildAfter(doc.b[1], frag); |
insert a new fragment at beginning: frag = <b c="0"/>; doc.insertChildAfter(null, frag); |
parameterized locate: cValue=2; bNode = doc.b.(@c==cValue); |
parameterized text in literal: text="text here"; frag = <b c="4">{text}</b>; |
parameterized attribute in literal: text="4"; frag = <b/>; frag.@c=text; |
count matches: NetKernel specificSource an external resource as an XML object: org=new XML( context.sourceAspect( "http://www.1060.org/", IAspectXmlObject).getXmlObject() ); |
Create a response from an XML object: aspect=new XmlObjectAspect(doc.getXmlObject()); response=context.createResponseFrom(aspect); response.setMimeType("text/xml"); context.setResponse(response); |
Process an XML object with an XSLT transform: req = context.createSubRequest(); req.setURI("active:xslt"); req.addArgument("operator","style.xsl"); req.addArgument("operand", new XmlObjectAspect(doc.getXmlObject())); req.setAspectClass(IAspectXmlObject); table=new XML(context.issueSubRequestForAspect(req).getXmlObject()); | Strange things I've noticedBuilding an XML object from an XmlObject when there is a leading comment, for example with this doc: You end up with a doc just containing a comment and no root element if you: doc=new XML( context.sourceAspect( "doc.xml",IAspectXmlObject). getXmlObject() );
|
Building an XML object from a String when there are an comments - all comments get lost! |