检测传入的 XML “CR"字符,然后转换为 <br/> [英] Detecting incoming XML "CR" character, then converting to <br/>

查看:28
本文介绍了检测传入的 XML “CR"字符,然后转换为 <br/>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP 中使用 XSLT,它由 msxml6 提供服务.

I am using XSLT inside of ASP, it's serviced by msxml6.

加载到对象的传入 XML 具有回车符",我认为可能是 ASCII 10.我想在输出中将它们转换为 <br/>.

Incoming XML loaded to the object has "carriage returns" which I think may be ASCII 10. I would like to transform those to <br/> in the output.

我试图在传入的 XML 中检测 &#10;,但似乎找不到.我尝试过 Javascript(ASP 中的 JScript),但无济于事.

I am trying to detect &#10; in the incoming XML, but can't seem to find that. I've tried Javascript (JScript inside of ASP), to no avail.

有趣的是,它来自 MS Excel 电子表格ML.

It's coming from MS Excel spreadsheetML, interestingly.

想法:

  • 它是如何在 msxsm6 中的 XML 对象中编码的
  • 如何检测,然后用
    替换?

谢谢大家,stackoverflow 很棒!!

Thank you everyone, stackoverflow is great!!

推荐答案

来自 http://www.w3.org/TR/2008/REC-xml-20081126/#sec-line-ends

XML 解析的实体经常被存储在计算机文件中,用于编辑方便,被组织成行.这些行通常由字符的某种组合回车 (#xD) 和换行(#xA).

XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA).

为了简化应用程序的任务,XML 处理器必须表现得好像它标准化外部的所有换行符解析的实体(包括文档实体)在输入时,之前解析,通过翻译两个字符的序列 #xD #xA 和任何#xD 后面不跟 #xA 到单个 #xA 字符.

To simplify the tasks of applications, the XML processor MUST behave as if it normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character.

因此,可以查找 &#xA;(或 &#10;).但请注意,根据 XML 树提供程序(MSXSL XML 解析器不保留此文本节点),可能会或可能不会保留输入中的仅空白文本节点.当然,不保留空白,只保留文本节点.

So, it's ok to look for &#xA; (or &#10;). But do note that white space only text nodes from the input may or may not be preserve depending on XML tree provider (MSXSL XML parser doesn't preserve this text nodes). Not white space only text nodes are preserved, of course.

然后,这个名为 text 的模板在 XSLT 1.0 中用空的 br 元素替换新行:

Then, this text named template replace new lines with empty br elements in XSLT 1.0:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()" name="text">
        <xsl:param name="pString" select="."/>
        <xsl:choose>
            <xsl:when test="contains($pString,'&#xA;')">
                <xsl:call-template name="text">
                    <xsl:with-param name="pString" 
                     select="substring-before($pString,'&#xA;')"/>
                </xsl:call-template>
                <br/>
                <xsl:call-template name="text">
                    <xsl:with-param name="pString" 
                     select="substring-after($pString,'&#xA;')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$pString"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

<root>
<text>
whatever
</text>
<text>and more</text>
</root>

输出:

<root>
<text><br />whatever<br /></text>
<text>and more</text>
</root>

这篇关于检测传入的 XML “CR"字符,然后转换为 &lt;br/&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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