Product: Mapping Manager
Article Link: http://help.truecommerce.com/en/articles/10002307-xpath-predicate-selection-by-attribute
We can use Predicates to filter our xPath selection based on an Attribute that appears within an Element. For example - we can grab all <Item> nodes that have an attribute to select line items that are not physical items.
In this example - we will have an Attribute named type with two possible values
Attribute Name | Attribute Value |
type | inventory-item |
type | shipping-charge |
Example XML
In the below example XML - we have only the Line Items group present, with 4 items included. Two of the items are Inventory Items while two are Shipping Charges.
<LineItems>
<Item type="inventory-item">
<LineNumber>1</LineNumber>
<ProductCode>ABC123</ProductCode>
<Description>Widget A</Description>
<Quantity>10</Quantity>
<UnitPrice>250.00</UnitPrice>
<TotalPrice>2500.00</TotalPrice>
</Item>
<Item type="inventory-item">
<LineNumber>2</LineNumber>
<ProductCode>XYZ789</ProductCode>
<Description>Gadget B</Description>
<Quantity>5</Quantity>
<UnitPrice>200.00</UnitPrice>
<TotalPrice>1000.00</TotalPrice>
</Item>
<Item type="shipping-charge">
<LineNumber>3</LineNumber>
<ProductCode>Freight Charge</ProductCode>
<Description>Surcharge for freight</Description>
<Quantity>0</Quantity>
<UnitPrice>128.00</UnitPrice>
<TotalPrice>128.00</TotalPrice>
</Item>
<Item type="shipping-charge">
<LineNumber>4</LineNumber>
<ProductCode>Express</ProductCode>
<Description>express shipping</Description>
<Quantity>0</Quantity>
<UnitPrice>300.00</UnitPrice>
<TotalPrice>300.00</TotalPrice>
</Item>
</LineItems>
Filtering by Attributes
In order to make a selection based on an attribute you will need to prefix the attribute's name with an @ symbol.
Since our attribute's name is that of value our selection would be
@type
With the attribute name defined, we simply need to use an equal (=) sign to define the value we're looking for
@type="inventory-item"
@type="shipping-charge"
Selecting Shipping-Charge Items
In the above example we have two types of items, inventory-item and shipping-charge items. We can use a Predicate to easily filter our selection down to only shipping-charge types.
Using the above XML example, if we wanted to select all Items that were of type "shipping-charge" we could do so utilizing our predicate brackets [ ] to select an Item with an attribute (type) with the shipping-charge value.
//Item[@type="shipping-charge"]
This xPath would return any item marked with that particular attribute
xPath Results
<Item type="shipping-charge">
<LineNumber>3</LineNumber>
<ProductCode>Freight Charge</ProductCode>
<Description>Surcharge for freight</Description>
<Quantity>0</Quantity>
<UnitPrice>128.00</UnitPrice>
<TotalPrice>128.00</TotalPrice>
</Item>
<Item type="shipping-charge">
<LineNumber>4</LineNumber>
<ProductCode>Express</ProductCode>
<Description>express shipping</Description>
<Quantity>0</Quantity>
<UnitPrice>300.00</UnitPrice>
<TotalPrice>300.00</TotalPrice>
</Item>
Only the two items with type="shipping-charge" is returned
Selecting Express Charge
Perhaps you need to select the Express charge specifically - but you want to ensure you don't accidently grab an Inventory Item that may have the same code as your shipping line. You can combine predicates to select only <ProductCode> with the value of Express while ensuring the Item's type is that of shipping-charge only for safety.
//Item[type="shipping-charge"][ProductCode="Express"]
xPath Results
<Item type="shipping-charge">
<LineNumber>4</LineNumber>
<ProductCode>Express</ProductCode>
<Description>express shipping</Description>
<Quantity>0</Quantity>
<UnitPrice>300.00</UnitPrice>
<TotalPrice>300.00</TotalPrice>
</Item>
Only the one item is returned as it contains both the required attribute, as well as ProductCode defined in the predicates.
rev 10/16/2024