search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
xpath is usually used in conjunction with dom4j, And if you want to use xpath, you need to introduce a new package jaxen-1.1-beta-6.jar
1. The basic xpath syntax is similar to locating files in a file system. If the path starts with a slash / starts, then the path represents the absolute path to an element.
//, it means that all the files in the document satisfy Elements of the rules after the double slash // (regardless of hierarchical relationship)
* means selecting all elements located by the path before the asterisk
count()The function can count the number of selected elements
And if you want to use xpath, you need to introduce a new package jaxen-1.1-beta-6.jar
/ starts, then the path represents the absolute path to an element.
2.如果路径以双斜线//开头,则表示文档中所有满足双斜线//之后规则的元素(无论层级关系)
3.星号*表示选择所有由星号之前路径所定位的元素
4.方括号里的表达式可以进一步地指定元素,其中数字表示元素在选择集里的位置,而last()函数则表示选择集中的最后一个元素。特别要注意的是这里的下标是从1开始的,而不是0!
5.对属性的操作
6.属性的值可以被用来作为选择的准则
7.count()函数可以计数所选元素的个数
Home Backend Development XML/RSS Tutorial XML—XPATH syntax introduction

XML—XPATH syntax introduction

Feb 24, 2017 pm 03:19 PM


Why do we need xpath?

When using dom4j, we cannot obtain a certain element across layers. We must obtain it layer by layer, which is very troublesome.
So in order for us to access a certain node more conveniently, we can use xpath technology, which allows us to read the specified node very conveniently.

xpath is usually used in conjunction with dom4j, And if you want to use xpath, you need to introduce a new package jaxen-1.1-beta-6.jar

The basic syntax of xpath has the following points:

1. The basic xpath syntax is similar to locating files in a file system. If the path starts with a slash / starts, then the path represents the absolute path to an element.

(1) /AAA, which represents the selection of the root element AAA

<AAA>这里    <BBB/>
    <CCC/>
    <BBB/>
    <BBB/>
    <DDD>
        <BBB/>
    <DDD/>
    <CCC/><AAA/>这里

(2) /AAA/CCC, indicating the selection of all CCC sub-elements of AAA

<AAA>
    <BBB/>
    <CCC/>这里    <BBB/>
    <BBB/>
    <DDD>
        <BBB/>
    <DDD/>
    <CCC/>这里<AAA/>

(3) /AAA/DDD/BBB, indicating the selection All BBB sub-elements of AAA's sub-elements DDD

<AAA>
    <BBB/>
    <CCC/>
    <BBB/>
    <BBB/>
    <DDD>
        <BBB/>这里    <DDD/>
    <CCC/><AAA/>

So how to use xpath in dom4j? It's actually very simple:

//1.得到SAXReader解析器SAXReader saxReader = new SAXReader();
//2.指定去解析哪个文件Document document = saxReader.read(new File(path));
//3.可以使用xpath随心读取// document.selectNodes(args)返回多个元素
// document.selectSingleNode(args)返回单个元素List nodes = document.selectNodes("/AAA/BBB");

After getting the document object through dom4j, you can use the document's selectNodes(args) method. This method will return a List# based on the xpath path you wrote. ##, the remaining operations are similar to dom4j.

At the same time, it also has a

selectSingleNode(args) method, which is used to return a single Node.


The following continues to introduce other xpath syntax:

2. If the path starts with a double slash //, it means that all the files in the document satisfy Elements of the rules after the double slash // (regardless of hierarchical relationship)

(1)

//BBB, which means selecting all BBB elements

<AAA>
    <BBB/>这里    <CCC/>
    <BBB/>这里    <DDD>
        <BBB/>这里    </DDD>
    <CCC>
        <DDD>
            <BBB/>这里            <BBB/>这里        </DDD>
    </CCC></AAA>

(2)

//DDD/BBB, indicating that all parent elements are BBB elements of DDD

<AAA>
    <BBB/>
    <CCC/>
    <BBB/>
    <DDD>
        <BBB/>这里    </DDD>
    <CCC>
        <DDD>
            <BBB/>这里            <BBB/>这里        </DDD>
    </CCC></AAA>


3. Asterisk* means selecting all elements located by the path before the asterisk

(1)

