如何在 HTML 代码标签内显示缩进的 XML
我想在
内的 html 页面中显示缩进的 XML 获取 XML
我从 javascript 函数new XMLSerializer()).serializeToString(newXMLDoc)
,这给了我意外的 XML 字符串。
我能想到的一种方法是使用
和
但有更好的方法吗?
在这种情况下,XSLT 可以帮助我吗(抱歉,我对 XSLT
不太了解)。如果是,我需要在哪里附加 XSLT 样式表,在 html 文档中还是在 xmlString 中
我要附加到
标记。
它只是一个客户端应用程序,我无法在服务器端执行任何操作。
更新:我还将替换 <和>在带有 <
和 >
的 xmlString 中我仍然可以使用 XSLT 吗?
即使在应用了 Dimitre Novaatchev 给出的 XSLT 之后,我也没有得到缩进的文本,尽管当我使用 SAXON 解析器 kernow 应用 XSLT 时,我可以看到 XML 被缩进,但是当我在 javascript 代码中执行此操作时,我得到了相同的未缩进的文本代码。
//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)
I want to display indented XML in a html page inside <code></code>
I am getting the XML as from the javascript function
new XMLSerializer()).serializeToString(newXMLDoc)
which gives me unintended XML String.
One way that I can think is doing this is using <ul>
and <li>
But is there a better way of doing this ?
Can XSLT help me in this case ( sorry I don't know much about XSLT
).If yes where do I need to attach the XSLT style sheet , in the html document or in the xmlString
that i am appending to the <code></code>
tag.
Its only a client side app I can not do anything on the server side.
UPDATE: I am also replacing the < and > in the xmlString with <
and >
can i still use XSLT ?
Even after applying the XSLT given by Dimitre Novatchev I am not getting the indented text,though I can see the XML getting indented while I apply the XSLT using SAXON parser kernow but when i am doing it in my javascript code I am getting the same unindented 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用服务器端代码来执行此操作,并将 HTML 格式的 XML 发送到客户端。是的,XSLT 可以帮助您。但是您应该在服务器上HTML 编码您的XML。
但是,请注意,如果您要从文档中删除
\n
标记(换行符)和其他空白字符(例如空格、制表符等),那么您需要搜索类似 <客户端文档格式化程序,或类似的东西。写这样的东西从来都不是一件容易的事。您还可以使用 XML 的语法荧光笔。 此处可以找到一个很好的示例。然后你可以简单地使用:
I suggest that you use server-side code to do this, and send HTML formatted XML to your client. Yeah, XSLT can help you. But you should HTML encode your XML at server.
However, please note that if you are removing
\n
marks (line feed characters) and other whitespace characters from your document (like spaces, tabs, etc.), then you need to search for something like client-side document formatter, or stuff like that. Writing such a thing is never an easy task.Also you can use a syntax highlighter for XML. One good example could be found here. Then you can simply use:
您可以分两步完成此操作:
首先,只需使用身份转换< /a> 并使用
指令:在任何 XML 文档上应用此转换时(例如这个):
大多数 XSLT 处理器< /strong>(.NET XslCompiledTransform、Saxon 6.5.4 和 Saxon 9.0.0.2、AltovaXML)生成所需结果:
第二步是执行字符串替换操作结果,以便任何
<
字符都替换为<
字符串。如果您想要更高级的格式显示,请查看 XPath Visualizer 使用 XSLT 来完成此操作。
You can do this in two steps:
First, just process the XML document with the identity transformation and with an
<xsl:output indent="yes"/>
instruction:When applying this transformation on any XML document (e.g. this one):
most XSLT processors (.NET XslCompiledTransform, Saxon 6.5.4 and Saxon 9.0.0.2, AltovaXML) produce the wanted result:
The second step is to perform a string replace operation on the result so that any
<
character is replaced with the<
string.In case you want a fancier-formatted display, do have a look how the XPath Visualizer does this with XSLT.