views:

193

answers:

2

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]

A: 

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.

Rahul
But i have also tried to use just JavaScript, but problem is, to get a XML DOM of page... Even when i just look DOM of this page in Mozzila(Firebug), i see the HTML DOM representation of the page, not source-xml dom.
Le_Coeur
Well, see David's answer below for why you won't be able to use XPath as long as it's parsed as HTML. In which case I recommend using the DOM's various methods to access the information you need rather than XPath.
Rahul
A: 

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?)

David Dorward
The problem is, that i can't change the source of the pages, because, it's some module(through Proxy) for TWiki. I use TWiki pages, the content of this pages defined as content="text/html" but all pages are XHTML 1.0 valid. http://twiki.org/
Le_Coeur
Validity is only the start of conformance checking. If you want to use XHTML (and care about IE) then you need to conform to the HTML Compatibility Guidelines. If the service doesn't give you conformant code then you need to get it fixed, or process it yourself before passing it on. It's broken - fix it, don't try to work around it.
David Dorward