Presentation is loading. Please wait.

Presentation is loading. Please wait.

XPATH מבוא MCSD Doron Amir www.doronamir.com. What is XPath? A W3C Standard A W3C Standard Not written in XML Not written in XML Defines parts of an XML.

Similar presentations


Presentation on theme: "XPATH מבוא MCSD Doron Amir www.doronamir.com. What is XPath? A W3C Standard A W3C Standard Not written in XML Not written in XML Defines parts of an XML."— Presentation transcript:

1 XPATH מבוא MCSD Doron Amir www.doronamir.com

2 What is XPath? A W3C Standard A W3C Standard Not written in XML Not written in XML Defines parts of an XML document Defines parts of an XML document Defines a standard functions Defines a standard functions Major element in XSLT Major element in XSLT לפי כללי ה w3c ה XPATH אינה XML ה XPATH מגדירה חלקים ממסמך ה XML ה XPATH מגדירה פונקציות שימושיות לטיפול במסמך XML ה XPATH הינה חלק עקרי מ XSLT

3 XPath Expression XPATH מכילה ביטויים עבר זיהוי צמתים במסמך XPATH מכילה ביטויים עבר זיהוי צמתים במסמך /catalog/cd/price Root Element בחירת כל האלמנטים Price שבתוך האלמנטים CD שבתוך אלמנט השורש CATALOG The CD Element The Price Element Empire Burlesque Bob Dylan 10.90

4 XPath Expression /catalog/cd/price = Absolute path /catalog/cd/price = Absolute path //cd = All the CD elemen ts in the document: //cd = All the CD elemen ts in the document: Selecting Unknown Elements Select All the child elements of all the cd /catalog/cd/*

5 XPath Expression /catalog/cd/* /catalog/cd/* All the child elements of all the cd elements All the child elements of all the cd elements /catalog/*/price /catalog/*/price All the price elements that are grandchild elements of the catalog All the price elements that are grandchild elements of the catalog /*/*/price /*/*/price All price elements which have 2 ancestors All price elements which have 2 ancestors //* //* All elements in the document All elements in the document Parent child element grandchild elements /*/*/price /catalog/*/price /catalog/cd/*

6 Selecting Branches /catalog/cd[1] /catalog/cd[1] first cd child element first cd child element /catalog/cd[last()] /catalog/cd[last()] last cd child element last cd child element /catalog/cd[price] /catalog/cd[price] element that have a price element element that have a price element /catalog/cd[price=10.90] /catalog/cd[price=10.90] all the cd elements that have a price element with a value of 10.90: all the cd elements that have a price element with a value of 10.90: /catalog/cd[price=10.90]/price /catalog/cd[price=10.90]/price all the Price elements that have a price element with a value of 10.90 all the Price elements that have a price element with a value of 10.90

7 Several Paths /catalog/cd/title | /catalog/cd/artist /catalog/cd/title | /catalog/cd/artist all the title and artist elements of the cd element of the catalog element //title | //artist //title | //artist all the title and artist elements in the document: //title | //artist | //price //title | //artist | //price all the title, artist and price elements in the document: /catalog/cd/title | //artist /catalog/cd/title | //artist all the title elements of the cd element and all the artist elements in the document

8 Attributes Specified by the @ prefix //@country //@country all attributes named country //cd[@country] //cd[@country] all cd elements which have an attribute named country: //cd[@*] //cd[@*] all cd elements which have any attribute: //cd[@country='UK'] //cd[@country='UK'] all cd elements which have an attribute named country with a value of 'UK':

9 Location child::text() Selects the text node children Selects all the children of the current nodechild::node() Selects the X attribute of the current node attribute::X attribute::* Selects all attributes of the current node

10 child::price[price=9.90] child::price[price=9.90] child::cd[position()=1] child::cd[position()=1] child::cd[position()=last()] child::cd[position()=last()] child::cd[position()<6] child::cd[position()<6] /descendant::cd[position()=7] /descendant::cd[position()=7] child::cd[attribute::type="classic"] child::cd[attribute::type="classic"] Selects all cd children of the current node that have a type attribute with value classic Selects the seventh cd element in the document Selects the first five cd children of the current node Selects the last cd child 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

11 short cd[@type="classic"] child::cd[attribute::type="classic"] parent::node()/child::cd../cd

12 XPath Expression ResultExample Selects all the cd elements that are children of the current nodecd Selects all child elements of the current node* Selects all text node children of the current nodetext() Selects the src attribute of the current node@src Selects all the attributes of the current node@* Selects the first cd child of the current nodecd[1] Selects the last cd child of the current nodecd[last()]

13 XPath Expression Selects all cd grandchildren of the current node*/cd Selects the first para of the third chapter of the book/book/chapter[3]/para[1] Selects all the cd descendants of the document root and thus selects all cd elements in the same document as the current node //cd Selects the current node. Selects the cd element descendants of the current node.//cd Selects the parent of the current node.. Selects the src attribute of the parent of the current node../@src Selects all cd children of the current node that have a type attribute with value classic cd[@type="classic"]

14 XPath Expression Selects all cd children of the current node that have a type attribute with value classic cd[@type="classic"] Selects the fifth cd child of the current node that has a type attribute with value classic cd[@type="classic"][5] Selects the fifth cd child of the current node if that child has a type attribute with value classic cd[5][@type="classic"] Selects all the cd children of the current node that have both a type attribute and a country attribute cd[@type and @country]

15 XPath Functions string-length('Beatles') Result: 7 string-length('Beatles') Result: 7 substring('Beatles',1,4) Result: 'Beat' substring('Beatles',1,4) Result: 'Beat' round(3.14) Result: 3 round(3.14) Result: 3 number(false()) Result: 0 number(false()) Result: 0

16 XPath Functions ceiling(3.14) Result: 4 ceiling(3.14) Result: 4 floor(3.14) Result: 3 floor(3.14) Result: 3 string(314) Result: '314' string(314) Result: '314' translate('12:30','0123','abcd') Result: 'bc:da' translate('12:30','0123','abcd') Result: 'bc:da' translate('12:30','03','54') Result: '12:45' translate('12:30','03','54') Result: '12:45'

17 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(" ") document.write(x.xml) document.write(" ") next Sylvias Mother Dr.Hook UK CBS 8.10 1973 Maggie May Rod Stewart UK Pickwick 8.50 1990

18 XPATH מבוא MCSD Doron Amir www.doronamir.com


Download ppt "XPATH מבוא MCSD Doron Amir www.doronamir.com. What is XPath? A W3C Standard A W3C Standard Not written in XML Not written in XML Defines parts of an XML."

Similar presentations


Ads by Google