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:

  /child::Order

Translated into the syntax is off, this expression becomes:

  /Order

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

  /child::Order/child::Customer

The following are abbreviated XPath expression equivalent:

  /Order/Customer

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

  /child::Order/attribute::OrderNo

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:

  /child::Order/descendant::Product

Syntax is the equivalent off:

  /Order//Product

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 :

  /child::Order/child::*

Syntax is the equivalent off:

  /Order/*

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:

  child::Quantity

The syntax is off, the relative location path is:

  Quantity

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

  child::Product/attribute::ProductID

Expression was translated into short syntax:

  Product/@ProductID

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:

  parent::Order/attribute::OrderNo

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:

  parent::*/attribute::OrderNo

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:

  ../@OrderNo

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:

  /child::Order/child::Item/child::Product[attribute::UnitPrice>70]

The syntax is off, it was:

  /Order/Item/Product[@UnitPrice>70]

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:

  /child::Order/child::Item[child::Product/attribute::ProductID=1]

Translated into the syntax is off, we have:

  /Order/Item[Product/@ProductID=1]

(Continued)

0 comments:

Post a Comment