XPATH
מבוא
MCSD Doron Amir
www.doronamir.com
What is XPath?
A W3C Standard
Not written in XML
Defines parts of an XML document
Defines a standard functions
Major element in XSLT
w3c לפי כללי ה
XML אינהXPATH ה
XML מגדירה חלקים ממסמך הXPATH ה
XML מגדירה פונקציות שימושיות לטיפול במסמךXPATH ה
XSLT הינה חלק עקרי מXPATH ה
XPath Expression
מכילה ביטויים עבר זיהוי צמתים במסמךXPATH
CATALOG שבתוך אלמנט השורשCD שבתוך האלמנטיםPrice בחירת כל האלמנטים
“/catalog/cd/price”
Root Element
The CD Element
The Price Element
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price> </cd>
<catalog>
XPath Expression
/catalog/cd/price = Absolute path
//cd = All the CD elemen ts in the document:
Selecting Unknown Elements
/catalog/cd/*
Select All the child elements of all the cd
XPath Expression
/catalog/cd/*
All the child elements of all the cd elements
/catalog/*/price
All the price elements that are grandchild elements of the catalog
/*/*/price
All price elements which have 2 ancestors
//*
All elements in the document
Parent
/catalog/*/price
child element
/*/*/price
grandchild elements
/catalog/cd/*
Selecting Branches
/catalog/cd[1]
first cd child element
/catalog/cd[last()]
last cd child element
/catalog/cd[price]
element that have a price element
/catalog/cd[price=10.90]
all the cd elements that have a price element with a value of 10.90:
/catalog/cd[price=10.90]/price
all the Price elements that have a price element with a value of 10.90
Several Paths
/catalog/cd/title | /catalog/cd/artist
all the title and artist elements of the cd element of the catalog element
//title | //artist
all the title and artist elements in the
document:
//title | //artist | //price
all the title, artist and price elements in the
document:
/catalog/cd/title | //artist
all the title elements of the
cd element and
all the artist elements in the
document
Attributes
Specified by the @ prefix
//@country
all attributes named country
//cd[@country]
all cd elements which have an attribute named country:
//cd[@*]
all cd elements which have any attribute:
//cd[@country='UK']
all cd elements which have an attribute named country with a value of 'UK':
Location
child::text()
child::node()
attribute::X
attribute::*
Selects the text node children
Selects all the children of the current node
Selects the X attribute of the current node
Selects all attributes of the current node
child::price[price=9.90]
Selects all price elements that are children of the current node
with a price element that equals 9.90
child::cd[position()=1]
Selects the first cd child of the current node
child::cd[position()=last()]
Selects the last cd child of the current node
child::cd[position()<6]
Selects the first five cd children of the current node
/descendant::cd[position()=7]
Selects the seventh cd element in the document
child::cd[attribute::type="classic"]
Selects all cd children of the current node that have a type attribute with value classic
short
child::cd[attribute::type="classic"]
cd[@type="classic"]
parent::node()/child::cd
../cd
XPath Expression
Example
Result
cd
Selects all the cd elements that are children of the current node
*
Selects all child elements of the current node
text()
Selects all text node children of the current node
@src
Selects the src attribute of the current node
@*
Selects all the attributes of the current node
cd[1]
Selects the first cd child of the current node
cd[last()]
Selects the last cd child of the current node
XPath Expression
*/cd
Selects all cd grandchildren of the current node
/book/chapter[3]/para[1]
Selects the first para of the third chapter of the book
//cd
Selects all the cd descendants of the document root and thus
selects all cd elements in the same document as the current
node
.
Selects the current node
.//cd
Selects the cd element descendants of the current node
..
Selects the parent of the current node
../@src
Selects the src attribute of the parent of the current node
cd[@type="classic"]
Selects all cd children of the current node that have a type
attribute with value classic
XPath Expression
cd[@type="classic"]
Selects all cd children of the current node that have
a type attribute with value classic
cd[@type="classic"][5]
Selects the fifth cd child of the current node that has
a type attribute with value classic
cd[5][@type="classic"]
Selects the fifth cd child of the current node
if that child has a type attribute with value classic
cd[@type and @country]
Selects all the cd children of the current node
that have both a type attribute and a country attribute
XPath Functions
string-length('Beatles')
Result: 7
substring('Beatles',1,4)
Result: 'Beat'
round(3.14)
Result: 3
number(false())
Result: 0
XPath Functions
ceiling(3.14)
Result: 4
floor(3.14)
Result: 3
string(314)
Result: '314'
translate('12:30','0123','abcd')
Result: 'bc:da'
translate('12:30','03','54')
Result: '12:45'
<html>
<body>
<script type="text/vbscript">
Example
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("cdcatalog.xml")
path="//cd[price<9.90]"
set nodes=xmlDoc.selectNodes(path)
for each x in nodes
document.write("<xmp>")
document.write(x.xml)
document.write("</xmp>")
next
</script>
</body>
</html>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</countr
<company>Pickwick</comp
any>
<price>8.50</price>
<year>1990</year>
</cd>
XPATH
מבוא
MCSD Doron Amir
www.doronamir.com
© Copyright 2025 Paperzz