Here, in this article I would brief about querying XML Document Node values using X-path
XPath is used to navigate through elements and attributes in an XML document. It is a major element in W3C's XSLT standard. XPath uses path expressions to navigate in XML documents
It contains a library of standard functions and is a major element in XSLT; without XPath knowledge you will not be able to create XSLT documents.
XPath Terminology
Nodes
In xpath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes.
XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.
x-path queries
Lets see about relationship between nodes and read element value using xpath with the below xml document,
<AddressBookEntryIdentifier>1212121212</AddressBookEntryIdentifier>
<ConsumerOrderNumber>100-200</ConsumerOrderNumber>
<Orders>
<Order>
<MetadataCollection>
<Metadata>
<Name>LoanNumber</Name>
<Value>100</Value>
</Metadata>
<Metadata>
<Name>TID</Name>
<Value>10</Value>
</Metadata>
<Metadata>
<Name>ID</Name>
<Value>11</Value>
</Metadata>
<Metadata>
<Name>ProviderID</Name>
<Value>1000</Value>
</Metadata>
</MetadataCollection>
<OrderNumber>10000</OrderNumber>
<ReferenceNumbers>
<OrderReference>
<ReferenceNumber>10101010</ReferenceNumber>
<ReferenceType>FileId</ReferenceType>
</OrderReference>
</ReferenceNumbers>
</Order>
<Order>
<MetadataCollection>
<Metadata>
<Name>LoanNumber</Name>
<Value>200</Value>
</Metadata>
<Metadata>
<Name>TID</Name>
<Value>20</Value>
</Metadata>
<Metadata>
<Name>EID</Name>
<Value>21</Value>
</Metadata>
<Metadata>
<Name>ProviderID</Name>
<Value>2000</Value>
</Metadata>
</MetadataCollection>
<OrderNumber>20000</OrderNumber>
<ReferenceNumbers>
<OrderReference>
<ReferenceNumber>202020</ReferenceNumber>
<ReferenceType>FileId</ReferenceType>
</OrderReference>
</ReferenceNumbers>
</Order>
</Orders>
</OrderCollection>
Relationship of Nodes
Parent
-
Each element and attribute has one parent.
- In the sample shown; the Order element is the parent of the MetadataCollection, OrderNumber and ReferenceNumbers Child
-
Element nodes may have zero, one or more children.
- In the example; the ReferenceNumber and ReferenceType elements are all children of the OrderReference element Siblings
-
Nodes that have the same parent.
- In the sample shown example; the Name and Value elements are all siblings Ancestors
-
A node's parent, parent's parent, etc.
- In the sample shown; the ancestors of the Order element are the Orders and OrderCollection elements Xpath Queries :
Select all the occurrence of an element irrespective of its parent,
\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='']/\*[local-name()='MetadataCollection' and namespace-uri()='']/\*[local-name()='Metadata' and namespace-uri()='']\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='']/\*[local-name()='MetadataCollection' and namespace-uri()='']/\*[local-name()='Metadata' and namespace-uri()='' and \*[local-name()='Name' and namespace-uri()='' and .='ProviderID']]/\*[local-name()='Value' and namespace-uri()='']\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='']/\*[local-name()='ReferenceNumbers' and namespace-uri()='']/\*[local-name()='OrderReference' and namespace-uri()='' and \*[local-name()='ReferenceType' and namespace-uri()='' and .='FileId']]/\*[local-name()='ReferenceNumber' and namespace-uri()='']
Select an element within a specific parent
-
\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='' and \*[local-name()='OrderNumber' and namespace-uri()='' and .='10000']]/\*[local-name()='MetadataCollection' and namespace-uri()='']/\*[local-name()='Metadata' and namespace-uri()='' and \*[local-name()='Name' and namespace-uri()='' and .='ProviderID']]/\*[local-name()='Value' and namespace-uri()='']
Select parent with its child element
\*[local-name()='Metadata' and namespace-uri()='' and \*[local-name()='Name' and namespace-uri()=''and .='ProviderID']]
Select an element with its sibling
-
\*[local-name()='Metadata' and namespace-uri()='' and \*[local-name()='Name' and namespace-uri()='' and .='ProviderID']]/\*[local-name()='Value' and namespace-uri()='']
Select an element with case insensitive evaluation using translate function
-
\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='']/\*[local-name()='MetadataCollection' and namespace-uri()='']/\*[local-name()='Metadata' and namespace-uri()='' and \*[local-name()='Name' and namespace-uri()='' and translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ='id']]
Select Ancestor/Parent with its child
\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='' and \*[local-name()='OrderNumber' and namespace-uri()='' and .='10000']]\*[local-name()='OrderCollection' and namespace-uri()='']/\*[local-name()='Orders' and namespace-uri()='']/\*[local-name()='Order' and namespace-uri()='' and \*[local-name()='ReferenceNumbers' and namespace-uri()='' and \*[local-name()='OrderReference' and namespace-uri()='' and \*[local-name()='ReferenceNumber' and namespace-uri()='' and .='10101010']]]]
Conclusion :
This article would help you understand writing xpath queries on xml and as BizTalk Server is a message-based system and all the messages that enter the BizTalk are converted to XML format, it would help you read elements inside maps/orchestration and pipeline components using xpaths
Top comments (0)