{"id":375,"date":"2017-11-26T07:22:22","date_gmt":"2017-11-26T07:22:22","guid":{"rendered":"http:\/\/www.josejavierfm.es\/blog\/?p=375"},"modified":"2017-11-26T07:22:22","modified_gmt":"2017-11-26T07:22:22","slug":"xpath","status":"publish","type":"post","link":"https:\/\/www.josejavierfm.es\/blog\/2017\/11\/26\/xpath\/","title":{"rendered":"xpath"},"content":{"rendered":"<p>Partimos del siguiente xml:<\/p>\n<pre>\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n<bookstore>\r\n\r\n<book category=\"cooking\">\r\n  <title lang=\"en\">Everyday Italian<\/title>\r\n  <author>Giada De Laurentiis<\/author>\r\n  <year>2005<\/year>\r\n  <price>30.00<\/price>\r\n<\/book>\r\n\r\n<book category=\"children\">\r\n  <title lang=\"en\">Harry Potter<\/title>\r\n  <author>J K. Rowling<\/author>\r\n  <year>2005<\/year>\r\n  <price>29.99<\/price>\r\n<\/book>\r\n\r\n<book category=\"web\">\r\n  <title lang=\"en\">XQuery Kick Start<\/title>\r\n  <author>James McGovern<\/author>\r\n  <author>Per Bothner<\/author>\r\n  <author>Kurt Cagle<\/author>\r\n  <author>James Linn<\/author>\r\n  <author>Vaidyanathan Nagarajan<\/author>\r\n  <year>2003<\/year>\r\n  <price>49.99<\/price>\r\n<\/book>\r\n\r\n<book category=\"web\">\r\n  <title lang=\"en\">Learning XML<\/title>\r\n  <author>Erik T. Ray<\/author>\r\n  <year>2003<\/year>\r\n  <price>39.95<\/price>\r\n<\/book>\r\n\r\n<\/bookstore>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>y trabajamos con javascript para utilizar xpath y ver los resultados<\/p>\n<pre>\r\n<!DOCTYPE html>\r\n<html>\r\n<body>\r\n\r\n<p id=\"demo\"><\/p>\r\n<p id=\"resultado\"><\/p>\r\n\r\n<script>\r\n\r\n\r\n\r\n\r\nvar text = \"<bookstore><book category='cooking'>  <title lang='en'>Everyday Italian<\/title>  <author>Giada De Laurentiis<\/author>  <year>2005<\/year>  <price>30.00<\/price><\/book><book category='children'>  <title lang='en'>Harry Potter<\/title>  <author>J K. Rowling<\/author>  <year>2005<\/year>  <price>29.99<\/price><\/book><book category='web'>  <title lang='en'>XQuery Kick Start<\/title>  <author>James McGovern<\/author>  <author>Per Bothner<\/author>  <author>Kurt Cagle<\/author>  <author>James Linn<\/author>  <author>Vaidyanathan Nagarajan<\/author>  <year>2003<\/year>  <price>49.99<\/price><\/book><book category='web'>  <title lang='en'>Learning XML<\/title>  <author>Erik T. Ray<\/author>  <year>2003<\/year>  <price>39.95<\/price><\/book><\/bookstore>\";\r\n\r\npath = \"\/bookstore\/book\/author\";\r\nvar parser = new DOMParser();\r\nxmlDoc = parser.parseFromString(text,\"text\/xml\");\r\n showResult(xmlDoc,path);\r\n showResult(xmlDoc,\"\/\/title\");\r\n showResult(xmlDoc,\"\/\/book\/author[1]\");\r\n showResult(xmlDoc,\"\/\/book[@category=\\\"web\\\"]\/title\");\r\n\r\nfunction showResult(xml,path) {\r\n    var txt = \"\";\r\n    \r\n    if (xml.evaluate) {\r\n        var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);\r\n        var result = nodes.iterateNext();\r\n        while (result) {\r\n            txt += result.childNodes[0].nodeValue + \"<br>\";\r\n            result = nodes.iterateNext();\r\n        } \r\n    \/\/ Code For Internet Explorer\r\n    } else if (window.ActiveXObject || xhttp.responseType == \"msxml-document\") {\r\n        xml.setProperty(\"SelectionLanguage\", \"XPath\");\r\n        nodes = xml.selectNodes(path);\r\n        for (i = 0; i < nodes.length; i++) {\r\n            txt += nodes[i].childNodes[0].nodeValue + \"<br>\";\r\n        }\r\n    }\r\n    document.getElementById('resultado').innerHTML += \"<strong>\"+path+\"<\/strong><br\/>\"+txt+\"<br>\";\r\n}\r\n<\/script>\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n<p>Y vemos como resultado:<\/p>\n<pre>\r\n\/bookstore\/book\/author\r\nGiada De Laurentiis\r\nJ K. Rowling\r\nJames McGovern\r\nPer Bothner\r\nKurt Cagle\r\nJames Linn\r\nVaidyanathan Nagarajan\r\nErik T. Ray\r\n\r\n\/\/title\r\nEveryday Italian\r\nHarry Potter\r\nXQuery Kick Start\r\nLearning XML\r\n\r\n\/\/book\/author[1]\r\nGiada De Laurentiis\r\nJ K. Rowling\r\nJames McGovern\r\nErik T. Ray\r\n\r\n\/\/book[@category=\"web\"]\/title\r\nXQuery Kick Start\r\nLearning XML\r\n<\/pre>\n<p>Se puede interactuar y probar desde la url: <a href=\"https:\/\/jsfiddle.net\/g17x06L9\/\" target=\"_blank\">aqu\u00ed<\/a><\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=josejavierfm\" class=\"twitter-share-button\">Twittear<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Partimos del siguiente xml: Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95 &nbsp; y trabajamos con javascript para utilizar xpath y ver los resultados Y vemos como [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,22],"tags":[],"_links":{"self":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/375"}],"collection":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":3,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/375\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}