/AAA/CCC/DDD/*, which means selecting all paths attached to / Elements of AAA/CCC/DDD:

<AAA>
    <XXX>
        <DDD>
            <BBB/>
            <BBB/>
            <EEE/>
            <FFF/>
        </DDD>
    </XXX>
    <CCC>
        <DDD>
            <BBB/>这里            
            <BBB/>这里            
            <EEE/>这里            
            <FFF/>这里        
            </DDD>
    </CCC>
    <CCC>
        <BBB>
            <BBB>
                <BBB/>
            </BBB>
        </BBB>
    </CCC></AAA>

(2)

/*/*/*/BBB, which represents all BBB elements with 3 ancestor elements

<AAA>
    <XXX>
        <DDD>
            <BBB/>这里            
            <BBB/>这里            
            <EEE/>
            <FFF/>
        </DDD>
    </XXX>
    <CCC>
        <DDD>
            <BBB/>这里            
            <BBB/>这里            
            <EEE/>
            <FFF/>
        </DDD>
    </CCC>
    <CCC>
        <BBB>这里            <BBB>
                <BBB/>
            </BBB>
        </BBB>
    </CCC></AAA>

(3)

//*, which means selecting all elements


4. The expressions in square brackets can further specify the elements, where the numbers represent The position of the element in the selection set, and the last() function represents the last element in the selection set. It is important to note that the subscripts here start from 1, not 0! (1)
/AAA/BBB[1], which means selecting the first BBB sub-element of AAA

<AAA>
    <BBB/>这个    <BBB/>
    <BBB/>
    <BBB/></AAA>

(2)

/AAA/ BBB[last()] means selecting the last BBB element of AAA

<AAA>
    <BBB/>
    <BBB/>
    <BBB/>
    <BBB/>这个</AAA>


5. Operations on attributes

(1)

//@id, select all id attributes. Note: all id attributes are returned as nodes, not nodes with id attributes.

<AAA>
    <BBB id="b1"/>返回这里的id属性节点    <BBB id="b2"/>也返回这里的id属性节点    <BBB name="bbb"/>
    <BBB/></AAA>

(2)

//BBB[@id], select all BBB nodes with id attributes

<AAA>
    <BBB id="b1"/>返回这个BBB节点    <BBB id="b2"/>也返回这个BBB节点    <BBB name="bbb"/>
    <BBB/></AAA>

(3)

//BBB[@ name], select all BBB nodes with name attribute

<AAA>
    <BBB id="b1"/>
    <BBB id="b2"/>
    <BBB name="bbb"/>返回这个BBB节点    <BBB/></AAA>

(4)

//BBB[@*], select all BBB nodes with attribute

<AAA>
    <BBB id="b1"/>返回这个BBB节点    <BBB id="b2"/>返回这个BBB节点    <BBB name="bbb"/>返回这个BBB节点    <BBB/></AAA>

(5)

//BBB[not(@*)], select all BBB nodes without attributes

<AAA>
    <BBB id="b1"/>
    <BBB id="b2"/>
    <BBB name="bbb"/>
    <BBB/>这个</AAA>


6. The value of the attribute can be used As the selection criteria

(1)

//BBB[@id='b1'], select the BBB element that contains the attribute id and its value is 'b1'

<AAA>
    <BBB id="b1"/>这个    <BBB name="bbb"/>
    <BBB name="bbb"/></AAA>


7.count()The function can count the number of selected elements

(1)

//* [count(BBB)=2], select the element containing 2 BBB sub-elements

<AAA>
    <CCC>
        <BBB/>
        <BBB/>
        <BBB/>
    </CCC>
    <DDD>返回这个元素        <BBB/>
        <BBB/>
    </DDD>
    <EEE>
        <CCC/>
        <DDD/>
    </EEE></AAA>

(2)

//*[count(*)=2], select Elements containing 2 sub-elements

<AAA>
    <CCC>
        <BBB/>
        <BBB/>
        <BBB/>
    </CCC>
    <DDD>返回这个元素        <BBB/>
        <BBB/>
    </DDD>
    <EEE>也返回这个元素        <CCC/>
        <DDD/>
    </EEE></AAA>

There are many other syntaxes, including the application of many functions, which are not used much and will not be introduced here


In addition, the syntax points introduced above can be combined in any combination, such as the following xml document:

<AAA>
    <BBB id="b1">
        <CCC>
            <KKK>k1</KKK>
        </CCC>
        <CCC>
            <KKK>k2</KKK>这个        </CCC>
    </BBB>
    <BBB id="b2"/>
    <BBB name="bbb"/></AAA>

If we now want to find the KKK sub-element of the second CCC sub-element under the first BBB sub-element under the AAA element element, the xpath path should be written like this:


/AAA/BBB[1]/CCC[2]/KKK

Why is xpath needed?

In use When using dom4j, we cannot obtain an element across layers. We must obtain it layer by layer, which is very troublesome.

So in order for us to access a certain node more conveniently, we can use xpath technology, which allows us to read the specified node very conveniently.

xpath is usually used in conjunction with dom4j, And if you want to use xpath, you need to introduce a new package jaxen-1.1-beta-6.jar

The basic syntax of xpath has the following points:

1. The basic xpath syntax is similar to locating files in a file system. If the path starts with a slash / starts, then the path represents the absolute path to an element.

(1)

/AAA, which represents the selection of the root element AAA

<AAA>这里    <BBB/>
    <CCC/>
    <BBB/>
    <BBB/>
    <DDD>
        <BBB/>
    <DDD/>
    <CCC/><AAA/>这里

(2)

/AAA/CCC, indicating the selection of all CCC sub-elements of AAA

<AAA>
    <BBB/>
    <CCC/>这里    <BBB/>
    <BBB/>
    <DDD>
        <BBB/>
    <DDD/>
    <CCC/>这里<AAA/>

(3)

/AAA/DDD/BBB, indicating the selection All BBB sub-elements of AAA's sub-elements DDD

<AAA>
    <BBB/>
    <CCC/>
    <BBB/>
    <BBB/>
    <DDD>
        <BBB/>这里    <DDD/>
    <CCC/><AAA/>

那么怎么在dom4j中运用xpath呢?其实很简单:

//1.得到SAXReader解析器SAXReader saxReader = new SAXReader();
//2.指定去解析哪个文件Document document = saxReader.read(new File(path));
//3.可以使用xpath随心读取
// document.selectNodes(args)返回多个元素
// document.selectSingleNode(args)返回单个元素List nodes = document.selectNodes("/AAA/BBB");

通过dom4j得到document对象后,可以使用document的selectNodes(args)方法,这个方法会根据你写的xpath路径返回一个List,余下的操作就和dom4j类似了。

同时它也有一个selectSingleNode(args)方法,用于返回一个单个的Node。


下面继续介绍其他的xpath语法:

2.如果路径以双斜线//开头,则表示文档中所有满足双斜线//之后规则的元素(无论层级关系)

(1)//BBB,它表示选择所有BBB元素

<AAA>
    <BBB/>这里    <CCC/>
    <BBB/>这里    <DDD>
        <BBB/>这里    </DDD>
    <CCC>
        <DDD>
            <BBB/>这里            <BBB/>这里        </DDD>
    </CCC></AAA>

(2)//DDD/BBB,表示所有父元素是DDD的BBB元素

<AAA>
    <BBB/>
    <CCC/>
    <BBB/>
    <DDD>
        <BBB/>这里    </DDD>
    <CCC>
        <DDD>
            <BBB/>这里            <BBB/>这里        </DDD>
    </CCC></AAA>

3.星号*表示选择所有由星号之前路径所定位的元素

(1)/AAA/CCC/DDD/*,它表示选择所有路径依附于/AAA/CCC/DDD的元素:

<AAA>
    <XXX>
        <DDD>
            <BBB/>
            <BBB/>
            <EEE/>
            <FFF/>
        </DDD>
    </XXX>
    <CCC>
        <DDD>
            <BBB/>这里            
            <BBB/>这里            
            <EEE/>这里            
            <FFF/>这里        
            </DDD>
    </CCC>
    <CCC>
        <BBB>
            <BBB>
                <BBB/>
            </BBB>
        </BBB>
    </CCC></AAA>

(2)/*/*/*/BBB,它表示所有的有3个祖先元素的BBB元素

<AAA>
    <XXX>
        <DDD>
            <BBB/>这里            
            <BBB/>这里            
            <EEE/>
            <FFF/>
        </DDD>
    </XXX>
    <CCC>
        <DDD>
            <BBB/>这里            
            <BBB/>这里            
            <EEE/>
            <FFF/>
        </DDD>
    </CCC>
    <CCC>
        <BBB>这里            
        <BBB>
                <BBB/>
            </BBB>
        </BBB>
    </CCC></AAA>

(3)//*,它表示选择所有的元素


4.方括号里的表达式可以进一步地指定元素,其中数字表示元素在选择集里的位置,而last()函数则表示选择集中的最后一个元素。特别要注意的是这里的下标是从1开始的,而不是0!
(1)/AAA/BBB[1],它表示选择AAA的第一个BBB子元素

<AAA>
    <BBB/>这个    <BBB/>
    <BBB/>
    <BBB/></AAA>

(2)/AAA/BBB[last()],表示选择AAA的最后一个BBB元素

<AAA>
    <BBB/>
    <BBB/>
    <BBB/>
    <BBB/>这个</AAA>

5.对属性的操作

(1)//@id,选择所有的id属性,注意:是把所有的id属性当做节点返回,而不是返回有id属性的节点。

<AAA>
    <BBB id="b1"/>返回这里的id属性节点    <BBB id="b2"/>也返回这里的id属性节点    <BBB name="bbb"/>
    <BBB/></AAA>

(2)//BBB[@id],选择所有有id属性的BBB节点

<AAA>
    <BBB id="b1"/>返回这个BBB节点    <BBB id="b2"/>也返回这个BBB节点    <BBB name="bbb"/>
    <BBB/></AAA>

(3)//BBB[@name],选择所有有name属性的BBB节点

<AAA>
    <BBB id="b1"/>
    <BBB id="b2"/>
    <BBB name="bbb"/>返回这个BBB节点    <BBB/></AAA>

(4)//BBB[@*],选择所有有属性的BBB节点

<AAA>
    <BBB id="b1"/>返回这个BBB节点    <BBB id="b2"/>返回这个BBB节点    <BBB name="bbb"/>返回这个BBB节点    <BBB/></AAA>

(5)//BBB[not(@*)],选择所有没有属性的BBB节点

<AAA>
    <BBB id="b1"/>
    <BBB id="b2"/>
    <BBB name="bbb"/>
    <BBB/>这个</AAA>

6.属性的值可以被用来作为选择的准则

(1)//BBB[@id='b1'],选择含有属性id且其值为’b1’的BBB元素

<AAA>
    <BBB id="b1"/>这个    <BBB name="bbb"/>
    <BBB name="bbb"/></AAA>

7.count()函数可以计数所选元素的个数

(1)//*[count(BBB)=2],选择含有2个BBB子元素的元素

<AAA>
    <CCC>
        <BBB/>
        <BBB/>
        <BBB/>
    </CCC>
    <DDD>返回这个元素        <BBB/>
        <BBB/>
    </DDD>
    <EEE>
        <CCC/>
        <DDD/>
    </EEE></AAA>

(2)//*[count(*)=2],选择含有2个子元素的元素

<AAA>
    <CCC>
        <BBB/>
        <BBB/>
        <BBB/>
    </CCC>
    <DDD>返回这个元素        <BBB/>
        <BBB/>
    </DDD>
    <EEE>也返回这个元素        <CCC/>
        <DDD/>
    </EEE></AAA>

还有很多其他的语法,包括很多函数的应用,用的不多,这里不做介绍


另外,上述介绍的几点语法可以任意组合,比如下述的xml文档:

<AAA>
    <BBB id="b1">
        <CCC>
            <KKK>k1</KKK>
        </CCC>
        <CCC>
            <KKK>k2</KKK>这个        </CCC>
    </BBB>
    <BBB id="b2"/>
    <BBB name="bbb"/></AAA>

假如我们现在要找AAA元素下面的第1个BBB子元素下面的第2CCC子元素的KKK子元素,则xpath路径应该这么写:
/AAA/BBB[1]/CCC[2]/KKK


 以上就是XML——XPATH语法介绍 的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

JSON vs. XML: Why RSS Chose XML JSON vs. XML: Why RSS Chose XML May 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

Understanding RSS Documents: A Comprehensive Guide Understanding RSS Documents: A Comprehensive Guide May 09, 2025 am 12:15 AM

RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.

Building XML Applications with C  : Practical Examples Building XML Applications with C : Practical Examples May 03, 2025 am 12:16 AM

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

RSS, XML and the Modern Web: A Content Syndication Deep Dive RSS, XML and the Modern Web: A Content Syndication Deep Dive May 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

XML in C  : Handling Complex Data Structures XML in C : Handling Complex Data Structures May 02, 2025 am 12:04 AM

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

Beyond Basics: Advanced RSS Features Enabled by XML Beyond Basics: Advanced RSS Features Enabled by XML May 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Understanding RSS: An XML Perspective Understanding RSS: An XML Perspective Apr 25, 2025 am 12:14 AM

RSS is an XML-based format used to publish frequently updated content. 1. RSSfeed organizes information through XML structure, including title, link, description, etc. 2. Creating RSSfeed requires writing in XML structure, adding metadata such as language and release date. 3. Advanced usage can include multimedia files and classified information. 4. Use XML verification tools during debugging to ensure that the required elements exist and are encoded correctly. 5. Optimizing RSSfeed can be achieved by paging, caching and keeping the structure simple. By understanding and applying this knowledge, content can be effectively managed and distributed.

Inside the RSS Document: Essential XML Tags and Attributes Inside the RSS Document: Essential XML Tags and Attributes May 03, 2025 am 12:12 AM

The core structure of RSS documents includes XML tags and attributes. The specific parsing and generation steps are as follows: 1. Read XML files, process and tags. 2. Extract,,, etc. tag information. 3. Handle custom tags and attributes to ensure version compatibility. 4. Use cache and asynchronous processing to optimize performance to ensure code readability.

Related articles