E4X - Primitive XML

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    E4X - Primitive XML - Presentation Transcript

    1. E4X: Primitive XML Joel Bernstein, London Perl Mongers joel@fysh.org
    2. E4X? • ECMAScript for XML • ECMAScript is the ISO Standard language known as JavaScript, JScript, ActionScript, ... • Provides native XML support for JavaScript • inline XML literals in JavaScript code! • Defined in ECMA-357, in use since FF 1.5
    3. What does it do? • Adds native XML datatypes to JavaScript • Extends semantics of operators to manipulate XML data • Adds new operators for filtering, searching XML • XML literals, namespaces, qualified names, etc
    4. XML DOM API function
writeList
()
{ 

var
labels
=
xmlDoc.getElementsByTagName('label'); 

var
ol
=
document.createElement('OL'); 

for
(i=0;
i
<
labels.length;
i++)
{ 



var
li
=
document.createElement('LI'); 



for
(j=0;
j
<
labels[i].childNodes.length;
j++)
{ 





if
(labels[i].childNodes[j].nodeType
!=
ELEMENT_NODE)
{
continue;
} 





var
cdata
=
document.createTextNode( 







labels[i].childNodes[j].firstChild.nodeValue); 





li.appendChild(cdata); 



} 



var
labelId
=
document.createTextNode('('
+ 





labels[i].getAttribute('id')
+
')'); 



li.appendChild(labelId); 



ol.appendChild(li); 

} 

document.getElementById('updateTarget').appendChild(ol); }
    5. E4X Equivalent function
writeList
(domDoc)
{ 



var
xdoc
=
 new
XML((new
XMLSerializer()) .serializeToString(domDoc.documentElement)); 



var
xlist
=
<ol/>; 



for
each
(label
in
xdoc..label)
 







xlist.*
+=
<li>{
//
this
is
an
XMLList
object label.name.text()
+
'
('
+
label.@id
+
')' }</li>; 



} 



document.getElementById('updateTarget').innerHTML
 =
xlist;
//
UGH! }
    6. No, what does it DO? var
mongers
=
<perlmongers> <monger
gender=”male”
id=”2”> <contact
email=”joel@fysh.org”> <name>Joel
Bernstein</name> <age>27</age> <hair>Brown</hair> </contact> </monger> <monger
gender=”male”
id=”34”> <contact
email=”dave@dave.org.uk”> <name>Dave</name> <age>Unspecified</age> <hair>Corporate
Silver</hair> </contact> </monger> </perlmongers>;
    7. E4X syntax examples • mongers..monger. (@id==\"2\").contact.name.text() • Find all “item” elements at any depth, select one whose “id” attribute is ‘2’ and navigate to their name.
    8. E4X syntax examples 2 • mongers..monger. (contact.@email==\"joel@fysh.org\")..na me.text()
 • Similar to previous search, this time searching by child element attribute.
    9. E4X syntax examples 3 • function
foo
()
{
return
<><item
/></ item
/></>;
} • could
also
be:
return
new
 XMLList(<item
/>,
<item
/>); • function
bar
()
{
var
x
=
 <items>{foo()}</items>;
return
x;
} • Yields: <items><item /><item /></items>
    10. The good • Terse, powerful syntax. • Extends JS operators. • Easy and natural to write, much less verbose than DOM
    11. The bad • XML() and <xml>...</xml> objects are primitives rather than true objects • Impedance mismatch between JS objects and XML objects -- a JS object can’t seamlessly render itself as XML • Can’t directly insert E4X nodes into DOM • we can solve this last one
    12. E4X to DOM node function
importNode
(
e4x,
doc
)
{ 

var
me



=
importNode,
xhtml,
domTree,
importMe; 

me.Const

=
me.Const
||
{
mimeType:
'text/xml'
}; 

me.Static
=
me.Static
||
{}; 

me.Static.parser
=
me.Static.parser
||
new
DOMParser; 

xhtml
=
<testing
xmlns=\"http://www.w3.org/1999/xhtml\"
/>; 

xhtml.test
=
e4x; 

domTree
=
me.Static.parser.parseFromString(
xhtml.toXMLString(),
me.Const.mimeType
); 

importMe
=
domTree.documentElement.firstChild; 

while
(
importMe
&&
importMe.nodeType
!=
1
)
{ 



importMe
=
importMe.nextSibling; 

} 

if
(
!doc
)
doc
=
document; 

return
importMe
?
doc.importNode(
importMe,
true
)
:
null; } function
appendTo
(
e4x,
node,
doc
)
{
//
inject
E4X
at
end
of
node 

return
node.appendChild(
importNode(
e4x,
doc
||
node.ownerDocument
)
); } function
setContent
(
e4x,
node
)
{
//
replace
node
contents
with
E4X 

while(
node.firstChild
)
{ 



node.removeChild(
node.firstChild
); 

} 

appendTo(
e4x,
node
); }
    13. Thank you. Any questions?
    SlideShare Zeitgeist 2009

    + joelbernsteinjoelbernstein Nominate

    custom

    443 views, 0 favs, 0 embeds more stats

    Lightning talk (5 minutes) on E4X (XML literals / o more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 443
      • 443 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 2
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories