====== XPath dnevnik ====== An XPath recipe list. See also [[z:biblio-xpath|Bibliographic XPath Queries]] and [[z:basex-adv|BaseX Adventures]]. ===== 1. Find attributes by ends of their value strings ===== * 2011-09-03 * XPath 2.0 * Find all attributes "ana" whose value ends with "exempla" * Expression (apply the forward slash abbreviation before ''@ana'' if necessary):
''@ana[ends-with(.,"exempla")]''
===== 2. Find a specific element and all of its descendants of the same kind ===== * 2011-09-04 * XPath 2.0 * Find the "seg" element with the xml:id attribute value "E6.9.4"; also find all "seg" descendants of this element * Expression (add double forward slash before seg as needed):
''seg[ancestor-or-self::seg[@xml:id[. eq 'E6.9.4']]]''
===== 3. Find all elements with attributes that contain a specific string ===== * 2011-09-08 * XPath 2.0 * Find all elements with attribute ana whose value contains "MARK" * Expression (add double forward slash before @ana as needed):
''@ana[contains(.,"MARK")]''
===== 4. Retrieve all elements with specific attributes preceded by siblings with specific attributes ===== * 2011-09-11 * XPath 2.0 * Find all seg elements with attribute ana whose value contains "MARK", preceded by seg elements with attribute ana whose value contains "NORMAL" * Expression (add double forward slash before seg as needed):
''seg[@ana[. eq 'MARK']][preceding-sibling::seg[@ana[. eq 'NORMAL']]]''
===== 5. Retrieve all elements with specific attributes immediately preceded by a sibling with specific attributes ===== * 2011-09-12 * XPath 2.0 * Find all ''seg'' elements with attribute ''@ana'' whose value equals "PLANA", immediately preceded by a ''seg'' element sibling with attribute ''@ana'' whose value contains "tekst" * Expression (add double forward slash before ''seg'' as needed):
''seg[@ana[. eq 'PLANA']][preceding-sibling::seg[1][@ana[contains(.,'tekst')]]]''
===== 6. Find all elements with odd / even numeric values in attributes ===== * 2011-12-2 * XPath 2.0 * Find all ''anchor'' elements with attribute ''@n'' whose value is an odd number * Expression (add double forward slash before ''seg'' as needed):
''anchor[number(@n) mod 2 = 1]''
* Useful link: stackoverflow question [[http://stackoverflow.com/questions/8032198/xslt-template-to-identify-odd-even-numbers|XSLT Template to identify odd/even numbers?]]