XSLT用空格替换非ascii

时间:2017-10-04 11:16:16

标签: xml xslt ascii

我已经制作了这个XSLT但只删除了非ASCII字符。

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="charset" select="'@.1234567890 abcdefghilmnopqrstuvwzkyx ABCDEFGHILMNOPQRSTUVWZYKX'" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()">
    <xsl:value-of select="translate(., translate(., $charset, ''), '')"/>
</xsl:template>

</xsl:stylesheet>

我想用简单的空格替换。 任何线索?

1 个答案:

答案 0 :(得分:2)

如果您要替换的文本有最大长度,您只需定义一个由150个空格组成的变量,并在translate表达式中使用该变量。

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

    <xsl:strip-space elements="*"/>

    <xsl:param name="charset" select="'@.1234567890 abcdefghilmnopqrstuvwzkyx ABCDEFGHILMNOPQRSTUVWZYKX'" />
    <!-- 150 spaces -->
    <xsl:param name="spaces" select="'                                                                                                                                                      '" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="translate(., translate(., $charset, ''), $spaces)"/>
    </xsl:template>
</xsl:stylesheet>

如果您可以使用XSLT 2.0,则可以使用replace函数,该函数允许使用正则表达式。

相关问题