News Ticker

Menu

XPath

<XML_XPath> Bài 8: Một số cú pháp cơ bản của XPath.

1.Select node

Các bạn có thể đọc tại http://www.w3schools.com/xsl/xpath_syntax.asp.

2.Các toán tử

Các bạn có thể đọc tại http://www.w3schools.com/xsl/xpath_operators.asp.

3. Ví dụ: <nguồn https://www.w3.org>

  • - para selects the para element children of the context node
  • - * selects all element children of the context node
  • - text() selects all text node children of the context node
  • - @name selects the name attribute of the context node
  • - @* selects all the attributes of the context node
  • - para[1] selects the first para child of the context node
  • - para[fn:last()] selects the last para child of the context node
  • - */para selects all para grandchildren of the context node
  • - /book/chapter[5]/section[2] selects the second section of the fifth chapter of the book whose parent is the document node that contains the context node
  • - chapter//para selects the para element descendants of the chapter element children of the context node
  • - //para selects all the para descendants of the root document node and thus selects all para elements in the same document as the context node
  • - //@version selects all the version attribute nodes that are in the same document as the context node
  • - //list/member selects all the member elements in the same document as the context node that have a list parent
  • - .//para selects the para element descendants of the context node
  • - .. selects the parent of the context node
  • - ../@lang selects the lang attribute of the parent of the context node
  • - para[@type="warning"] selects all para children of the context node that have a type attribute with value warning
  • - para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning
  • - para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
  • - chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children whose typed value is equal to the string Introduction
  • - chapter[title] selects the chapter children of the context node that have one or more title children
  • - employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute
  • - book/(chapter|appendix)/section selects every section element that has a parent that is either a chapter or an appendix element, that in turn is a child of a book element that is a child of the context node.
  • - If E is any expression that returns a sequence of nodes, then the expression E/. returns the same nodes in document order, with duplicates eliminated based on node identity.

4. Các Hàm <Functions>

4.1 Boolean functions ( T - F )

• Boolean boolean(arg) //returns false if object equal to zero/NaN/empty 
• Boolean false() //returns the Boolean value false 
• Boolean lang(string arg) //to check language which is used in context node 
• Boolean not(arg) //
• Boolean true() // returns the Boolean value true

4.2 Node-set Functions

• number count(node-set arg) //returns the number of nodes 
• node-set id(string arg) //returns a node-set 
• last() // returns a value equal to the context size 
• string local-name(optional node-set arg) //returns local name(context node) 
• string name(optional node-set arg) //returns the nam of node in QName format 
• string namespace-uri(optional node-set arg) //returns namespace-uri of node-set 
• Number position() //returns value equal to the context position

4.3 Numeric Functions

• number ceiling( number arg) //returns the smallest integer greater than this 
• number floor(number arg) //returns the largest integer that is lower than this 
• number number(arg) //to convert string, Boolean, node-set to number //returns NaN if can not convert 
• number round(number arg) //returns the integer that is closest to the number arg 
• number sum(node-set arg) // return sum of node in node-set // return NaN if node can’t converted to number

4.4 String Functions

• string concat(string arg1, arg2) //returns the concatenation of those strings 
• Boolean contains(string arg1, arg2) //returns true if arg1 contains arg2 
• string normalize-space(string arg1) //=trim & replace whitespace=single space 
• Boolean start-with(string arg1, arg2) //returns true if arg1 starts with arg2 
• string string(arg) //to convert Boolean, node-set, number to string 
• number string-length(string arg) //returns the length of the string arg 
• string substring(string arg1, number arg2, optional number arg3) //return substring of arg1 
• string substring-before(string arg1, arg2) // 
• string substring-after(string arg1, arg2) // 
• string translate(string arg1, arg2, arg 3) //

Chúc các bạn thành công!

Share This:

Post Tags:

1 comment to ''XPath"

ADD COMMENT

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM