• XPath语法


    XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
    XPath使用路径表达式来选择XML文档的节或是节集。顺着路径或步骤来选择节。


    The XML Example Document
    XML实例文档

    We will use the following XML document in the examples below.
    举例中我们将使用下面的XML文档

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <bookstore>
    <book>  <title lang="eng">Harry Potter</title>  <price>29.99</price></book>
    <book>  <title lang="eng">Learning XML</title>  <price>39.95</price></book>
    </bookstore>


    Selecting Nodes
    选择节

    XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:
    一些非常有用的路径表达式:

    表达式 描述
    nodename Selects all child nodes of the node[选择所有目前节的子节]
    / Selects from the root node[从根节进行选择]
    // Selects nodes in the document from the current node that match the selection no matter where they are [选择文档中相吻合的节而不管其在文档的何处]
    . Selects the current node[选择当前节]
    .. Selects the parent of the current node[当前节的父节]
    @ Selects attributes[选择属性]

    Examples
    实例

    In the table below we have listed some path expressions and the result of the expressions:
    下面我们所列举的表格有路径表达式以及其结果:

    路径表达式 结果
    bookstore Selects all the child nodes of the bookstore element[选择所有bookstore元素的子节]
    /bookstore Selects the root element bookstore

    Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

    [选择了bookstore的根元素。注意:如果路径的开始为(/)那此路径一定是到该元素的绝对路径]
    bookstore/book Selects all book elements that are children of bookstore[选择了所有在bookstore的子元素book元素所包含的所有元素(其实就为bookstore里book元素所包含的元素)]
    //book Selects all book elements no matter where they are in the document[选择所有为book元素的内容而不管book元素处于何处(有不同的父也没关系)]
    bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element[在bookstore元素内所有含有book元素的元素内容(只要book元素的祖元素为bookstore元素那都符合条件)]
    //@lang Selects all attributes that are named lang[选择所有属性名为lang的属性]


    Predicates
    谓语

    Predicates are used to find a specific node or a node that contains a specific value.
    谓语用来指定明确的节所含有的特殊的值

    Predicates are always embedded in square brackets.
    谓语被嵌入在中括号

    Examples
    举例

    In the table below we have listed some path expressions with predicates and the result of the expressions:
    下面的表格列举了一些使用了谓语的路径表达式以及其产生的结果:

    路径表达式 结果
    /bookstore/book[1] Selects the first book element that is the child of the bookstore element[选择了bookstore里的第一个book元素]
    /bookstore/book[last()] Selects the last book element that is the child of the bookstore element[选择bookstore里最后一个book元素]
    /bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element[bookstore中倒数第二个book元素]
    /bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element[在bookstore中前两个book元素]
    //title[@lang] Selects all the title elements that have an attribute named lang[选择所有含有lang属性的title元素]
    //title[@lang=***eng***] Selects all the title elements that have an attribute named lang with a value of ***eng***[选择所有含有lang属性并且值为eng的title元素]
    /bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00[选择所有bookstore中book元素里price元素内容大于35.00的book元素]
    /bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00[选择bookstore中book的子元素title,并且其兄弟元素price的内容得大于35.00]


    Selecting Unknown Nodes
    选择未知的节

    XPath wildcards can be used to select unknown XML elements.
    XPath的通配符可以用来选择未知的XML元素

    通配符 描述
    * Matches any element node[相吻合的所有元素节]
    @* Matches any attribute node[相吻合的所有属性节]
    node() Matches any node of any kind[吻合任何类型的节]

    Examples实例

    In the table below we have listed some path expressions and the result of the expressions:
    下面的表格我们将列举一些路径表达式以及它们的结果

    路径表达式 结果
    /bookstore/* Selects all the child nodes of the bookstore element[选择所有bookstore的子节]
    //* Selects all elements in the document[选择所有文档中的元素]
    //title[@*] Selects all title elements which have any attribute[选择元素为title并且其含有属性]


    Selecting Several Paths
    选择数个路径

    By using the | operator in an XPath expression you can select several paths.
    通过在XPath中使用 | 你可以选择数个路径

    Examples
    实例

    In the table below we have listed some path expressions and the result of the expressions:
    下面的表格我们会列举一些路径表达式以及其结果:

    路径表达 结果
    //book/title | //book/price Selects all the title AND price elements of all book elements[选择所有book里title和price元素]
    //title | //price Selects all the title AND price elements in the document[选择所有title和price元素]
    /bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document[选择所有book里的title元素和所有price元素]
     
  • 相关阅读:
    java中 == 与 equals
    java中只有值传递
    java中8种基本类型、包装类、常量池
    Markdown知识
    logback的使用和logback.xml详解[转]
    Log4J日志配置详解[转]
    批量删除Redis数据库中的Key
    linux的top命令参数详解
    Java之美[从菜鸟到高手演变]系列之博文阅读导航
    Java 密码扩展无限制权限策略文件[转]
  • 原文地址:https://www.cnblogs.com/zzh/p/1113307.html
Copyright © 2020-2023  润新知