{"id":495,"date":"2015-11-11T19:49:39","date_gmt":"2015-11-11T18:49:39","guid":{"rendered":"http:\/\/xml-buddy.com\/xmltribune\/?p=495"},"modified":"2015-11-11T19:50:10","modified_gmt":"2015-11-11T18:50:10","slug":"use-xpath-regular-expressions-to-search-in-xml-data","status":"publish","type":"post","link":"https:\/\/www.xml-buddy.com\/xmltribune\/use-xpath-regular-expressions-to-search-in-xml-data\/","title":{"rendered":"Use XPath regular expressions to search in XML data"},"content":{"rendered":"<p>The XML <a href=\"http:\/\/www.xml-buddy.com\/ValidatorBuddy.htm\" >ValidatorBuddy<\/a> editor provides a convenient\u00a0search and replace window where you can quickly find any text in your XML or JSON data. But sometimes you want to have more control on where you are actually searching for a special string. Maybe you want to search only in XML attribute values or you want to get all elements with a special name. You can use the XPath evaluator window in the editor for those tasks.<\/p>\n<p>For example we could use the following regular expression to find any match of &#8220;Oak&#8221; if it appears as a separate word in any text content of an element:<\/p>\n<p><div id=\"attachment_499\" style=\"width: 479px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_oak_word.png\"><img aria-describedby=\"caption-attachment-499\" loading=\"lazy\" class=\"size-full wp-image-499\" src=\"http:\/\/xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_oak_word.png\" alt=\"XPath to find Oak as word\" width=\"469\" height=\"222\" srcset=\"https:\/\/www.xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_oak_word.png 469w, https:\/\/www.xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_oak_word-300x142.png 300w\" sizes=\"(max-width: 469px) 100vw, 469px\" \/><\/a><p id=\"caption-attachment-499\" class=\"wp-caption-text\">XPath:\u00a0\/\/*[text()[matches(., &#8216;(^|[\\t\\n ])Oak($|[\\t\\n ])&#8217;)]]<\/p><\/div>You can also look for all elements where the tag ends with the string &#8220;name&#8221; and do the comparison case-insensitive:<\/p>\n<p><div id=\"attachment_500\" style=\"width: 432px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_name_element.png\"><img aria-describedby=\"caption-attachment-500\" loading=\"lazy\" class=\"size-full wp-image-500\" src=\"http:\/\/xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_name_element.png\" alt=\"xpath find name element\" width=\"422\" height=\"222\" srcset=\"https:\/\/www.xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_name_element.png 422w, https:\/\/www.xml-buddy.com\/xmltribune\/wp-content\/uploads\/2015\/11\/xpath_find_name_element-300x158.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/a><p id=\"caption-attachment-500\" class=\"wp-caption-text\">XPath: \/\/*[local-name()[matches(., &#8216;name$&#8217;, &#8216;i&#8217;)]]<\/p><\/div>All details about using regular expressions in XPath statements are available on this w3.org page: <a href=\"http:\/\/www.w3.org\/TR\/xpath-functions\/#string.match\" target=\"_blank\">String functions that use pattern matching<\/a><\/p>\n<p style=\"opacity:0.5;padding:0;margin:0;display:inline;\"><sub><a href=\"#\" onclick=\"window.open('https:\/\/www.janhvizdak.com\/rdr.me.1'); return false;\" target=\"_blank\" style=\"cursor:help;\"><b>&#187;crosslinked&#171;<\/b><\/a><\/sub><\/p>","protected":false},"excerpt":{"rendered":"<p>The XML ValidatorBuddy editor provides a convenient\u00a0search and replace window where you can quickly find any text in your XML or JSON data. But sometimes you want to have more control on where you are actually searching for a special string. Maybe you want to search only in XML attribute values or you want to get all elements with a&#8230; <a href=\"https:\/\/www.xml-buddy.com\/xmltribune\/use-xpath-regular-expressions-to-search-in-xml-data\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6],"tags":[80,58],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/posts\/495"}],"collection":[{"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":4,"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/posts\/495\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xml-buddy.com\/xmltribune\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}