Note that there are some explanatory texts on larger screens.

plurals
  1. POUse XSLT to pretty print XML/XHTML without corrupting namespace info
    primarykey
    data
    text
    <p>I'm trying to use XSLTs (in Javascript) to pretty-print an XHTML doc that has been machine generated. However, the various XSLTs I've tried to use, all mangle the xmlns attributes (see below).</p> <p>Here is a sample of desired output (made by hand from unindented, compact, XHTML). </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:orx="http://openrosa.org/jr/xforms" xmlns="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa"&gt; &lt;h:head&gt; &lt;h:title&gt;New Form1&lt;/h:title&gt; .... </code></pre> <p>Here is what I'm getting instead:</p> <pre><code>&lt;h:html h="http://www.w3.org/1999/xhtml" orx="http://openrosa.org/jr/xforms" xmlns="http://www.w3.org/2002/xforms" xsd="http://www.w3.org/2001/XMLSchema" jr="http://openrosa.org/javarosa"&gt; &lt;h:head&gt; &lt;h:title&gt;New Form1&lt;/h:title&gt; ... </code></pre> <p>Notice the xmlns attributes are altered in the 'h:html' tag in the second code snippet. Also the beginning <code>&lt;?xml ...&gt;</code> tag is missing.</p> <p>This is (one of many) XSLTs I've used with similar results:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;\ &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Any ideas on what I'm doing wrong? Am I trying to do the impossible?</p> <p>If you're wondering why I'm trying to do this: I have to use GWT as the framework for designing a FormDesigner web app. This is the output, but needs to be human readable for the more technically inclined users that want to do by-hand edits. GWT just <em>doesn't do</em> xml pretty printing (as far as I can tell in my searching so far). Thus, we go native to JS land and try for a solution there.</p> <p>Ideas/solutions would be greatly appreciated!</p> <p>Edit:</p> <p>Here is the Javascript that makes use of the XSLT. I call the beautifyXML() function to actual perform the indentation:</p> <pre><code>//var xsl_string = '&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt;&lt;/xsl:stylesheet&gt;'; // from: http://www.xml.com/pub/a/2006/11/29/xslt-xml-pretty-printer.html?page=3 var xsl_string = '&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;\ &lt;xsl:output method="xml" indent="yes"/&gt;\ &lt;xsl:strip-space elements="*"/&gt;\ &lt;xsl:template match="/"&gt;\ &lt;xsl:copy-of select="."/&gt;\ &lt;/xsl:template&gt;\ &lt;/xsl:stylesheet&gt;'; var xsl = (new DOMParser()).parseFromString(xsl_string, "text/xml"); function stringToXml(xml_string) { return (new DOMParser()).parseFromString(xml_string, "text/xml"); } function xmlToString(xml) { return (new XMLSerializer()).serializeToString(xml); } function isParseError(xml) { try { // console.log( xml.documentElement.firstChild.firstChild.tagName); return xml.documentElement.tagName == "parsererror" || xml.documentElement.firstChild.firstChild.tagName == "parsererror"; } catch (ex) { return false; } } function beautifyXml(input) { var xml = stringToXml(input); if (isParseError(xml)) { return input; } var transformedXml = xslTransformation(xml, xsl); return xmlToString(transformedXml); } /** * @param xml * @param xsl */ function xslTransformation(xml, xsl) { // code for IE if (window.ActiveXObject) { var ex = xml.transformNode(xsl); return ex; } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation &amp;&amp; document.implementation.createDocument) { var xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); var resultDocument = xsltProcessor.transformToFragment(xml, document); return resultDocument; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload