检测传入的XML“CR”字符,然后转换为
>

发布于 2024-09-25 13:46:16 字数 371 浏览 0 评论 0原文

我在 ASP 内部使用 XSLT,它由 msxml6 提供服务。

加载到对象的传入 XML 具有“回车符”,我认为这可能是 ASCII 10。我想将它们转换为输出中的

我试图检测传入 XML 中的 
,但似乎找不到。我尝试过 Javascript(ASP 中的 JScript),但没有成功。

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

想法:

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

谢谢大家,stackoverflow 太棒了!

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

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.

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

It's coming from MS Excel spreadsheetML, interestingly.

Ideas on:

  • how it's encoded in the XML object inside msxsm6
  • how to detect, then replace with <br/>?

Thank you everyone, stackoverflow is great!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再让梦枯萎 2024-10-02 13:46:16

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

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

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

因此,可以查找 (或 )。但请注意,输入中仅包含空白的文本节点可能会也可能不会保留,具体取决于 XML 树提供程序(MSXSL XML 解析器不保留此文本节点)。当然,不保留空白,仅保留文本节点。

然后,此 text 命名模板用 XSLT 1.0 中的空 br 元素替换新行:

<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,'
')">
                <xsl:call-template name="text">
                    <xsl:with-param name="pString" 
                     select="substring-before($pString,'
')"/>
                </xsl:call-template>
                <br/>
                <xsl:call-template name="text">
                    <xsl:with-param name="pString" 
                     select="substring-after($pString,'
')"/>
                </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>

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

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).

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.

So, it's ok to look for (or ). 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.

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,'
')">
                <xsl:call-template name="text">
                    <xsl:with-param name="pString" 
                     select="substring-before($pString,'
')"/>
                </xsl:call-template>
                <br/>
                <xsl:call-template name="text">
                    <xsl:with-param name="pString" 
                     select="substring-after($pString,'
')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$pString"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

With this input:

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

Output:

<root>
<text><br />whatever<br /></text>
<text>and more</text>
</root>
行至春深 2024-10-02 13:46:16
replace(replace(replace(XMLString, vbCrLf, ""), vbCr, ""), vbLf, "")
replace(replace(replace(XMLString, vbCrLf, ""), vbCr, ""), vbLf, "")
云巢 2024-10-02 13:46:16

这是我用来执行此操作的模板:

<xsl:template name="nl2br">
        <xsl:param name="contents" />

        <xsl:choose>
                <xsl:when test="contains($contents, '
')">
                        <xsl:value-of select="substring-before($contents, '
')" disable-output-escaping="yes" />
                        <br />
                        <xsl:call-template name="nl2br">
                                <xsl:with-param name="contents" select="substring-after($contents, '
')" />
                        </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="$contents" disable-output-escaping="yes" />
                </xsl:otherwise>
        </xsl:choose>
</xsl:template>

Here is a template I use to do this:

<xsl:template name="nl2br">
        <xsl:param name="contents" />

        <xsl:choose>
                <xsl:when test="contains($contents, '
')">
                        <xsl:value-of select="substring-before($contents, '
')" disable-output-escaping="yes" />
                        <br />
                        <xsl:call-template name="nl2br">
                                <xsl:with-param name="contents" select="substring-after($contents, '
')" />
                        </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="$contents" disable-output-escaping="yes" />
                </xsl:otherwise>
        </xsl:choose>
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文