【问题标题】:XSLT replace non ascii with spaceXSLT 用空格替换非 ascii
【发布时间】:2017-10-04 11:16:15
【问题描述】:

我已经制作了这个 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>

我想用一个简单的空格代替。 有什么线索吗?

【问题讨论】:

  • 要调整的字符串是否有最大长度?
  • 是的,最多 150 个字符。

标签: xml xslt ascii


【解决方案1】:

如果您要替换的文本有最大长度,您可以简单地定义一个由 150 个空格组成的变量,并在翻译表达式中使用它。

<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 函数,它允许使用正则表达式。

【讨论】:

  • 完美!非常感谢,我已经有一段时间头疼了。
  • 这是一个非常好的技术。我不知道这可以用 XSLT 1.0 完成。
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 2013-12-10
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多