KyungHwan's etc.

XPath 본문

XML/XSL

XPath

KyungHwan_0 2018. 5. 31. 14:32

1. XPath

XPath(XML Path Language)는 W3C의 표준으로 확장 생성 언어 문서의 구조를 통해 경로 위에 지정한 구문을 사용하여 항목을 배치하고 처리하는 방법을 기술하는 언어이다.

1.1 XPath 소개

1.1.1 What is XPath?

  • XPath 는 XML Path Language 이다.

  • XPath는 "path like" 구문을 사용하여 XML 문서의 노드를 식별하고 탐색한다.

  • XPath에는 200 가지가 넘는 내장 함수가 포함되어 있다.

  • XPath는 XSLT 표준의 주요 요소 이다.

  • XPath 는 W3C 의 권장사항이다.

1.1.2 XPath Expressions

XPath 는 XML document 에서 node 들 혹은 node-sets 를 선택하기 위해 path expression 을 사용한다.

이러한 path expression 들은 컴퓨터 파일 시스템에서 전통적으로 사용하는 path expression 과 매우 비슷한다.

1.1.3 XPath Standard Functions

XPath 는 200 개이상의 내장 함수들을 가지고 있다.

이 함수들은 string 값, numeric 값, booleans, 날짜 및 시간 비교, node 조작, sequence 조작과 그 이상의 것들에 대한 기능을 가지고 있다.

오늘날 XPath 표현식은 JavaScript, Java, XML 스키마, PHP, Python, C 및 C ++ 및 기타 많은 언어에서도 사용할 수 있다.

1.1.4 XPath is Used in XSLT

XPath 는 XSLT 표준의 주요 요소이다.

XPath 지식을 사용하면 XSLT 지식을 최대한 활용할 수 있다.

1.1.5 XPath 는 W3C 권장사항이다.

XPath 1.0(1999.11.16) specification : http://www.w3.org/TR/xpath

XPath 2.0(2007.01.23) specification : https://www.w3.org/TR/xpath20/

XPath 3.0(2014.04.08) specification : https://www.w3.org/TR/xpath30/

1.2 XPath Nodes

1.2.1 XPath 용어

1.2.1.1 Nodes

XPath에는 Element, Attribute, Text, Namespace, Processing-instruction, Comment, and Document 의 총 7가지의 Node 들로 구성되어 있다.

XML document 도 트리를 구성하는 Node로 취급된다. Element 트리 중 최고 상위 Element 를 Root Element 라고 부른다.

아래의 XML document 예제를 보자:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 <book>
   <title lang="en">Harry Potter</title>
   <author>J K. Rowling</author>
   <year>2005</year>
   <price>29.99</price>
 </book>
</bookstore>

위 XML 문서의 노드 예:


<bookstore> (Root Element 노드)

<author>J K. Rowling</author> (element node)

lang="en" (attribute node)

1.2.1.2 Atomic values

Atomic values 는 부모나 자식이 없는 노드를 말한다.

atomic values 의 예는 아래와 같다:


J K. Rowling

"en"

1.2.1.3 Items

Items 는 atomic values 혹은 nodes 이다.

1.2.2 노드들의 관계

1.2.2.1 Parent

각 element 와 attribute 는 하나의 parent 를 가진다.

아래 예제에서 booke element 는 title, author, year, 그리고 price 의 부모이다.


<book>
 <title>Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
 <price>29.99</price>
</book>

1.2.2.2 Children

Element node 들은 0 혹은 그 이상의 자식을 가지고 있다.

다음 예제를 보면, title, author, year 그리고 price elements 들이 book element 의 모두 자식들이다.


<book>
 <title>Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
 <price>29.99</price>
</book>

1.2.2.3 Siblings

같은 부모를 가진 노드들을 말한다.

아래의 예제를 보면, title, author, year, 그리고 price elements 들이 모두 형제(siblings)노드 이다.


<book>
 <title>Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
 <price>29.99</price>
</book>

1.2.2.4 Ancestors

하나의 노드의 부모, 부모의 부모 등.

다음의 예제를 보면, title element 의 조상(ancestors) 은 book element 와 bookstore element 이다.


<book>
 <title>Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
 <price>29.99</price>
</book>

1.2.2.5 Descendants

하나의 노드의 자식, 자식의 자식 등

다음 예제를 보면, boostore element 의 자손(descendants)은 book, title, author, year, 그리고 price element 들이다.

1.3 XPath Syntax

XPath 는 XML document 에 있는 node 혹은 node-sets 를 선택하기 위해 path expression 을 사용한다. node 는 path 혹은 steps(단계)를 따라 선택된다.

1.3.1 예제 XML Document

아래 예제에서 다음 XML Document 를 사용한다.


<?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>

1.3.2 Node 선택(Selecting Nodes)

XML document 에서 노드를 선택하기 위해 path expression 을 사용한다. 노드는 path 혹은 steps 를 따라 선택된다. 아래에 가장 많이 사용되는 path expression 을 나열하였다.

