|
| 1 | +# Filter XMLLists |
| 2 | + |
| 3 | +The xFilter() method creates a new XMLList with all elements that pass the test implemented by the provided function. This function has some advantages over native e4x filtering. |
| 4 | +- It passes the actual node to be filtered to the filter function rather than operating on it in a *with* context. |
| 5 | +- This allows calling methods on the node as well as referencing child elements that contain javascript operators (most notibly the . character, which is heavily used in hl7.) |
| 6 | +- Additional parameters let you limit the number of results returned or throw exceptions if there is an unexpected number of filtered results. |
| 7 | + |
| 8 | +## Syntax |
| 9 | + |
| 10 | +```javascript |
| 11 | +var filteredList = xFilter(xmlList, callback(node[, index[, list]])[, minLimit[, maxReturned[, enforceMax]]]) |
| 12 | +``` |
| 13 | + |
| 14 | +### Parameters: |
| 15 | + |
| 16 | +- **xmlList** The XMLList to be filtered |
| 17 | +- **callback** Function is a predicate, to test each node of the XMLList. Return `true` to keep the element, `false` otherwise. It accepts 3 arguments |
| 18 | + - **node** The current node being processed in the XMLList |
| 19 | + - **index** The index of the current node being processed in the XMLList |
| 20 | + - **list** The XMLList passed as the first parameter of xFilter() |
| 21 | +- **minLimit** Optional. Throw a RangeError if the length of the xmlList to be returned is fewer than this number. Defaults to no lower limit. |
| 22 | +- **maxReturned** Optional. Only return up to this many results. This can be used to short circuit the function if you do not need all of the results. Defaults to no upper limit. |
| 23 | +- **enforceMax** Optional. Throw a RangeError if more than maxReturned results will be returned. Defaults to false. |
| 24 | + |
| 25 | +### Return Value |
| 26 | + |
| 27 | +A new XMLList with the nodes that pass the test (up to maxReturned.) If no nodes pass the test and minLimit is not set, an empty XMLList will be returned. |
| 28 | + |
| 29 | + |
| 30 | +### Examples |
| 31 | +Get the id that represents the NPI number from the repetition of OBR-16 where OBR-16.9 equals `NPI` or empty string if it doesn't exist. Stop looking after the first match if more than one repetition qualifies. |
| 32 | + |
| 33 | +```javascript |
| 34 | +var npiOrdering = xFilter(msg.OBR['OBR.16'], function(obr16) {return obr16['OBR.16.9']=='NPI'}, 0, 1); |
| 35 | +var npiNumber = npiOrdering['OBR.16.1'].toString(); |
| 36 | +``` |
| 37 | + |
| 38 | +Compile the OBX-5 text of all OBX segments ignoring the first 5 OBX segments and any OBX segment where OBX-3.2 is not `Report`. Ensure at least one line is printed. Arrow functions can be used when ES6 mode is enabled. |
| 39 | + |
| 40 | +```javascript |
| 41 | +var clippedReport = new java.lang.StringBuilder(); |
| 42 | +try { |
| 43 | + for each (var obx in xFilter(msg.OBX, (obx,i) => (i>4 && obx['OBX.3']['OBX.3.2']=='Report'), 1)) { |
| 44 | + clippedReport.append(obx['OBX.5']['OBX.5.1']).append('\n'); |
| 45 | + } |
| 46 | +} |
| 47 | +// let other exceptions go unchecked |
| 48 | +catch(e if e instanceof RangeError) { |
| 49 | + // handle report that does not meet requirements |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | + |
0 commit comments