在浏览器上渲染XML [英] Render escaped XML on browser

查看:209
本文介绍了在浏览器上渲染XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下xml,我想在浏览器上从xslt呈现未转义的文本部分,例如:

For the below xml i want to render the unescaped text parts on browser from xslt e.g.:

<one>
&lt;text&gt;
</one>

我希望在浏览器上呈现的标记为:

I want the markup rendered on browser to be:

&lt;text&gt;

但是当我使用如下所示的应用模板

But when I use the apply templates which looks like below

<xsl:template match="text()" mode="literalHTML">
    <xsl:copy-of select=".">
    </xsl:copy-of>
</xsl:template>

上述XML被呈现为:

<text>

如何修改此模板,以便打印& lt; text& gt;在浏览器上?

How can I modify this template so that it prints &lt;text&gt; on browser?

最好的问候,
Keshav

Best Regards, Keshav

推荐答案

这可以在XSLT 1.0中使用相当棘手的递归处理来实现。

This can be achieved in XSLT 1.0 using quite tricky recursive processing.

幸运的是,可以使用 FXSL (XSLT模板库)在短短几分钟内解决相同的任务:

Fortunately, one can use FXSL ( a library of XSLT templates) to solve the same task in just a few minutes:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:testmap="testmap"
exclude-result-prefixes="xsl f testmap">
   <xsl:import href="str-dvc-map.xsl"/>

   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>

   <xsl:template match="/">
     <xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vTestMap"/>
       <xsl:with-param name="pStr" select="/*/text()"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="escape" mode="f:FXSL"
     match="*[namespace-uri() = 'testmap']">
      <xsl:param name="arg1"/>

      <xsl:choose>
       <xsl:when test="$arg1 = '&lt;'">&amp;lt;</xsl:when>
       <xsl:when test="$arg1 = '&gt;'">&amp;gt;</xsl:when>
       <xsl:otherwise><xsl:value-of select="$arg1"/></xsl:otherwise>
      </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

<one>
&lt;text&gt;
</one>

想要的结果生成

&amp;lt;text&amp;gt;

它在浏览器中显示为 & lt; text& gt;

这篇关于在浏览器上渲染XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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