Reputation: 5
I am trying to filter an XML based on multiple parameters that I have as an input.
I am trying to identify the parent nodes which have the matching records so that I can filter them out and process.
<A>
<B1>
<C1>
<D1>111</D1>
<E1>111</E1>
<F1>
<G1>111</G1>
<H1>
<I1>111</I1>
<J1>111</J1>
</H1>
</F1>
</C1>
</B1>
<B1>
<C1>
<D1>222</D1>
<E1>333</E1>
<F1>
<G1>222</G1>
<H1>
<I1>222</I1>
<J1>222</J1>
</H1>
</F1>
</C1>
</B1>
<B1>
<C1>
<D1>333</D1>
<E1>333</E1>
<F1>
<G1>333</G1>
<H1>
<I1>333</I1>
<J1>333</J1>
</H1>
</F1>
</C1>
</B1>
</A>
Lets say I need to match for the node D1 and E1 and I1, but if there is a 'AND' match from all the parameters, I need to have the node right from <B1>
for the result.
<B1>
<C1>
<D1>222</D1>
<E1>333</E1>
<F1>
<G1>222</G1>
<H1>
<I1>222</I1>
<J1>222</J1>
</H1>
</F1>
</C1>
</B1>
I am trying usng the below combination to get the data:
xml..*.((hasOwnProperty("D1") && D1 == "222")&&hasOwnProperty("E1") && D1 == "333"))
But think there is some gap. Can someone fill in and tell me where am I going wrong or is there a better approach to filter an XML? Also, is there something which the filterFunction (collections) can help out with?
Upvotes: 0
Views: 497
Reputation: 22604
You can use the ..
operator, or call its equvalentdescendants()
to get an XMLList of all sub nodes that match your criteria, regardless of where they are in the hierarchy. Then, using xml.( criteria )
, select only the nodes that also match the expression within the parentheses:
var result:XMLList = xml..B1.(
( descendants ("D1") == "222" ) &&
( descendants ("E1") == "333" ) &&
( descendants ("I1").length() > 0 ) // or any other expression
);
Upvotes: 1