【发布时间】:2014-04-28 18:18:16
【问题描述】:
我正在编辑 XSL 样式表以将 XML 文档转换为样式化的 RTF 文件,并且字符串中有一些 HTML 代码和 ASCII 字符,我想用等效的文本字符替换它们。我一直在寻找这个问题的答案,但一直找不到有效的解决方案。这是我第一次使用 XLS,非常感谢您提供的任何帮助!
例如,我有以下 XML,通过氧气编辑器从 CSV 翻译
<root>
<row>
<first_name>Joe</first_name>
<last_name>Smith</last_name>
<classnote>Joe Smith &#39;62 sent a text to his friend‰Ûªs phone <br /></classnote>
</row>
</root>
我想替换“'”的所有实例和 "‰Ûª" 使用右单引号并将所有 HTML 中断替换为空格。
我试过了
<xsl:value-of select="translate(., '&#39;', '’')" />
和
<xsl:value-of select="translate(., ''', '’')" />
替换“'”,以及替换换行符的类似代码。
我在这里缺少一些简单的东西吗?我真的很感激帮助。完整的 XSL 如下。
<?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" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="text" indent="no" encoding="macroman" />
<xsl:strip-space elements="*" />
<xsl:template name="row">
<xsl:text>{\pard \s2 \ql \f22\fs24 \li0\ri0\sb240\sl-360\slmult0</xsl:text>
<xsl:text>{\cs11 \b </xsl:text>
<xsl:variable name="first_name" select="normalize-space(first_name)" />
<xsl:variable name="last_name" select="normalize-space(last_name)" />
<xsl:value-of select="$first_name" />
<xsl:value-of select="concat(' ', $last_name)" />
<xsl:value-of
select="if ( normalize-space(SVP) )
then concat(' ', normalize-space(replace(SVP, '''', '’')), ',')
else ''" />
<xsl:call-template name="degrees" />
<xsl:text>} </xsl:text>
<xsl:variable name="temp" select="replace(classnote, '\s+', ' ')" />
<xsl:value-of
select="replace(replace(replace($temp, ' "', ' “'), '"', '”'), '''', '’')" />
<xsl:text>\par} </xsl:text>
<xsl:value-of select="translate(., '&#39;', '’')" />
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
如果您知道 XSLT/XPath 2.0 中的
replace,那么您为什么要尝试使用translate函数来替换字符序列?translate用于将单个字符映射到单个字符,但如果您想用另一个字符串或字符替换‰Ûª之类的字符序列,请使用replace。