如何替换 return ( | ) 通过 xslt 1.0 中的字符串“\n”? [XML 多行 base64 到 json]

How to replace return (
 | 
) by the string "\n" in xslt 1.0 ? [XML multiline base64 to json]

我有一个 xml,我需要把它变成 json。 除了 base64 多行

,我大部分都很好
<file>TU0...AAA
FOO...BCD
FOO...012
FOO...ZYX</file>

在 json 中,多行是不可能的,这应该只在 1 行中重写为

"file":"TU0...AAA\nFOO...BCD\nFOO...012\nFOO...ZYX" 

用"real"双字符字符串“\n”连接每一行。

我可以在 xslt 1.0 中这样做吗?

我知道我可以使用翻译,但那只能用于一个字符。 我试试

translate(.,'&#10;',' ') 

这会将 returns 替换为 space,也许这不会破坏 json.

的 base64 解码

但是,如果我想这样做 "right way",我想我需要自定义函数。 在我的例子中 returns 似乎是“ ”。 但是如果有人提出了适用于所有组合的解决方案( ) 那太好了。

我的主要目标是 chrome 网络浏览器,但 运行 在所有浏览器中都很好。

如果您只想去掉换行符,您可以使用 normalize-space($string) 函数,例如:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xd"
    version="1.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Apr 22, 2020</xd:p>
            <xd:p><xd:b>Author:</xd:b> bwb</xd:p>
            <xd:p>generates a normalized text output of the file element</xd:p>
        </xd:desc>
    </xd:doc>

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:apply-templates select="file"/>
    </xsl:template>

    <xsl:template match="file">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

</xsl:stylesheet>

你仍然可以用其他东西替换空格(对于 JSON 可能用 ,

如果您确实想要 \n,您可以尝试以下样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs math xd"
    version="1.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Apr 22, 2020</xd:p>
            <xd:p><xd:b>Author:</xd:b> bwb</xd:p>
            <xd:p>Override default text() template by adding a search and replace funtionality</xd:p>
        </xd:desc>
    </xd:doc>

    <xsl:output method="text"/>

    <xd:doc scope="component">
        <xd:desc>The string that should be searched and replaced by $param-replaceString</xd:desc>
    </xd:doc>
    <xsl:param name="param-searchString" select="'&#10;    '"/><!-- actually you also wnat to replace the whitespaces, that's why the searchString looks so  strange -->

    <xd:doc>
        <xd:desc>The string that replace any occurence of $param-searchString</xd:desc>
    </xd:doc>
    <xsl:param name="param-replaceString" select="'\n'"/>

    <xd:doc scope="component">
        <xd:desc>Override for default text() template testing for $param-searchString presence and calling replace template</xd:desc>
    </xd:doc>
    <xsl:template match="text()">
        <xsl:choose>
            <xsl:when test="contains(., $param-searchString)">
                <xsl:call-template name="replace">
                    <xsl:with-param name="InputString" select="."/>
                    <xsl:with-param name="searchString" select="$param-searchString"/>
                    <xsl:with-param name="replaceString" select="$param-replaceString"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="replace">
        <xsl:param name="InputString"/>
        <xsl:param name="searchString"/>
        <xsl:param name="replaceString"/>

        <xsl:choose>
            <xsl:when test="contains($InputString, $searchString)">
                <xsl:variable name="token-before-first-match" select="substring-before($InputString, $searchString)"/>
                <xsl:variable name="token-after-first-match" select="substring-after(., concat($token-before-first-match, $searchString))"/>
                <xsl:value-of select="concat($token-before-first-match, $replaceString)"/>
                <xsl:choose>
                    <xsl:when test="contains($token-after-first-match, $searchString)">
                        <xsl:call-template name="replace">
                            <xsl:with-param name="InputString" select="$token-after-first-match"/>
                            <xsl:with-param name="searchString" select="$searchString"/>
                            <xsl:with-param name="replaceString" select="$replaceString"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$token-after-first-match"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$InputString"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>