Learn how to get started with XPath

Source: Internet
Author: User
Tags xml attribute xpath xslt

What is XPath

XPath (extensible path) is the result of providing a common syntax and semantics for the shared functionality of XSL transformations [XSLT] and Xpointer[xpointer. The main purpose of XPath is to address XML document components. While supporting this primary purpose, it also provides a basic tool for string, digital, and Boolean operations. XPath uses concise, non-XML syntax to manipulate the abstract, logical structure of an XML document, rather than its surface syntax, within URIs and XML attribute values, Xpath,xpath The name of the XPath is derived from its use in the URL as a path flag for navigating the hierarchy of XML documents.

In addition to being used for addressing, XPath is designed so that it has a natural subset that can be used to match (test whether a node matches a pattern), and XSLT describes the use of XPath in this regard.

XPath models an XML document into a node tree with different types of nodes, including element nodes, attribute nodes, and body nodes. XPath defines a method to compute the string value of a node for each class. Some types of nodes also have names. XPath fully supports XML namespaces [Xmlnames]. In this way, the name of the node is modeled as a pair consisting of a local part and possibly an empty namespace URI, which is called an extension.

Types returned by XPath

1: Node set (unordered, no duplicate node set)

2: Boolean (True or FALSE)

3: Number (a floating-point number)

4: String (Order of UCS characters)

Address path

An address path is a statement that XPath uses to locate

, the basic syntax is as follows:

/Select the root node of the XML document

/* Select all child nodes of the root node,

* Match any child nodes

/x selects all x elements of the root node

Book Select the book element in all descendant nodes of the root node

@id Select a child node that contains an id attribute

PARA[1] Select the first para child of the context node

vendor[@id = ' id1_2 ']/book Select all book elements that conform to the attribute id= ' id1_2 '

/BIB/VENDOR/BOOK[YEAR>2002] Selects all book elements that conform to the element year> ' 2002 '

Text () selects all the body nodes of the context node child

@name Select the Name property of the context node

@* selects all properties of the context node

*/para Select the context node of all the para grandchildren

/DOC/CHAPTER[5]/SECTION[2] Select the second section of Doc's fifth chapter

.//para select the Para element of the context node descendants

para[@type = "Warning"] all of the context nodes selected have attribute type and the value is warning para child

chapter[title= "Introduction" Select the chapter child of the context node if it has one or more title children and the string value is Introduction

employee[@secretaryand @assistant] Selects all Employee children with both Secretary attributes and assistant attributes for the context node

Chpater[title] The context node selected has one or more chpater children of the title child

1:child is the default axis. For example, the address path Div/para is a child::d iv/child::p ara abbreviation.

2: Attributes also have abbreviated form: attribute:: Can be abbreviated as @. For example, the address path para[@type = "Warning"] is the child::p the abbreviation for "Warning", which is warning children who have the type attribute and whose attribute value is para.

3://is a/descendant-or-self::node ()/abbreviation. For example,//para is the abbreviation for/descendant-or-self::node ()/child::p ara, so select all Para elements in the document (even if the para element is a document element, it will be chosen by the//para. Because the document element is the child of the root node; Div//para is Div/descendant-or-self::node ()/child::p ara abbreviation, so will select all para descendants of Div child

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.