using the parent() function in e4x epressions and actionscript to create complex queries.

Let’s say you have the following xml for greeting cards.

<designlist>

<design

id="123"
title="Happy Hanukkah"
previewImageUrl="assets/demo/images/g0022_4x6h_preview.jpg"
thumbImageUrl="">
<tags>

<tag id='1231'>Holiday</tag>
<tag id='3242'>Hanukkah</tag>

</tags>

</design>

 

<design

id="456"
title="Happy New Year"
previewImageUrl="assets/demo/images/g0412_4x6h_preview.jpg"
thumbImageUrl="">
<tags>

<tag id='1231'>Holiday</tag>
<tag id='2342'>New Year</tag>

</tags>

</design>

 

<design

id="8284"
title="Merry Xmas"
previewImageUrl="assets/demo/images/g0256_4x6v_preview.jpg"
thumbImageUrl="">

<tags>

<tag id='1231'>Holiday</tag>
<tag id='3242'>XMas</tag>
<tag id='8323'>Christmas</tag>

</tags>

</design>

 

<design

id="8284"
title="Happy Happy Happy"
previewImageUrl="assets/demo/images/g0055_4x6v_preview.jpg"
thumbImageUrl="">

<tags>

<tag id='1231'>Holiday</tag>
<tag id='3242'>Happy</tag>
<tag id='3242'>Wonderful</tag>

</tags>

</design>

</designlist>

</design>

And let’s say we wanted to return the titles for designs for that are tagged with “New Year”. This is tricky because the tags are further down the XML tree than the title, which is an attribute of the design itself.  How would we express this in e4x and actionscript?

var titles:XMLList = designViewerPanel.designListXml.design.@title.(parent().design.tags.(tag=="New Year"));

The key here is to use the parent() function, which returns us back to the top level of the xml tree. We can then express the particular tag we are looking for.

I still haven’t figured out how to do the same expression with out specifying the designs.tags…in other words, I’d like to say something like


(parent()..(tag=="New Year"))

but when I try this I get an error…actionscript doesn’t seem to like the parenthesis next to ‘tag’. Ideas anyone?

Comments (2) left to “using the parent() function in e4x epressions and actionscript to create complex queries.”

  1. Бизнесмен wrote:

    “мысли здравые, но тяжело читать, не знаю почему”

  2. Greg K. wrote:

    I know this is an old post, but for anyone trying to read it and figure out e4x, the reason why you need a tag before () is because you need something to run the search condition on. in English, blah.parent()..(tag==”New Year”) reads: get me all descendants of blah’s parent that have a tag child with simple content “New Year”. This does not resolve to a descrete set of descendents, since a>b>c(New Year) would return a, b, and c. instead you should write: give me all TAGS under blah’s parent, which have simple content of New Year: designViewerPanel..design.@title.(parent()..tag.(text()==”New Year”).length()>0);

Post a Comment

*Required
*Required (Never published)