Expression Description
nodename 이름이 “nodename” 인 모든 노드를 선택한다.
/ root 노드(루트 노드)부터 순서대로 탐색한다.
// 현재 노드의 위치와 상관없이 지정된 노드에서부터 순서대로 탐색한다.
. 현재 노드를 선택한다.
.. 현재 노드의 부모 노드를 선택한다.
@ attribute 를 선택한다.

아래 표에서 몇가지 path expression 과 expression 결과를 나열하였다.

Path Expression 결과
bookstore 이름이 “bookstore” 인 모든 노드(모든 자식 노드 포함)를 선택한다.
/bookstore 루트 경로의 하위 엘리먼트 bookstore 를 선택한다(절대 경로)
bookstore/book bookstore 엘리먼트의 자식 엘리먼트 중 book 엘리먼트를 모두 선택한다.
//book XML document 내의 모든 book 엘리먼트를 선택
bookstore//book bookstore 엘리먼트의 자손인 모든 book 엘리먼트 선택
//@lang XML document 내에서 name 이 lang 인 모든 attribute 를 선택.

1.3.3 Predicates

Predicates 는 특정한 노드 혹은 특정한 값을 가진 노드를 찾기 위해 사용된다.

Predicates 는 항상 대괄호 안에 포함된다.

아래 테이블에서 prediactes 와 expressions 의 결과가 있는 path expression 을 나열했다.

Path Expression 결과
/bookstore/book[1] bookstore 엘리먼트의 자식 엘리먼트 중 첫번째 book 엘리먼트를 선택
/bookstore/book[last()] bookstore 엘리먼트의 자식 엘리먼트 중 마지막 book 엘리먼트를 선택
/bookstore/book[last()-1] bookstore 엘리먼트의 자식 엘리먼트 중 마지막에서 첫번째 book 엘리먼트를 선택
/bookstore/book[position()<3] bookstore 엘리먼트의 자식 엘리먼트 중 처음부터 2개의 book 엘리먼트를 선택
//title[@lang] 문서 내의 모든 title 엘리먼트 중 attribute 의 name이 lang 인 것 들을 선택
//title[@lang='en'] 문서 내의 모든 title 엘리먼트 중 attribute 의 name이 lang 이고, 값이 ‘en’ 인 들을 선택
/bookstore/book[price>35.00] bookstore 엘리먼트의 자식 엘리먼트인 book 엘리먼트들 중에서 자식 엘리먼트 price의 값이 35보단 큰 노드들을 선택
/bookstore/book[price>35.00]/title 자식엘리먼트 price가 35보다 큰 book 엘리먼트의 자식 엘리먼트인 title 엘리먼트를 모두 선택

1.3.4 Selecting Unknown Nodes

XPath 와일드 카드(*) 를 사용하여 알 수 없는 XML 노드들(unknows XML nodes)를 선택할 수 있다.

Wildcard 설명
* 모든 엘리먼트 노드
@* 모든 attribute 노드(속성 노드)
node() 모든 종류의 노드

아래 표에서 몇 가지 XPath expression 과 expression 의 결과를 나열했다.

