Announcement

Collapse
No announcement yet.

XML Data Direct Accessor

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML Data Direct Accessor

    Hi everyone,

    JavaScript allows for direct filtering and accessing of data, without needing to use an iteration statement (for, for each), by using a dotted notation, such as:

    var m = x.car.(color != "silver").model

    Using the XML below, the above statement will return all of the "model" nodes of every parent "car" where the color is not silver.

    This dotted notation can't work with HL7 XML because there are dots all over the place in the node name. Either the dots needs to be escaped, which I haven't been able to do, or you have to try the bracketed notation, which doesn't seem to work either.

    Is there a way to do what I've done below with HL7 bracketed notation or a way to use the dotted notation by escaping the dots in node names?


    Sample XML which allows for dotted notation
    var x = new XML(
    <cars>
    <car>
    <make>Ford</make>
    <model>Focus</model>
    <color>blue</color>
    </car>
    <car>
    <make>Honda</make>
    <model>Civic</model>
    <color>silver</color>
    </car>
    <car>
    <make>Volkswagon</make>
    <model>Jetta</model>
    <color>silver</color>
    </car>
    </cars>
    );


    Different tests:
    var m = x.car.(color == "silver").model;
    returns: <model>Civic</model><model>Jetta</model>

    var m = x.car.(color != "silver").model;
    returns: Focus

    var m = x['car'](['car']['color'] == "silver")['model'];
    throws error

    var m = x['car'].(['car']['color'] == "silver")['model'];
    returns nothing

    var m = x['car'].(['color'] == "silver")['model'];
    returns nothing

    Thanks!
    Steve Metz

  • #2
    To clarify, that ".(foo == 'bar')" syntax is part of E4X, not JavaScript itself.

    You can use a function in there:

    Code:
    var m = x.car.(function(){return new XML(this)['color'] == 'silver'}()).model;
    Last edited by narupley; 03-04-2016, 08:15 AM.
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      Thanks. Works perfectly.

      Comment

      Working...
      X