xpath
DESCRIPTION
XPATH. http://www.zvon.org/xxl/XPathTutorial/General/examples.html. XPath syntax is similar to file system. If the path starts with the slash / , then it represents an absolute path to the required element. /AAA ( Select the root element AAA) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/1.jpg)
XPATH
http://www.zvon.org/xxl/XPathTutorial/General/examples.html
![Page 2: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/2.jpg)
• XPath syntax is similar to file system. If the path starts with the slash / , then it represents an absolute path to the required element.
• /AAA (Select the root element AAA)•
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
![Page 3: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/3.jpg)
• /AAA/CCC• Select all elements CCC which are children of the
root element AAA•
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
![Page 4: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/4.jpg)
• /AAA/DDD/BBB• Select all elements BBB which are children of
DDD which are children of the root element AAA•
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
![Page 5: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/5.jpg)
• If the path starts with // then all elements in the document which fulfill following criteria are selected.
• //BBB• Select all elements BBB•
<AAA> <BBB/> <CCC/> <BBB/> <DDD> <BBB/> </DDD> <CCC> <DDD> <BBB/> <BBB/> </DDD> </CCC> </AAA>
![Page 6: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/6.jpg)
• //DDD/BBB• Select all elements BBB which are children of DDD•
<AAA> <BBB/> <CCC/> <BBB/> <DDD> <BBB/> </DDD> <CCC> <DDD> <BBB/> <BBB/> </DDD> </CCC> </AAA>
![Page 7: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/7.jpg)
• The star * selects all elements located by preceeding path /AAA/CCC/DDD/*
• Select all elements enclosed by elements /AAA/CCC/DDD
•<AAA>
• <XXX> <DDD> <BBB/> <BBB/> <EEE/> <FFF/> </DDD> </XXX> <CCC> <DDD> <BBB/> <BBB/> <EEE/> <FFF/> </DDD>
• </CCC> <CCC> <BBB> <BBB> <BBB/> </BBB> </BBB> </CCC> </AAA>
![Page 8: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/8.jpg)
• /*/*/*/BBB• Select all elements BBB
which have 3 ancestors•
<AAA> <XXX> <DDD> <BBB/> <BBB/> <EEE/> <FFF/> </DDD> </XXX> <CCC>
• <DDD> <BBB/> <BBB/> <EEE/> <FFF/> </DDD> </CCC> <CCC> <BBB> <BBB> <BBB/> </BBB> </BBB> </CCC> </AAA>
![Page 9: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/9.jpg)
• //*• Select all elements•
<AAA> <XXX> <DDD> <BBB/> <BBB/> <EEE/> <FFF/> </DDD> </XXX> <CCC> <DDD>
• <BBB/> <BBB/> <EEE/> <FFF/> </DDD> </CCC> <CCC> <BBB> <BBB> <BBB/> </BBB> </BBB> </CCC> </AAA>
![Page 10: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/10.jpg)
• Expression in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection. /AAA/BBB[1]
• Select the first BBB child of element AAA•
<AAA> <BBB/> <BBB/> <BBB/> <BBB/> </AAA>
![Page 11: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/11.jpg)
• /AAA/BBB[last()]• Select the last BBB child of element AAA•
<AAA> <BBB/> <BBB/> <BBB/> <BBB/> </AAA>
![Page 12: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/12.jpg)
• Attributes are specified by @ prefix. • //@id• Select all attributes @id•
<AAA> <BBB id = "b1"/> <BBB id = "b2"/> <BBB name = "bbb"/> <BBB/> </AAA>
![Page 13: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/13.jpg)
• //BBB[@id]• Select BBB elements which have attribute
id•
<AAA> <BBB id = "b1"/> <BBB id = "b2"/> <BBB name = "bbb"/> <BBB/> </AAA>
![Page 14: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/14.jpg)
• //BBB[@name]• Select BBB elements which have attribute
name•
<AAA> <BBB id = "b1"/> <BBB id = "b2"/> <BBB name = "bbb"/> <BBB/> </AAA>
![Page 15: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/15.jpg)
• //BBB[@*]• Select BBB elements which have any
attribute•
<AAA> <BBB id = "b1"/> <BBB id = "b2"/> <BBB name = "bbb"/> <BBB/> </AAA>
![Page 16: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/16.jpg)
• //BBB[not(@*)]• Select BBB elements without an attribute•
<AAA> <BBB id = "b1"/> <BBB id = "b2"/> <BBB name = "bbb"/> <BBB/> </AAA>
![Page 17: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/17.jpg)
• Values of attributes can be used as selection criteria.. //BBB[@id='b1']
• Select BBB elements which have attribute id with value b1
• <AAA> <BBB id = "b1"/> <BBB name = " bbb "/> <BBB name = "bbb"/> </AAA>
![Page 18: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/18.jpg)
• //BBB[@name='bbb']• Select BBB elements which have attribute
name with value 'bbb'•
<AAA> <BBB id = "b1"/> <BBB name = " bbb "/> <BBB name = "bbb"/> </AAA>
![Page 19: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/19.jpg)
• Normalized-space removes leading and starting spaces and replaces sequences of whitespace characters by a single space
• //BBB[normalize-space(@name)='bbb']• Select BBB elements which have attribute name
with value bbb, leading and trailing spaces are removed before comparison
• <AAA> <BBB id = "b1"/> <BBB name = " bbb "/> <BBB name = "bbb"/> </AAA>
![Page 20: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/20.jpg)
• Function count() counts the number of selected elements
• //*[count(BBB)=2]• Select elements which have two children BBB•
<AAA> <CCC> <BBB/> <BBB/> <BBB/> </CCC> <DDD> <BBB/> <BBB/> </DDD>
![Page 21: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/21.jpg)
• //*[count(*)=2]• Select elements which have 2 children•
<AAA> <CCC> <BBB/> <BBB/> <BBB/> </CCC> <DDD> <BBB/> <BBB/> </DDD> <EEE> <CCC/> <DDD/> </EEE> </AAA>
![Page 22: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/22.jpg)
• //*[count(*)=3]• Select elements which have 3 children•
<AAA> <CCC> <BBB/> <BBB/> <BBB/> </CCC> <DDD> <BBB/> <BBB/> </DDD> <EEE> <CCC/> <DDD/> </EEE> </AAA>
![Page 23: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/23.jpg)
• //*[name()='BBB']• Select all elements with name BBB, equivalent
with //BBB•
<AAA> <BCC> <BBB/> <BBB/> <BBB/> </BCC> <DDB> <BBB/> <BBB/> </DDB>
![Page 24: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/24.jpg)
• //*[starts-with(name(),'B')]• Select all elements name of which starts with letter B•
<AAA> <BCC> <BBB/> <BBB/> <BBB/> </BCC> <DDB> <BBB/> <BBB/> </DDB> <BEC> <CCC/> <DBD/> </BEC> </AAA>
![Page 25: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/25.jpg)
• //*[contains(name(),'C')]• Select all elements name of which contain letter C•
<AAA> <BCC> <BBB/> <BBB/> <BBB/> </BCC> <DDB> <BBB/> <BBB/> </DDB> <BEC> <CCC/> <DBD/> </BEC> </AAA>
![Page 26: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/26.jpg)
• //*[string-length(name()) = 3]• Select elements with three-letter name•
<AAA> <Q/> <SSSS/> <BB/> <CCC/> <DDDDDDDD/> <EEEE/> </AAA>
![Page 27: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/27.jpg)
• //*[string-length(name()) < 3]• Select elements name of which has one or two
characters•
<AAA> <Q/> <SSSS/> <BB/> <CCC/> <DDDDDDDD/> <EEEE/> </AAA>
•
![Page 28: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/28.jpg)
• //*[string-length(name()) > 3]• Select elements with name longer than three
characters•
<AAA> <Q/> <SSSS/> <BB/> <CCC/> <DDDDDDDD/> <EEEE/> </AAA>
![Page 29: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/29.jpg)
• Several paths can be combined with | separator. //CCC | //BBB
• Select all elements CCC and BBB•
<AAA> <BBB/> <CCC/> <DDD> <CCC/> </DDD> <EEE/> </AAA>
![Page 30: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/30.jpg)
• /AAA/EEE | //BBB• Select all elements BBB and elements EEE
which are children of root element AAA•
<AAA> <BBB/> <CCC/> <DDD> <CCC/> </DDD> <EEE/> </AAA>
![Page 31: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/31.jpg)
• /AAA/EEE | //DDD/CCC | /AAA | //BBB• Number of combinations is not restricted•
<AAA> <BBB/> <CCC/> <DDD> <CCC/> </DDD> <EEE/> </AAA>
![Page 32: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/32.jpg)
• The child axis contains the children of the context node. The child axis is the default axis and it can be omitted. /AAA
• Equivalent of /child::AAA•
<AAA> <BBB/> <CCC/> </AAA>
![Page 33: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/33.jpg)
• /AAA/BBB• Equivalent of /child::AAA/child::BBB•
<AAA> <BBB/> <CCC/> </AAA>
![Page 34: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/34.jpg)
• /child::AAA/BBB• Both possibilities can be combined•
<AAA> <BBB/> <CCC/> </AAA>
![Page 35: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/35.jpg)
• /descendant::*• Select all descendats of document root and
therefore all elements•
<AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB>
![Page 36: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/36.jpg)
• /AAA/BBB/descendant::*• Select all descendants of /AAA/BBB•
<AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB
![Page 37: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/37.jpg)
• //CCC/descendant::*• Select all elements which
have CCC among its ancestors
• <AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB> <CCC>
• <DDD> <EEE> <DDD> <FFF/> </DDD> </EEE> </DDD> </CCC> </AAA>
![Page 38: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/38.jpg)
• //CCC/descendant::DDD• Select elements DDD
which have CCC among its ancestors
• <AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB> <CCC>
• <DDD> <EEE> <DDD> <FFF/> </DDD> </EEE> </DDD> </CCC> </AAA>
![Page 39: XPATH](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816844550346895dde1b97/html5/thumbnails/39.jpg)
• The parent axis contains the parent of the context node, if there is one. //DDD/parent::*
• Select all parents of DDD element
• <AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB> <CCC> <DDD>
• <EEE> <DDD> <FFF/> </DDD> </EEE> </DDD> </CCC> </AAA>