2

Consider this scenario:

Using Javascript/E4X, in a non-browser usage scenario (a Javascript HL7 integration engine), there is a variable holding an XML snippet that could have multiple repeating nodes.

<pets>       
 <pet type="dog">Barney</pet>
 <pet type="cat">Socks</pet>
</pets>

Code:

var petsXml; // pretend it holds the above xml value
//var cat = petsXml['pet']..... ?

Question: using E4X, how can you select the correct pet node with the type attribute holding the value of string 'cat'?

Update:

Some learnings with E4X:

  • to select a single/first node by an attribute value: var dog = petsXml.(@type == "dog");
  • to get a value from one node's specific attribute: var petType = somePetNode.@type;
p.campbell
  • 98,673
  • 67
  • 256
  • 322

1 Answers1

2
var petsXml;
var catList = petsXml.*.(@type == "cat");

See "Filters" here or "parameterized locate" over here.

Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41
  • thanks Pianoman. That worked fine. Although I've successfully implemented the solution as petsXml.(@type == "cat"); Is the asterisk indicating that multiples would be returned, whereas without would be the first match found? – p.campbell Jul 22 '09 at 17:30
  • You are absolutely right. The `*` should scan all the `` nodes, and `catList` could very well be an array if there are >1 cats. – Daniel F. Thornton Jul 22 '09 at 18:09