DOM and XPATH

DOM

  • Originally developed for HTML. Supported by most browsers.

  • Represents the content of the XML or HTML document as a tree structure.

  • Using DOM, we can easily read and update the contents of the document.

D: Document —> file

O: Object —> tags elements

M: Model —> layout structure

Online DOM viewer:

Live DOM Viewer

XPATH

  • Allows us to use path expressions to navigate an XML document.

  • It allow us to select only the nodes we're interested in.

its :

  • Expression language.

  • Contains some standard functions.

  • Non-XML

XPATH works on the following kinds of nodes:

ExpressionDescription
nodenameSelects all nodes with the name"nodename"
/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 value of the current node
..Selects the parent of the current node
@Selects attributes

Predicates

  • similar to filters

  • They always appear in[]

  • They appear after the axis and node text

Path ExpressionResult
/bookstore/book[1]Selects the first book element that is the child of the bookstore element.
/bookstore/book[last()]Selects the last book element that is the child of the bookstore element
/bookstore/book[last()-1]Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3]Selects the first two book elements that are children of the bookstore element
//title[@lang]Selects all the title elements that have an attribute named lang
//title[@lang='en']Selects all the title elements that have a "lang" attribute with a value of "en"
/bookstore/book[price>35.00]Selects all the book elements of the bookstore element that have a price element
/bookstore/book[price>35.00]/titleSelects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

XPATH Axes

an Axes represents a relationship to the current node on the tree.

AxisNameResult
ancestorSelects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attributeSelects all attributes of the current node
childSelects all children of the current node
descendantSelects all descendants (children, grandchildren, etc.) of the current node
descendant-or-selfSelects all descendants (children, grandchildren, etc.) of the current node and the current node itself
followingSelects everything in the document after the closing tag of the current node
following-siblingSelects all siblings after the current node
namespaceSelects all namespace nodes of the current node
parentSelects the parent of the current node
precedingSelects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
preceding-siblingSelects all siblings before the current node
selfSelects the current node
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Benaso

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值