Product: Mapping Manager
Article Link: http://help.truecommerce.com/en/articles/9929841-xpath-predicate-selection-by-child-element
You can filter your selections based on specific values or conditions related to children elements utilizing Predicates. These predicates allow for a quick easy way to limit your results.
Example XML
The below example is a chunk of XML that contains all items within the document. In it, we have two items with
<Items>
<Item>
<LineNum>1</LineNum>
<ItemNo>R1230</ItemNo>
<Quantity>24</Quantity>
<Price>84.46</Price>
</Item>
<Item>
<LineNum>2</LineNum>
<ItemNo>R890</ItemNo>
<Quantity>12</Quantity>
<Price>12.92</Price>
</Item>
<Item>
<LineNum>3</LineNum>
<ItemNo>Shipping</ItemNo>
<Quantity>1</Quantity>
<Price>128.00</Price>
</Item>
</Items>
Child Element Selection
By default predicates look at the child element nodes - so all we have to do is utilize the predicate square brackets [ ] alongside our parent's node name to select based on the child nodes info.
Parent[Child]
With this structure, we can then include a conditional statement to select a specific XML element based on the value of the child surrounded in quotes.
Parent[Child="value"]
Selecting the Shipping Item
In the above example, there is a single item that has a <ItemNo> equal to that of Shipping. To select that item specifically, we can simply define the xPath by checking for the ItemNo with that particular value.
Item[ItemNo="Shipping"]
xPath Results
<Item>
<LineNum>3</LineNum>
<ItemNo>Shipping</ItemNo>
<Quantity>1</Quantity>
<Price>128.00</Price>
</Item>
Only the Item with it's <ItemNo> marked as Shipping is returned
Selecting Items with Quantity Threshold
If you would like to select only items with a specific quantity greater/less than a threshold, you can do so utilizing your basic greater than (>) or less than operators (<)
Greater Than
Item[Quantity > 12]
<Item>
<LineNum>1</LineNum>
<ItemNo>R1230</ItemNo>
<Quantity>24</Quantity>
<Price>84.46</Price>
</Item>
Returns only the Item group that has a Quantity greater than 12, since Line 2 is equal to 12 - it would not be returned.
Less Than
Item[Quantity < 24]
<Item>
<LineNum>2</LineNum>
<ItemNo>R890</ItemNo>
<Quantity>12</Quantity>
<Price>12.92</Price>
</Item>
<Item>
<LineNum>3</LineNum>
<ItemNo>Shipping</ItemNo>
<Quantity>1</Quantity>
<Price>128.00</Price>
</Item>
Only the 2nd and 3rd item are returned, as the 1st is equal to 24
Combining Predicates
You can combine predicate conditions to filter based on multiple conditions for greater filtering capabilities.
Less Than (24) and NOT a Shipping Item
Item[Quantity < 24][ItemNo != 'Shipping']
<Item>
<LineNum>2</LineNum>
<ItemNo>R890</ItemNo>
<Quantity>12</Quantity>
<Price>12.92</Price>
</Item>
Only the 2nd item is returned since it is the only item that meets both conditions
rev 10/16/2024