XPath 축
XML 예제 문서
아래 예제에서는 다음 XML 문서를 사용합니다.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
XPath 축
축은 컨텍스트(현재) 노드에 대한 관계를 나타내며 트리에서 해당 노드를 기준으로 노드를 찾는 데 사용됩니다.
AxisName | Result |
---|---|
ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node |
ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself |
attribute | Selects all attributes of the current node |
child | Selects all children of the current node |
descendant | Selects all descendants (children, grandchildren, etc.) of the current node |
descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself |
following | Selects everything in the document after the closing tag of the current node |
following-sibling | Selects all siblings after the current node |
namespace | Selects all namespace nodes of the current node |
parent | Selects the parent of the current node |
preceding | Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes |
preceding-sibling | Selects all siblings before the current node |
self | Selects the current node |
위치 경로 표현식
위치 경로는 절대적이거나 상대적일 수 있습니다.
절대 위치 경로는 슬래시( / )로 시작하고 상대 위치 경로는 슬래시로 시작하지 않습니다. 두 경우 모두 위치 경로는 슬래시로 구분된 하나 이상의 단계로 구성됩니다.
An absolute location path:
/step/step/...
A relative location path:
step/step/...
각 단계는 현재 노드 집합의 노드에 대해 평가됩니다.
단계는 다음으로 구성됩니다.
- 축(선택한 노드와 현재 노드 간의 트리 관계 정의)
- node-test(축 내에서 노드 식별)
- 0개 이상의 술어(선택한 노드 세트를 더 세분화하기 위해)
위치 단계의 구문은 다음과 같습니다.
axisname::nodetest[predicate]
예
Example | Result |
---|---|
child::book | Selects all book nodes that are children of the current node |
attribute::lang | Selects the lang attribute of the current node |
child::* | Selects all element children of the current node |
attribute::* | Selects all attributes of the current node |
child::text() | Selects all text node children of the current node |
child::node() | Selects all children of the current node |
descendant::book | Selects all book descendants of the current node |
ancestor::book | Selects all book ancestors of the current node |
ancestor-or-self::book | Selects all book ancestors of the current node - and the current as well if it is a book node |
child::*/child::price | Selects all price grandchildren of the current node |