How can I display pending XML code inside an HTML tag

I want to display XML indentation on an html page inside <code></code> I get XML as from a javascript function

new XMLSerializer()).serializeToString(newXMLDoc)

which gives me an unintended XML string.

One way that I can think of is to use <ul> and <li>

But is there a better way to do this?

Can XSLT help me in this case (sorry, I don't know much about XSLT ). If so, where do I need to attach the XSLT stylesheet, in the html document or in the xmlString , which I am adding to the <code></code> .

The only client application that I can not do on the server side.

UPDATE: I also replace <and> in xmlString with &lt; and &gt; can i use XSLT?

Even after applying XSLT provided by Dimitre Novatchev, I don’t get indented text, although I see that XML is indented when I use XSLT using the SAXON parser core, but when I do this in my javascript code, I get the same Inappropriate code.

  //var xslt contains the XSLT provided by Dimitre Novatchev as string //var XMLDoc is the XMLDocument object to be transformed xsltProcessor=new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); newXMLDoc = xsltProcessor.transformToDocument(xml,document); //This is how I am converting the transformed XML to string return (new XMLSerializer()).serializeToString(newXMLDoc) 
+4
source share
2 answers

You can do this in two steps:

First, simply process the XML document with the conversion and <xsl:output indent="yes"/> :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

When applying this transform to any XML document (like this one):

 <root><node/></root> 

most XSLT processors (.NET XslCompiledTransform, Saxon 6.5.4 and Saxon 9.0.0.2, AltovaXML) produce the desired result :

 <root> <node /> </root> 

The second step is to perform the operation of replacing the string with the result in order to replace the < character with the string &lt; .

In case you need a fancier formatted display, see how XPath Visualizer does it with XSLT .

+1
source

I suggest that you use server-side code for this and send the HTML HTML format to your client. Yes, XSLT can help you. But you must encode your XML on the server HTML .

However, note that if you remove \n tags (line characters) and other space characters from your document (e.g. spaces, tabs, etc.), you need to look for something like a client - formatted document formatting , or similar . Writing such a thing is never an easy task.

You can also use syntax for XML. One good example can be found here . Then you can simply use:

 <pre class='brush: xml;'> // XML goes here. </pre> 
+5
source

Source: https://habr.com/ru/post/1384469/


All Articles