javascript - XHR, XPath, and custom local XML schema -


although it's possible, , common use xml extensions in (x)html5, (e.g. svgs), parsing xhtml5 document, extended custom local xsd, using xpaths causing unexpected results.

<?xml version="1.0" encoding="utf-8" ?> <!doctype html> <html xmlns:hp="http://homepage.org/homepage"        hp:schemalocation="http://homepage.org ./schemas/homepage.xsd">   <head>     ...   </head>   <body>     <hp:homepage title="homepage">     <header>       <h1></h1>     </header>     ... 

utilising usual nsresolver logic causes namespace errors:

  var hpresolver = document.creatensresolver( document.ownerdocument == null ? document.documentelement : document.ownerdocument.documentelement); 

and creating custom handlers leads no errors, xpath still doesn't return expected results:

  var hpresolver = function (prefix) {     if (prefix === 'hp') {       return 'http://homepage.org/homepage';     } else {       return null;     }   }; 

the xsd being supplied rest of files, , being tested on local file system rather on server educational exercise.

the xpath query follows:

  var path = '/html/body/hp:homepage/header/h1';   var headtitle = document.evaluate(path, document, hpresolver, xpathresult.first_ordered_node_type, null).singlenodevalue; 

the above samples have been written example.

<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"            targetnamespace="http://homepage.org/homepage"            xmlns="http://homepage.org/homepage"            elementformdefault="qualified">    <xs:element name="homepage">     <xs:complextype>       <xs:sequence>         <xs:attribute name="title" type="xs:string" use="required" />       </xs:sequence>     </xs:complextype>   </xs:element>  </xs:schema> 

i don't think schemas matter @ browsers don't have validating parsers schema support. matters namespaces , if want use custom elements , namespaces need make sure use xhtml5 correctly, making sure elements in namespace, making sure serve documents application/xhtml+xml (as parsed browsers xml parser understands namespaces, text/html parser parsed html5 parser not support namespaces).

based on created example http://home.arcor.de/martin.honnen/html/test2015061502.xhtml has code

<?xml version="1.0" encoding="utf-8"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>xmlhttprequest , dom level 3 xpath api test 'application/xhtml+xml' document</title> <script type="text/javascript"><![cdata[ function test() {   var req = new xmlhttprequest();   req.open('get', 'test2015061501.xhtml', true);   req.onload = function() {     var doc = req.responsexml;     var res = function(prefix) {       if (prefix === 'xhtml') {         return 'http://www.w3.org/1999/xhtml';       }       else if (prefix === 'hp') {         return 'http://example.com/hp';       }       else {         return null;       }     }     var path = '/xhtml:html/xhtml:body/hp:homepage/xhtml:header/xhtml:h1';     var val = doc.evaluate(path, doc, res, xpathresult.first_ordered_node_type, null).singlenodevalue;     document.body.insertadjacenthtml('beforeend', '<p>found ' + val + '</p>');   };   req.send(); }  window.onload = test; ]]></script> </head> <body> <h1>xmlhttprequest , dom level 3 xpath api test 'application/xhtml+xml' document</h1> </body> </html> 

and loads http://home.arcor.de/martin.honnen/html/test2015061501.xhtml has elements in xhtml namespace , elements in custom namespace:

<?xml version="1.0" encoding="utf-8" ?> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"       xmlns:hp="http://example.com/hp"        hp:schemalocation="http://example.com/hp ./schemas/homepage.xsd">   <head>     ...   </head>   <body>     <hp:homepage title="homepage">       <header>         <h1>i heading</h1>       </header>     </hp:homepage>   </body> </html> 

the xpath 1.0 expression in first document /xhtml:html/xhtml:body/hp:homepage/xhtml:header/xhtml:h1 , finds xhtml h1 htmlheadingelement element, both firefox 38 chrome 43.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -