在JSP中漂亮的打印XML数据 [英] Pretty print XML data in JSP

查看:182
本文介绍了在JSP中漂亮的打印XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在JSP中打印(即缩进)XML数据?我有以下代码:

How do I pretty print (ie. with indentation) XML data in the JSP? I have the following code:

<c:forEach items="${stuffs}" var="stuff">
    <pre>
        <c:out value="${stuff}" escapeXml="true"/><br/>
    </pre>
</c:forEach>

但问题是当 $ {stuff} 是一个无格式的XML,它将在jsp中显示为一个长XML数据。我需要将它打印在< p> 标记中。

But the problem is when ${stuff} is an unformatted XML, it will show in the jsp as one long XML data. I need it pretty-printed inside the <p> tag.

推荐答案

XSLT通过 xsl:output 元素来实现这一点。如果你可以应用XSLT,我建议使用这样的样式表(基于身份转换):

XSLT has a simple means of doing this via the xsl:output element. If you can apply an XSLT, I suggest using a stylesheet like this (based on the identity transformation):

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

这篇关于在JSP中漂亮的打印XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