Path Expression 결과
/bookstore/* bookstore 엘리먼트의 모든 자식 엘리먼트 노드들을 선택
//* 이 문서의 모든 엘리먼트들을 선택
//title[@*] 적어도 하나의 attribute 를 가진 모든 title 엘리먼트를 선택

1.3.5 Selecting Several Paths

XPath expression 의 | 연산자를 이용하여 여러 경로들을 선택할 수 있다.

아래 표에서 몇 가지 XPath expression 과 expression 의 결과를 나열했다.

Path Expression 결과
//book/title | //book/price 모든 book 엘리먼트들 중에서 title 과 price 엘리먼트들을 선택
//title | //price document 에서 모든 title 과 price 엘리먼트들을 선택
/bookstore/book/title | //price document 에서 모든 price 엘리먼트들과 bookstore의 손자이며 book의 자식인 모든 title 엘리먼트를 선택

1.4 Axes

1.4.1 예제 XML Document

아래 예제에서 다음 XML Document 를 사용한다.


<?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>

1.4.2 XPath Axes

axis 현재노드를 기준으로 node-set 을 정의한다.

AxisName 결과
ancestor 현재 노드의 모든 조상 노드들(ancestors)을 선택(parent, grandparent, 기타.)
ancestor-or-self 현재 노드와 현재 노드의 모든 조상 노드들(ancestors)을 선택(parent, grandparent, 기타.)
attribute 현재 노드의 모든 attribute 들을 선택
child 현재 노드의 모든 자식 노드를 선택
descendant 현재 노드의 모든 자손(descendants) 노드들(children, grandchildren, 기타)을 선택
descendant-or-self 현재 노드와 현재 노드의 모든 자손(descendants) 노드들(children, grandchildren, 기타)을 선택
following XML document 에서 현재 노드의 닫는 태그(closing tag) 이후에 등장하는 모든 노드를 선택
following-sibling 현재 노드 이후에 위치하는 형제 노드를 모두 선택
namespace 현재 노드의 모든 네임스페이스 노드를 선택
parent 현재 노드의 부모를 선택
preceding XML document 에서 현재 노드 이전에 등장하는 모든 노드를 선택 (조상 노드, 속성 노드, 네임스페이스 노드는 제외)
preceding-sibling 현재 노드 이전에 위치하는 형제 노드를 모두 선택
self 현재 노드를 선택

1.4.3 Location Path Expression

위치 경로(location path) 는 절대 경로이거나 상대 경로 일 수 있다.

절대 위치 경로는 슬래시(/) 로 시작하며, 상대 경로는 그렇지 않다. 두 경우 모두 위치 경로(location path)는 하나 이상의 단계로 구성되며, 각 단계는 슬래시(/)로 구분된다.


절대 위치 경로:

/step/step/…

상대 위치 경로:
step/step/…

http://fortune94.tistory.com/179 참고

https://www.w3schools.com/xml/xpath_syntax.asp 참고

http://tcpschool.com/xml/xml_xpath_pathExpression 참고 해서 한다.

https://qiita.com/rllllho/items/cb1187cec0fb17fc650a 예제들

그리고, 유튜브 강좌에서 XPath 말고, 아직 안적은 모든 것들을 다시 문서로 작성한다.

이 모든것들을 일단 끝내고 개발에 들어간다.

1.4.4 The XML Example Document

아래의 예제에서 다음 XML document 를 사용한다.

1.5 XPath 사용 예제

<?xml version="1.0" encoding="utf-8"?>
<wikimedia>
 <projects>
   <project name="Wikipedia" launch="2001-01-05">
     <editions>
       <edition language="English">en.wikipedia.org</edition>
       <edition language="German">de.wikipedia.org</edition>
       <edition language="French">fr.wikipedia.org</edition>
       <edition language="Polish">pl.wikipedia.org</edition>
     </editions>
   </project>
   <project name="Wiktionary" launch="2002-12-12">
     <editions>
       <edition language="English">en.wiktionary.org</edition>
       <edition language="French">fr.wiktionary.org</edition>
       <edition language="Vietnamese">vi.wiktionary.org</edition>
       <edition language="Turkish">tr.wiktionary.org</edition>
     </editions>
   </project>
 </projects>
</wikimedia>

위와 같은, XML 이 있을 경우,

XPATH 식 선택 부분
/wikimedia/projects/project/@name 모든 project 요소의 name 속성을 선택
/wikimedia/projects/project/editions/edition[@language="English"]/text() 모든 영문 Wikimedia 프로젝트의 주소(language 속성이 English인 모든 edition 요소의 문자열)를 선택
/wikimedia/projects/project[@name="Wikipedia"]/editions/edition/text() 모든 위키백과의 주소(Wikipedia의 이름 특성을 가진 project 요소 아래에 존재하는 모든 edition 요소의 문자열)를 선택

1.6 XPath의 데이터 모델

① XML 문서는 트리 구조의 Node들로 구성된다.

② XML 문서의 트리 구조는 단지 개념적 구조이다.

③ XPath는 XML 문서를 트리 구조로 모델링 하여 처리한다.

④ XML 문서의 계층구조, 즉 트리 구조에서 Node들을 식별, 선택, 조작하기 위해 XPath 표현 식을 사용한다.

1.7 XPath 용어

XPath에서 사용되는 용어는 다음과 같다.

용어 설명
Nodes
Atomic values 부모와 자식이 없는 단일 Node들의 값을 말한다. 예) <TITLE>XPath</TITLE>, <TYPE>A</TYPE>
Items 단일 값들(Atomic values)과 Node들을 말합니다. 예) <TITLE>, A
Node들 간의 관계 XPath의 각 Node들은 다음의 관계를 가진다. n Parent: 각각의 ElementNode 와 속성은 하나의 부모를 가진다. n Children: ElementNode는 0개 이상의 자식요소를 가진다. n Siblings: 같은 부모를 가지는 Node들을 말한다. n Ancestors: Node들의 부모 혹은 부모의 부모를 말한다. n Descendants: Node들의 자식 혹은 자식의 자식을 말한다.

1.8 XPath 표현식

1.8.1 Expression

XPath는 XML에서 특정 Node나 Node들을 선택하고자 할 때 파일경로와 비슷한 경로 식(Path Expression)을 사용한다.

파일경로와 마찬가지로 절대경로와 상대경로 또한 가진다.

예) /:루트 Node, @:Node의 속성

1.8.2 Functions

XPath는 100여개 이상의 내장함수를 포함하고 있다. 이러한 함수들은 문자열, 숫자, 날짜와 시간비교, 시퀀스 생성, 논리값 등 매우 다양하다.

예) fn:node-name(node): Node명 리턴, fn:abs(num): 절대값 리턴, fn:concat(string,string,...)

Comments