XML study (Module 2 in the XML with XPATH Travel (Part I))

We have seen the structure and syntax of XML is relatively simple. XML provides astandard way to exchange information between computers. The next step is to understand how a processing program (process) an XML document

Of course for an XML processing application programs have to go back inside to retrieve the material values of the Elements or Attributes. Therefore we designed the language XML Path Language , which is referred to as XPath . XPath plays an important role in the exchange of data between computers or between application programs because it allows us to select or screen out any information you want to discuss or display.
If working with a database SQL statement we use the Select .. WHERE TableXYZ from ... to extract some records from a table, then when working with XML, a small table of data, XPath expressions on the criteria for us (conditions) as clause like WHERE in SQL.

XPath is an XML standard to process, just as SQL is a standard for working with databases. Forward in implementing the program is to apply the XPath of the big software companies like Microsoft, Oracle, Sun, IBM, etc. The reason we need a standard XPath because it is applied in many circumstances , so theoretically should have a clear and accurate.

XPath theory but it is rather difficult to apply in any XML technology family. So please be patient to master the basics of it so when people use XPath gap, I recognize and understand. Compared to the martial arts, the XPath in the XML like breathing methods and Tan. Legal practice Tan, each leg, breathing exercises are boring, but there are two things that the cast does not have the resources, not beat it lost.

I will only learn things commonly used in XPath only, if you want full details of the XPath specification can refer to it in http://www.w3c.org/TR/xpath .

XML as a tree for XPath

XPath syntax for me to describe how to travel in the XML. We consider an XML document is represented by a tree (the tree) with many nodes. Each node is an Element or Attribute. To illustrate this concept, you take a look at the document order (order) the following XML:
<? xml version = "1.0" ?> 
< Order OrderNo = "1047" > 
   < order date > 03/26/2002 </ date order > 
   < Customer > John Costello </ Customer > 
   < Item > 
      < Product ProductID = "1" UnitPrice = "70" > Chair </ Product > 
      < Quantity > 6 </ Quantity > 
   </ Item > 
   < Item > 
      < Product ProductID = "2" UnitPrice = "250" > Desktop </ Product > 
      < Quantity > 1 </ Quantity > 
   </ Item > 
</ Order >

XML can be represented by a Tree on the link below, in which the Element node brown, blue node Attribute:

Specify Location Path

You can use XPath expression to specify the Location Path (path to the location) to extract nodes, or (pay for) one or more nodes agree on the requirements. XPath expression can be absolute , that is taking root node as a standard or relative , ie starting from the newly selected node. Node is called context node (node role in the situation).

There are two ways to write XPath Location express, written resources and abbreviations. In both ways we are using slash marks ( / ) to refer to Document Elements , which is the root node. We can travel in the same node of the tree as nodes of the Windows System Directory, which we see in the left pane of Windows Explorer. We will also use symbols such as slash / , a dot . and two dots .. to the Windows System File Folder Location abbreviations in XPath nodes to go down the children, grandchildren, only the context node, or run counter to the ancestor nodes.

Location Path absolute

Let's find some paths in the Tree location of the XML document of the said Order. Want to select the node of the Element Order (it is also the Root Element) with the raw syntax, we will use the following XPath expression:


Translated into the syntax is off, this expression becomes:


Take out the branch of the Tree, we will find a node customer by using the following XPath expression:


The following are abbreviated XPath expression equivalent:


If you want to remove a node attribute, you must specify this by using the key word (keyword) attribute the writing style used raw or character @ in the short syntax. So to get Attribute OrderNo Order , we will use the following XPath expression: the Element


OrderNo attribute syntax is referred to:

  / Order / @ OrderNo

To extract the children nodes, ie nodes beyond branch, use keyword descendant of the original syntax, or a double slash (/ /) in the short syntax. For example, to retrieve the Product nodes in the document, you can use location path expression follows:


Syntax is the equivalent off:


You can also use wildcards (Joker card) to refer to the nodes that their names did not matter. For example, the asterisk mark (*) wildcard specify any node name. The following location path selects all child nodes of Element Order :


Syntax is the equivalent off:


Location Path relative

Many XPath location paths are relative to the context node, location path where it describes how to remove a node or a (set of) nodes relative to the context node. For example, if the Element item first in order is the context node, then the location path relative to extract the child Element Quantity is:


The syntax is off, the relative location path is:


Similarly, to retrieve the ProductID Attribute Element of the Product, the relative locationpath:


Expression was translated into short syntax:


To go back to the top of the tree, we use the keyword parent (father). Short form of this keyword is the equivalent of two dots ( .. ). For example if the context node is Element OrderDate , the Attribute OrderNocan be removed from the Element Order using a relative path following location:


Note that this syntax returns a value only when the parent node named Order . If you want to retrieve the Attribute OrderNo from parent node does not need to know its name you have used the following expression:


Write off more simple style because you do not need to provide the name of the parent node. You can talk to the father node by using two dots ( .. ) as follows:


Also, you can refer to the context node or keyword using the self or a dot ( . ). This is very convenient in some cases, especially if you want to know the current context node is any node.

Conditions used in the Location Path

You can limit the number of nodes retrieved by attaching additional conditions to the screening location path. The condition limits one or more nodes in the expression is inside a tower brackets ( [] ). For example, to remove any Element Product with Attribute UnitPrice greater than 70, you can use the following XPath expression:


The syntax is off, it was:


In the expression of conditions you can also use relative XPath, so the expression conditions, you can use any node in the hierarchy. The following example taken on the nodes Item has the Element Product with Attibute ProductID value to 1:


Translated into the syntax is off, we have:




Post a Comment