简体   繁体   中英

Problems with parsing XHTML page via javascript

There is some xhtml page, the source of this page was parsed before loading in the browser, to find some XPath positions. Then this page was loaded into browser, and i want via JAvaScript(on some action) insert some text in XPath positions, that already have been found. Problem is, that in JavaScript(jQuery) i can get only innerHTML (HTML DOM) of this page, and it's differs from XHTML DOM(XML DOM) that have been parsed. How can i get in JavaScript XML DOM of XHTML page, not HTML DOM. Example(some part of page):

<div><p />
  Text1
  <p />
  Text2
</div>

When i want to find XPath position of Text1 it will be /div/text()[1], but in browser this part of code will be converted in HTML DOM, and looks like this:

<div>
  <p>Text1</p>
  <p>Text2</p>
</div>

and Text1 is now on the /div/p[1]

Don't use jQuery to access the DOM. Instead, use XPath in Javascript or find a library which will allow you to query XML nodes using XPath. Read more about it at the Mozilla Developer Center's Introduction to XPath in Javascript .

Serve the XHTML as application/xhtml+xml or construct it so that it follows the HTML compatibility guidelines .

You can't tell the browser it is HTML (by serving it as text/html) and expect it to treat it as XHTML.

(And try to get the semantics right, what sort of nonsense is a paragraph containing nothing followed by some text that isn't in a paragraph?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM