TAGS :Viewed: 7 - Published at: a few seconds ago

[ xml <-> html conversion in javascript ]

What javascript framework/library may you advice if I want to convert HTML to XML and vice versa, by a set of rules?

e.g.

&lt;a type="foo"&gt;
  &lt;b&gt;bar&lt;sth x="42"/&gt;&lt;/b&gt;
&lt;/a&gt;

to

&lt;div class="a type-foo"&gt;
    &lt;div class="b"&gt;bar&lt;span class="sth"&gt;42&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

I used to traverse DOM by JQuery and build everything manually - lot of functions for every possible tag. What is a shorter solution? E.g. I want to say "<a> converts to <div class="a">" etc. and then run something like htmlToXml() or xmlToHtml().

Answer 1


if your HTML is a valid XHTML you can write an XSL transformation and apply it with javascript. Take a look here: http://www.w3schools.com/xsl/xsl_client.asp