XML Node path – XPath
|
RuleLab.Net uses XPath to reference XML nodes in an application data document. Node paths are entered in the XML References Editor. Unless you are already familiar with the XPath expression syntax, look at it simply as a full path to an XML node starting from the root.
For example, in the following application XML data sample:
<customer> <customerID>JNC136</customerID> <purchases> <item> <partNumber>17878</partNumber> <price>420.00</price> <date>9/10/2005</ date> </item> <item> <partNumber>5343</partNumber> <price>315.00</price> <date>1/10/2006</ date> </item> <item> <partNumber>6700</partNumber> <price>280.00</price> <date>2/1/2006</ date> </item> </purchases> </customer>
the full path to the “customerID” node is “/customer/customerID”.
Note that the “item” nodes create a loop containing “partNumber”, “price”, and “date” nodes. An XPath expression for referring to “price” nodes is “/purchases/item/price”. Selecting nodes via this expression would pull every “price” node within the “purchases” node.
|
|
The “purchases” node is considered the parent of the “item” loop. The path to the “purchases” node is “/customer/purchases”. When referring to loop nodes in RuleLab.Net via XPath expressions, you will need both the full path to the node itself and the path to the parent of the nearest loop above that node.
Entering XPath expressions manually is not required in RuleLab.Net. You can use the Reference Editor to create them off of your XML data document tree. However, if you would like to take advantage of XPath functionality, please read the specification below.
Using XPath in RuleLab.Net
XPath is a language for finding information in XML documents. To learn about XPath please see the XPath Tutorial. RuleLab.Net architecture requires that XPath is utilized so that the Rules Engine could evaluate Atoms and process loops. The following are the guidelines on using XPath expressions for referencing XML nodes in an application data document.
1. Every XPath expression must be entered as an absolute path starting with “/”. If “/” is not entered, the Reference Editor will insert it automatically.
2. Multiple child node groups with same node names, residing under the same parent node are considered a loop. In the sample above, “item” nodes make a loop. A node is considered part of a loop if a parent node path has been associated with it via the Reference Editor. Referencing it as a loop will enable filters, aggregate, and other loop functions in RuleLab.Net. By entering a parent node path you are specifying that this is a loop node.
In order to avoid needing to know XPath syntax in depth, RuleLab.Net requires users to enter the parent loop node path. It has the same meaning as “//” operator. For example, a combination of the node path of “/customer/purchases/item/price” and the loop parent path of “/customer /purchases” translates into “/customer/purchases//item/price”.
3. An XPath expression referencing loop nodes should point to a selection of all such nodes within a loop, not to a particular node inside a loop. In other words, if a parent path has been entered then the loop node path must point to a set of nodes that aggregate functions will operate on. For example, you’d want to refer to “date” loop nodes via “/customer/purchases/item/date” expression as opposed to “/customer/purchases/item[1]/date”.
4. Conditional selections are supported. For example, if would like to only select purchase item dates with prices over $300.00, you could enter a loop node path as “/customer/purchases/item[price > 300.00]/date”.
We suggest that you limit conditional selects to presenting XML data document structure to RuleLab.Net. We advise against using them in place of Rule conditions because it may confuse other users who would only be looking at the Conditions entered in the Rules. Whenever possible, please use RuleLab.Net filters and comparison operators to enter conditional statements where they belong – in the Rule Condition. Although it is perfectly legitimate to utilize XPath syntax in the Reference Editor, please use RuleLab.Net according to its architecture principles.
5. If a node that is not specified as a loop node is referenced by an expression that may return multiple nodes, then the first node will be used. The default RuleLab.Net XPath syntax does not require node indexes. It assumes that the index is set to 1. You can however overwrite it by including a node index in an XPath expression. For example, “/customer/purchases/item/date” returns the same result as “/customer/purchases/item[1]/date” while “/customer/purchases/item[3]/date” is pointing to the third item.
If you are not referring to a multi-entry node as part of a loop, then you may find it useful to reference it by index. For example, to refer to the second “price” node you could use an XPath expression of “/customer/purchases/item[2]/price”. You could potentially create 2 different References to the same XML node – one as a loop node and another one non-loop. Referencing it as a loop will enable aggregate and other loop functions in RuleLab.Net while the non-loop Reference allows direct access to specific nodes.
Related topics