如何替换字符串中间的换行符

How to replace newline character in the middle of string

 <xsl:value-of 
      select="concat(translate(., 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZ áéíóúüÁÉÍÓÚ¿?&gt;&lt;:,&#10;&#13;', 'abcdefghijklmnñopqrstuvwxyz-aeiouuaeiou-------'), '.html')" />

我正在使用此 XPath 从字符串中删除不需要的字符:

<somenode> This is a string
with new
lines
</somenode>

但它并没有像预期的那样替换换行符(&#10;&#13; 都没有)。输出仍然显示为 %0D%0A

在 PHP 中,当我将此 RegEx 应用于完全相同的字符串 /[\n\r]/(通过 $somenode->nodeValue)时,它确实按预期工作。

更新

这是我的XML,与示例有点不同:

<temas>
...
<tema>
    <title>La tildación&#13;
diacrítica </title>
    <nav>
        <menu>
            <menu-item>La ortografía acentual: ¿cómo pongo las tildes? </menu-item>
            <menu-item>La tildación&#13;
diacrítica </menu-item>
            <menu-item>El diptongo y el hiato</menu-item>
        </menu>
    </nav>
    <content>
        ...
    </content>
    <pie>
        <prev>La ortografía acentual: ¿cómo pongo las tildes? </prev>
        <next>El diptongo y el hiato</next>
    </pie>
</tema>
</temas>

这是从 HTML 文件生成的。该文件中的 ^M 被转换为 &#13; 和换行符。所以,我的猜测是 &#13; 与 xpath 不匹配。所以我的问题应该是 "how do I match the string '&#13' with XPath?".

正如 Barmar 所建议的那样,使用 &#10;,正确的换行符 - 和 try it online here.

如果这对您不起作用,请显示完整的、可验证的 XSLT 代码示例(整个样式表)并说明 XSLT 的版本和您使用的处理器。

XML 输入

<somenode> This is a string
with new
lines
</somenode>

样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="somenode">
        <xsl:copy>
            <xsl:value-of 
      select="concat(translate(., 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZ áéíóúüÁÉÍÓÚ¿?&gt;&lt;:,&#10;', 'abcdefghijklmnñopqrstuvwxyz-aeiouuaeiou-------'), '.html')" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

XML输出

<?xml version="1.0" encoding="utf-8"?>
<somenode>-this-is-a-string-with-new-lines-.html</somenode>

另一种方法是 规范化 所有字符串值,然后用 translate():

替换其中的字符
concat(translate(normalize-space(.), 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZ áéíóúüÁÉÍÓÚ¿?&gt;&lt;: ', 'abcdefghijklmnñopqrstuvwxyz-aeiouuaeiou-------'), '.html')"

normalize-space() 函数删除前导和尾随的白色 space 并将其间的所有白色 space 序列替换为单个 space 字符。然后,您的解决方案不依赖于特定白色space字符的出现。


编辑

根据您更新后的输入,它也可以正常工作 - 我对模板匹配(但不是内容)进行了稍微调整:

<xsl:template match="title">
        <xsl:copy>
            <xsl:value-of 
      select="concat(translate(normalize-space(.), 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZ áéíóúüÁÉÍÓÚ¿?&gt;&lt;: ', 'abcdefghijklmnñopqrstuvwxyz-aeiouuaeiou-------'), '.html')" />
        </xsl:copy>
</xsl:template>

试试看 here.

您在寻找 normalize-space() 吗?

Function: string normalize-space(string?)

The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. Whitespace characters are the same as those allowed by the S production in XML. If the argument is omitted, it defaults to the context node converted to a string, in other words the string-value of the context node.