180 likes | 359 Views
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 ה XPATH אינה XML ה XPATH מגדירה חלקים ממסמך ה XML
E N D
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 ה XPATH אינה XML ה XPATH מגדירה חלקים ממסמך ה XML ה XPATH מגדירה פונקציות שימושיות לטיפול במסמך XML ה XPATH הינה חלק עקרי מ XSLT
XPath Expression בחירת כל האלמנטים Price שבתוך האלמנטים CD שבתוך אלמנט השורש CATALOG • XPATH מכילה ביטויים עבר זיהוי צמתים במסמך “/catalog/cd/price” The Price Element Root Element The CD 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 andall 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 Selects the text node children child::text() child::node() Selects all the children of the current node attribute::X Selects the X attribute of the current node attribute::* Selects all attributes of the current node
Selects all price elements that are children of the current node with a price element that equals 9.90 Selects the first cd child of the current node • child::price[price=9.90] • child::cd[position()=1] • child::cd[position()=last()] • child::cd[position()<6] • /descendant::cd[position()=7] • child::cd[attribute::type="classic"] Selects the last cd child of the current node Selects the first five cd children of the current node Selects the seventh cd element in the document 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 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'
Example <html> <body> <script type="text/vbscript"> 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</company> <price>8.50</price> <year>1990</year> </cd>
XPATH מבוא MCSD Doron Amir www.doronamir.com