Topic - Javascript E4X tutorial and examples
Topic - Javascript E4X tutorial and examples Topic - Javascript E4X tutorial and examples
from forum XML
 forum index   my profile   search 
 new topic  post reply 
moderators: pjr tab
Javascript E4X tutorial and examples
Joined: 7-February-2005
Posts: 249
Location: Uncharted territory
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.htm

One page this page of E4X tests which was very helpful:
http://bclary.com/2004/10/03/js-tests/menubody.html

Here is quick cookbook of some code snippets I've discovered:
Cookbook

create a literal document:
doc = <a><b c="1"/><b c="2"/></a>;


create cursor into document
b0 = doc.b[0];


add/assign an attribute
doc.@c="new value";


delete a node:
delete doc.@c;


iterate over nodes:
for each (b in doc.b) { x = b.@c; }


find all decendants:
allc = doc..@c;


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:
count = doc.b.length();


NetKernel specific

Source 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 noticed

Building an XML object from an XmlObject when there is a leading comment, for example with this doc:

<!-- comment -->
<root />


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!
I have setup problems.
Joined: 28-July-2005
Posts: 1
Posted: 28-July-2005 16:49
The information you gave was very valuable. But i think i need more help. I have set up problems. Can you go over what is need to start with rhino. I cant write js including XML data type. The error is "XML" is not defined. What could be the problem...
a few checks...
Joined: 7-February-2005
Posts: 249
Location: Uncharted territory
Posted: 29-July-2005 13:47
First things first, are you using NetKernel version 2.0.6 as E4X support is new feature? Secondly try running the E4X example in the Developer Tools -> Script Playpen - does that work?

If you get this far then it is likely a configuration issue within your development module.

What is you setup? (OS/JVM version/ How are you using E4X - in a custom module/ in workbench)

Cheers,
Tony
How do I iterate over nodes that have period in their names?
Joined: 1-November-2006
Posts: 2
Posted: 1-November-2006 20:01
I have an xml document as follows
<doc>
<node>
<node.1> hello </node.1>
<node.1> world </node.1>
<node.1> welcome to </node.1>
<node.1> E4X </node.1>
</node>
</doc>

How do I iterate over node.1?

I would like to something like
for each ( text in doc..node.1 )
{
print (text);
}


Right now, I can't do this because of period in the node name. Rhino interpreter is throwing an error. 'missing ) after for-loop control'

Can someone help me? Thanks.
Property Accessors
Joined: 7-February-2005
Posts: 397
Location: UK
Posted: 1-November-2006 22:21
The answer seems to be in section '11.2.1 Property Accessors' of the E4X spec.  This works...


importPackage(Packages.java.lang);

doc = <doc>
<node>
<node.1> hello </node.1>
<node.1> world </node.1>
<node.1> welcome to </node.1>
<node.1> E4X </node.1>
<node.2> NOT SEEN </node.2>
</node>
</doc>;

qn=QName("node.1");

for each ( text in doc..*["node.1"] )
{
System.out.println(text);
}


Maybe there's an elegant escaping syntax but I haven't found it yet!

Cheers

Pete
Ignore QName() above
Joined: 7-February-2005
Posts: 397
Location: UK
Posted: 1-November-2006 22:22
I was playing around and that got left in the example!
Property Accessors works
Joined: 1-November-2006
Posts: 2
Posted: 2-November-2006 15:10
pjr, thank you very much for the solution. More importantly thanks for the E4X resource. I was googling with wrong keywords. I was able to get the E4X spec document, now. Thanks again.
 new topic  post reply  To find out about new replies to this post as they occur
please subscribe to one of these feeds:
AtomRSS moderate 
© 2003-2006, 1060 Research Limited. 1060 registered trademark, NetKernel trademark of 1060 Research Limited.