2017-10-04 123 views
-1

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

我想替换一个简单的空间。 任何线索?

+0

您将要调整的琴弦是否有最大长度? –

+0

是最多150个字符。 –

回答

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功能,它允许对正则表达式。

+0

只是完美!非常感谢,我已经有一段时间了。 –