Search code examples
javascripthtmlxmldomxhtml

How are Elements in an XML DOM Updated?


So far this is what I understand:

  1. The DOM is a diagrammatic representation of all the elements on a webpage which are called objects. These objects/elements are displayed and constructed as a tree or map.

  2. There is also another aspect of a DOM. Besides being a tree of elements/virtual objects, it is also a platform and neutral software based interface i.e. an API that allows programs and scripts to dynamically access and update the content, structure and style of a webpage. In other words, it allows for the manipulation of a webpage since it defines how to read, change, add and delete elements which is similar to CRUD functions of a database.

  3. The W3C DOM standard is separated into 3 different parts

  • Core DOM (An interface used to manipulate XML and HTML pages)
  • XML DOM (An interface used to manipulate XML pages)
  • HTML DOM (An interface used to manipulate HTML pages)

So from the research I did and and from putting all of the above three points together, what I have gotten so far is that the HTML DOM is accessed via JavaScript and from JavaScript you can manipulate the information on the webpage, for example changing elements and so on. But my question is, how do you manipulate page content via the XML DOM if XML itself does not support JavaScript natively?

I was able to find the following three post on the site, but they do not answer my question as it pertains to the manipulation of page content XML DOMs specifically:

*What is the difference between XML DOM and HTML DOM

*What is the difference between HTML DOM and XML DOM

*How to include Javascript in xml-document?


Solution

  • XML itself is the data format - it does not define tags. The meaning of tags are part of the next layer - formats/languages that conform to XML syntax.

    Most XML based formats do not have script support. An application (for example an web page with Javascript) loads them into DOM to read the data.

    HTML and XHTML are markup languages. Both can be parsed into DOM, but only XHTML is an XML format. They define and support the script element. The scripts will only be executed if you render the page, not if you just load the document into DOM. The rendering engine will execute the scripts - not the DOM parser. However the browser provides you with access to DOM its rendering and allows you to manipulate it.

    SVG is another XML (graphics) format that supports scripts. It can reference and embed JavaScript. But they will not be executed if you load them into an img tag, you need to embed them or or use an object tag.

    Another option is a (wellformed) XML referencing an XSLT using a processing instruction. XSLT is a programming language for converting XML. Browsers execute the XSLT to transform the XML into (X)HTML, which it can then render as a web page. The generated output could include script tags. In this case the DOM you access from the scripts represents the generated output - not the original XML.