[{"data":1,"prerenderedAt":1060},["ShallowReactive",2],{"guide-xpath-selectors-guide":3},{"id":4,"title":5,"body":6,"date":1052,"description":1053,"extension":1054,"meta":1055,"navigation":545,"path":1056,"readingTime":164,"seo":1057,"stem":1058,"__hash__":1059},"guides\u002Fguides\u002Fxpath-selectors-guide.md","XPath Selectors for XML Guide",{"type":7,"value":8,"toc":1032},"minimark",[9,13,18,30,116,121,231,235,242,246,290,294,336,340,382,386,389,520,524,571,575,578,728,732,739,829,833,914,918,921,975,979,1015,1019,1028],[10,11,12],"p",{},"XPath is the query language for navigating XML and HTML documents. It provides a concise syntax for selecting nodes, filtering by conditions, and extracting values — capabilities that make it indispensable for XML processing, web scraping, test automation, and XSLT transformations. This guide covers the core XPath syntax with practical examples you can apply immediately.",[14,15,17],"h2",{"id":16},"core-path-expressions","Core Path Expressions",[10,19,20,21,25,26,29],{},"XPath uses path notation similar to filesystem paths, with ",[22,23,24],"code",{},"\u002F"," separating steps from the root and ",[22,27,28],{},"\u002F\u002F"," selecting anywhere in the document.",[31,32,33,46],"table",{},[34,35,36],"thead",{},[37,38,39,43],"tr",{},[40,41,42],"th",{},"Expression",[40,44,45],{},"Meaning",[47,48,49,63,79,91,106],"tbody",{},[37,50,51,57],{},[52,53,54],"td",{},[22,55,56],{},"\u002Fcatalog",[52,58,59,60],{},"Root element ",[22,61,62],{},"catalog",[37,64,65,70],{},[52,66,67],{},[22,68,69],{},"\u002Fcatalog\u002Fbook",[52,71,72,73,76,77],{},"All ",[22,74,75],{},"book"," children of ",[22,78,62],{},[37,80,81,86],{},[52,82,83],{},[22,84,85],{},"\u002F\u002Fbook",[52,87,72,88,90],{},[22,89,75],{}," elements anywhere in the document",[37,92,93,98],{},[52,94,95],{},[22,96,97],{},"\u002Fcatalog\u002Fbook\u002Ftitle",[52,99,72,100,103,104],{},[22,101,102],{},"title"," elements under ",[22,105,75],{},[37,107,108,113],{},[52,109,110],{},[22,111,112],{},"\u002F\u002Fbook\u002Ftitle\u002Ftext()",[52,114,115],{},"Text content of all book titles",[117,118,120],"h3",{"id":119},"example-document","Example Document",[122,123,128],"pre",{"className":124,"code":125,"language":126,"meta":127,"style":127},"language-xml shiki shiki-themes github-light github-dark","\u003Ccatalog>\n  \u003Cbook id=\"1\" category=\"programming\">\n    \u003Ctitle>XML Basics\u003C\u002Ftitle>\n    \u003Cauthor>Jane Doe\u003C\u002Fauthor>\n    \u003Cprice>29.99\u003C\u002Fprice>\n  \u003C\u002Fbook>\n  \u003Cbook id=\"2\" category=\"design\">\n    \u003Ctitle>Design Patterns\u003C\u002Ftitle>\n    \u003Cauthor>John Smith\u003C\u002Fauthor>\n    \u003Cprice>39.99\u003C\u002Fprice>\n  \u003C\u002Fbook>\n  \u003Cbook id=\"3\" category=\"programming\">\n    \u003Ctitle>Advanced XPath\u003C\u002Ftitle>\n    \u003Cauthor>Jane Doe\u003C\u002Fauthor>\n    \u003Cprice>34.99\u003C\u002Fprice>\n  \u003C\u002Fbook>\n\u003C\u002Fcatalog>\n","xml","",[22,129,130,138,144,150,156,162,168,174,180,186,192,197,203,209,214,220,225],{"__ignoreMap":127},[131,132,135],"span",{"class":133,"line":134},"line",1,[131,136,137],{},"\u003Ccatalog>\n",[131,139,141],{"class":133,"line":140},2,[131,142,143],{},"  \u003Cbook id=\"1\" category=\"programming\">\n",[131,145,147],{"class":133,"line":146},3,[131,148,149],{},"    \u003Ctitle>XML Basics\u003C\u002Ftitle>\n",[131,151,153],{"class":133,"line":152},4,[131,154,155],{},"    \u003Cauthor>Jane Doe\u003C\u002Fauthor>\n",[131,157,159],{"class":133,"line":158},5,[131,160,161],{},"    \u003Cprice>29.99\u003C\u002Fprice>\n",[131,163,165],{"class":133,"line":164},6,[131,166,167],{},"  \u003C\u002Fbook>\n",[131,169,171],{"class":133,"line":170},7,[131,172,173],{},"  \u003Cbook id=\"2\" category=\"design\">\n",[131,175,177],{"class":133,"line":176},8,[131,178,179],{},"    \u003Ctitle>Design Patterns\u003C\u002Ftitle>\n",[131,181,183],{"class":133,"line":182},9,[131,184,185],{},"    \u003Cauthor>John Smith\u003C\u002Fauthor>\n",[131,187,189],{"class":133,"line":188},10,[131,190,191],{},"    \u003Cprice>39.99\u003C\u002Fprice>\n",[131,193,195],{"class":133,"line":194},11,[131,196,167],{},[131,198,200],{"class":133,"line":199},12,[131,201,202],{},"  \u003Cbook id=\"3\" category=\"programming\">\n",[131,204,206],{"class":133,"line":205},13,[131,207,208],{},"    \u003Ctitle>Advanced XPath\u003C\u002Ftitle>\n",[131,210,212],{"class":133,"line":211},14,[131,213,155],{},[131,215,217],{"class":133,"line":216},15,[131,218,219],{},"    \u003Cprice>34.99\u003C\u002Fprice>\n",[131,221,223],{"class":133,"line":222},16,[131,224,167],{},[131,226,228],{"class":133,"line":227},17,[131,229,230],{},"\u003C\u002Fcatalog>\n",[14,232,234],{"id":233},"predicates-filtering-nodes","Predicates — Filtering Nodes",[10,236,237,238,241],{},"Predicates are enclosed in square brackets ",[22,239,240],{},"[]"," and filter nodes by position, attribute, or child content.",[117,243,245],{"id":244},"by-position","By Position",[31,247,248,258],{},[34,249,250],{},[37,251,252,255],{},[40,253,254],{},"XPath",[40,256,257],{},"Result",[47,259,260,270,280],{},[37,261,262,267],{},[52,263,264],{},[22,265,266],{},"\u002Fcatalog\u002Fbook[1]",[52,268,269],{},"First book",[37,271,272,277],{},[52,273,274],{},[22,275,276],{},"\u002Fcatalog\u002Fbook[last()]",[52,278,279],{},"Last book",[37,281,282,287],{},[52,283,284],{},[22,285,286],{},"\u002Fcatalog\u002Fbook[position() \u003C 3]",[52,288,289],{},"First two books",[117,291,293],{"id":292},"by-attribute","By Attribute",[31,295,296,304],{},[34,297,298],{},[37,299,300,302],{},[40,301,254],{},[40,303,257],{},[47,305,306,316,326],{},[37,307,308,313],{},[52,309,310],{},[22,311,312],{},"\u002F\u002Fbook[@id='2']",[52,314,315],{},"Book with id=\"2\"",[37,317,318,323],{},[52,319,320],{},[22,321,322],{},"\u002F\u002Fbook[@category='programming']",[52,324,325],{},"All programming books",[37,327,328,333],{},[52,329,330],{},[22,331,332],{},"\u002F\u002Fbook[@id]",[52,334,335],{},"All books that have an id attribute",[117,337,339],{"id":338},"by-child-content","By Child Content",[31,341,342,350],{},[34,343,344],{},[37,345,346,348],{},[40,347,254],{},[40,349,257],{},[47,351,352,362,372],{},[37,353,354,359],{},[52,355,356],{},[22,357,358],{},"\u002F\u002Fbook[author='Jane Doe']",[52,360,361],{},"Books by Jane Doe",[37,363,364,369],{},[52,365,366],{},[22,367,368],{},"\u002F\u002Fbook[price > 30]",[52,370,371],{},"Books priced above 30",[37,373,374,379],{},[52,375,376],{},[22,377,378],{},"\u002F\u002Fbook[contains(title, 'XML')]",[52,380,381],{},"Books with \"XML\" in title",[14,383,385],{"id":384},"axes-navigating-relationships","Axes — Navigating Relationships",[10,387,388],{},"Axes specify the direction of navigation relative to the context node.",[31,390,391,404],{},[34,392,393],{},[37,394,395,398,401],{},[40,396,397],{},"Axis",[40,399,400],{},"Direction",[40,402,403],{},"Example",[47,405,406,424,442,457,472,487,502],{},[37,407,408,413,416],{},[52,409,410],{},[22,411,412],{},"child",[52,414,415],{},"Children (default)",[52,417,418,421,422],{},[22,419,420],{},"child::book"," ≡ ",[22,423,75],{},[37,425,426,431,434],{},[52,427,428],{},[22,429,430],{},"parent",[52,432,433],{},"Parent node",[52,435,436,421,439],{},[22,437,438],{},"parent::*",[22,440,441],{},"..",[37,443,444,449,452],{},[52,445,446],{},[22,447,448],{},"ancestor",[52,450,451],{},"All ancestors up to root",[52,453,454],{},[22,455,456],{},"ancestor::catalog",[37,458,459,464,467],{},[52,460,461],{},[22,462,463],{},"descendant",[52,465,466],{},"All descendants at any depth",[52,468,469],{},[22,470,471],{},"descendant::title",[37,473,474,479,482],{},[52,475,476],{},[22,477,478],{},"following-sibling",[52,480,481],{},"Subsequent siblings",[52,483,484],{},[22,485,486],{},"title\u002Ffollowing-sibling::*",[37,488,489,494,497],{},[52,490,491],{},[22,492,493],{},"preceding-sibling",[52,495,496],{},"Previous siblings",[52,498,499],{},[22,500,501],{},"price\u002Fpreceding-sibling::*",[37,503,504,509,512],{},[52,505,506],{},[22,507,508],{},"self",[52,510,511],{},"The context node itself",[52,513,514,421,517],{},[22,515,516],{},"self::book",[22,518,519],{},".",[117,521,523],{"id":522},"practical-axis-examples","Practical Axis Examples",[122,525,529],{"className":526,"code":527,"language":528,"meta":127,"style":127},"language-xpath shiki shiki-themes github-light github-dark","\u003C!-- All titles after the first book's title in document order -->\n\u002Fcatalog\u002Fbook[1]\u002Ftitle\u002Ffollowing-sibling::*\n\n\u003C!-- Price of the book titled \"XML Basics\" -->\n\u002F\u002Fbook[title='XML Basics']\u002Fprice\n\n\u003C!-- All books that share an author with the first book -->\n\u002F\u002Fbook[author = \u002Fcatalog\u002Fbook[1]\u002Fauthor]\n","xpath",[22,530,531,536,541,547,552,557,561,566],{"__ignoreMap":127},[131,532,533],{"class":133,"line":134},[131,534,535],{},"\u003C!-- All titles after the first book's title in document order -->\n",[131,537,538],{"class":133,"line":140},[131,539,540],{},"\u002Fcatalog\u002Fbook[1]\u002Ftitle\u002Ffollowing-sibling::*\n",[131,542,543],{"class":133,"line":146},[131,544,546],{"emptyLinePlaceholder":545},true,"\n",[131,548,549],{"class":133,"line":152},[131,550,551],{},"\u003C!-- Price of the book titled \"XML Basics\" -->\n",[131,553,554],{"class":133,"line":158},[131,555,556],{},"\u002F\u002Fbook[title='XML Basics']\u002Fprice\n",[131,558,559],{"class":133,"line":164},[131,560,546],{"emptyLinePlaceholder":545},[131,562,563],{"class":133,"line":170},[131,564,565],{},"\u003C!-- All books that share an author with the first book -->\n",[131,567,568],{"class":133,"line":176},[131,569,570],{},"\u002F\u002Fbook[author = \u002Fcatalog\u002Fbook[1]\u002Fauthor]\n",[14,572,574],{"id":573},"functions-xpath-provides","Functions XPath Provides",[10,576,577],{},"XPath includes built-in functions for string manipulation, numeric operations, and boolean logic.",[31,579,580,592],{},[34,581,582],{},[37,583,584,587,590],{},[40,585,586],{},"Function",[40,588,589],{},"Purpose",[40,591,403],{},[47,593,594,609,623,638,653,668,683,698,713],{},[37,595,596,601,604],{},[52,597,598],{},[22,599,600],{},"text()",[52,602,603],{},"Select text nodes",[52,605,606],{},[22,607,608],{},"\u002F\u002Ftitle\u002Ftext()",[37,610,611,616,619],{},[52,612,613],{},[22,614,615],{},"contains()",[52,617,618],{},"Substring check",[52,620,621],{},[22,622,378],{},[37,624,625,630,633],{},[52,626,627],{},[22,628,629],{},"starts-with()",[52,631,632],{},"Prefix check",[52,634,635],{},[22,636,637],{},"\u002F\u002Fbook[starts-with(@id, '1')]",[37,639,640,645,648],{},[52,641,642],{},[22,643,644],{},"string-length()",[52,646,647],{},"Character count",[52,649,650],{},[22,651,652],{},"\u002F\u002Ftitle[string-length() > 10]",[37,654,655,660,663],{},[52,656,657],{},[22,658,659],{},"normalize-space()",[52,661,662],{},"Trim whitespace",[52,664,665],{},[22,666,667],{},"normalize-space(title)",[37,669,670,675,678],{},[52,671,672],{},[22,673,674],{},"number()",[52,676,677],{},"Convert to number",[52,679,680],{},[22,681,682],{},"\u002F\u002Fbook[number(price) > 30]",[37,684,685,690,693],{},[52,686,687],{},[22,688,689],{},"count()",[52,691,692],{},"Count node set",[52,694,695],{},[22,696,697],{},"count(\u002F\u002Fbook)",[37,699,700,705,708],{},[52,701,702],{},[22,703,704],{},"concat()",[52,706,707],{},"Join strings",[52,709,710],{},[22,711,712],{},"concat(title, ' by ', author)",[37,714,715,720,723],{},[52,716,717],{},[22,718,719],{},"name()",[52,721,722],{},"Node name",[52,724,725],{},[22,726,727],{},"name(\u002F\u002Fbook\u002F*)",[14,729,731],{"id":730},"xpath-for-html-and-web-scraping","XPath for HTML and Web Scraping",[10,733,734,735,738],{},"Modern browsers support XPath on HTML via ",[22,736,737],{},"document.evaluate()",". This is particularly useful for web scraping and test automation when CSS selectors are insufficient.",[122,740,744],{"className":741,"code":742,"language":743,"meta":127,"style":127},"language-javascript shiki shiki-themes github-light github-dark","\u002F\u002F Select the first \u003Ch2> inside an element with class \"article\"\nconst result = document.evaluate(\n  '\u002F\u002Fdiv[@class=\"article\"]\u002F\u002Fh2[1]',\n  document,\n  null,\n  XPathResult.FIRST_ORDERED_NODE_TYPE,\n  null\n);\nconst heading = result.singleNodeValue;\n","javascript",[22,745,746,752,776,785,790,797,807,812,817],{"__ignoreMap":127},[131,747,748],{"class":133,"line":134},[131,749,751],{"class":750},"sJ8bj","\u002F\u002F Select the first \u003Ch2> inside an element with class \"article\"\n",[131,753,754,758,762,765,769,773],{"class":133,"line":140},[131,755,757],{"class":756},"szBVR","const",[131,759,761],{"class":760},"sj4cs"," result",[131,763,764],{"class":756}," =",[131,766,768],{"class":767},"sVt8B"," document.",[131,770,772],{"class":771},"sScJk","evaluate",[131,774,775],{"class":767},"(\n",[131,777,778,782],{"class":133,"line":146},[131,779,781],{"class":780},"sZZnC","  '\u002F\u002Fdiv[@class=\"article\"]\u002F\u002Fh2[1]'",[131,783,784],{"class":767},",\n",[131,786,787],{"class":133,"line":152},[131,788,789],{"class":767},"  document,\n",[131,791,792,795],{"class":133,"line":158},[131,793,794],{"class":760},"  null",[131,796,784],{"class":767},[131,798,799,802,805],{"class":133,"line":164},[131,800,801],{"class":767},"  XPathResult.",[131,803,804],{"class":760},"FIRST_ORDERED_NODE_TYPE",[131,806,784],{"class":767},[131,808,809],{"class":133,"line":170},[131,810,811],{"class":760},"  null\n",[131,813,814],{"class":133,"line":176},[131,815,816],{"class":767},");\n",[131,818,819,821,824,826],{"class":133,"line":182},[131,820,757],{"class":756},[131,822,823],{"class":760}," heading",[131,825,764],{"class":756},[131,827,828],{"class":767}," result.singleNodeValue;\n",[117,830,832],{"id":831},"when-xpath-beats-css-selectors","When XPath Beats CSS Selectors",[31,834,835,847],{},[34,836,837],{},[37,838,839,842,845],{},[40,840,841],{},"Task",[40,843,844],{},"CSS Selector",[40,846,254],{},[47,848,849,862,874,889,901],{},[37,850,851,854,857],{},[52,852,853],{},"Select by text content",[52,855,856],{},"Not possible",[52,858,859],{},[22,860,861],{},"\u002F\u002Fa[contains(., 'Login')]",[37,863,864,867,869],{},[52,865,866],{},"Select parent from child",[52,868,856],{},[52,870,871],{},[22,872,873],{},"\u002F\u002Fa[@href=\"\u002Flogin\"]\u002F..",[37,875,876,879,884],{},[52,877,878],{},"Select by partial attribute",[52,880,881],{},[22,882,883],{},"[attr*=\"val\"]",[52,885,886],{},[22,887,888],{},"[contains(@attr, 'val')]",[37,890,891,894,896],{},[52,892,893],{},"Select previous sibling",[52,895,856],{},[52,897,898],{},[22,899,900],{},"preceding-sibling::div[1]",[37,902,903,906,909],{},[52,904,905],{},"Select nth-from-last",[52,907,908],{},"Limited support",[52,910,911],{},[22,912,913],{},"div[last()-1]",[14,915,917],{"id":916},"namespaces-and-xpath","Namespaces and XPath",[10,919,920],{},"When querying XML that uses namespaces, you must register namespace prefixes — even if the source document uses a default namespace:",[122,922,926],{"className":923,"code":924,"language":925,"meta":127,"style":127},"language-python shiki shiki-themes github-light github-dark","from lxml import etree\n\ntree = etree.parse('catalog.xml')\nns = {'bk': 'http:\u002F\u002Fexample.com\u002Fbook'}\n\n# This works:\ntitles = tree.xpath('\u002F\u002Fbk:title', namespaces=ns)\n\n# This returns nothing:\ntitles = tree.xpath('\u002F\u002Ftitle')  # matches name in no namespace\n","python",[22,927,928,933,937,942,947,951,956,961,965,970],{"__ignoreMap":127},[131,929,930],{"class":133,"line":134},[131,931,932],{},"from lxml import etree\n",[131,934,935],{"class":133,"line":140},[131,936,546],{"emptyLinePlaceholder":545},[131,938,939],{"class":133,"line":146},[131,940,941],{},"tree = etree.parse('catalog.xml')\n",[131,943,944],{"class":133,"line":152},[131,945,946],{},"ns = {'bk': 'http:\u002F\u002Fexample.com\u002Fbook'}\n",[131,948,949],{"class":133,"line":158},[131,950,546],{"emptyLinePlaceholder":545},[131,952,953],{"class":133,"line":164},[131,954,955],{},"# This works:\n",[131,957,958],{"class":133,"line":170},[131,959,960],{},"titles = tree.xpath('\u002F\u002Fbk:title', namespaces=ns)\n",[131,962,963],{"class":133,"line":176},[131,964,546],{"emptyLinePlaceholder":545},[131,966,967],{"class":133,"line":182},[131,968,969],{},"# This returns nothing:\n",[131,971,972],{"class":133,"line":188},[131,973,974],{},"titles = tree.xpath('\u002F\u002Ftitle')  # matches name in no namespace\n",[14,976,978],{"id":977},"key-takeaways","Key Takeaways",[980,981,982,991,997,1006,1009,1012],"ul",{},[983,984,985,987,988,990],"li",{},[22,986,24],{}," navigates from root; ",[22,989,28],{}," selects anywhere in the document hierarchy.",[983,992,993,994,996],{},"Predicates in ",[22,995,240],{}," filter by position, attribute values, or child element content.",[983,998,999,1000,1002,1003,1005],{},"Axes like ",[22,1001,478],{}," and ",[22,1004,430],{}," enable navigation CSS selectors cannot do.",[983,1007,1008],{},"Built-in functions handle string matching, numeric comparison, and node counting.",[983,1010,1011],{},"For namespace-aware XML, always register prefixes and use them in your XPath expressions.",[983,1013,1014],{},"XPath excels over CSS selectors for selecting by text content, parent traversal, and sibling navigation.",[14,1016,1018],{"id":1017},"try-it-yourself","Try It Yourself",[10,1020,1021,1022,1027],{},"Working with XML and need to inspect document structure? Paste your XML into the ",[1023,1024,1026],"a",{"href":1025},"\u002Ftools\u002Fxml-formatter","XML Formatter"," to get clean, indented output that makes it easy to identify the correct XPath expressions for your queries.",[1029,1030,1031],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":127,"searchDepth":140,"depth":140,"links":1033},[1034,1037,1042,1045,1046,1049,1050,1051],{"id":16,"depth":140,"text":17,"children":1035},[1036],{"id":119,"depth":146,"text":120},{"id":233,"depth":140,"text":234,"children":1038},[1039,1040,1041],{"id":244,"depth":146,"text":245},{"id":292,"depth":146,"text":293},{"id":338,"depth":146,"text":339},{"id":384,"depth":140,"text":385,"children":1043},[1044],{"id":522,"depth":146,"text":523},{"id":573,"depth":140,"text":574},{"id":730,"depth":140,"text":731,"children":1047},[1048],{"id":831,"depth":146,"text":832},{"id":916,"depth":140,"text":917},{"id":977,"depth":140,"text":978},{"id":1017,"depth":140,"text":1018},"2026-05-28","Navigate and query XML documents using XPath expressions with practical examples.","md",{"immutable":545},"\u002Fguides\u002Fxpath-selectors-guide",{"title":5,"description":1053},"guides\u002Fxpath-selectors-guide","-bqE4PgNA60lj9IsHp2ruevnx-sprbIH3fVKPl6ZWbo",1780401338487]