【问题标题】:(XSL 2.0) Transform/Replace HTML and ASCII Characters in String(XSL 2.0) 转换/替换字符串中的 HTML 和 ASCII 字符
【发布时间】: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 &amp;#39;62 sent a text to his friend‰Ûªs phone &lt;br /></classnote>
</row>
</root>

我想替换“'”的所有实例和 "‰Ûª" 使用右单引号并将所有 HTML 中断替换为空格。

我试过了

<xsl:value-of select="translate(., '&amp;#39;', '’')" />

<xsl:value-of select="translate(., '&#39;', '’')" />

替换“'”,以及替换换行符的类似代码。

我在这里缺少一些简单的东西吗?我真的很感激帮助。完整的 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, ' &quot;', ' “'), '&quot;', '”'), '''', '’')" />
    <xsl:text>\par}&#10;</xsl:text>
    <xsl:value-of select="translate(., '&amp;#39;', '’')" />
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 如果您知道 XSLT/XPath 2.0 中的 replace,那么您为什么要尝试使用 translate 函数来替换字符序列? translate 用于将单个字符映射到单个字符,但如果您想用另一个字符串或字符替换 ‰Ûª 之类的字符序列,请使用 replace

标签: html xml xslt ascii


【解决方案1】:

classnote 元素的上下文似乎是一个带有 XML 片段的字符串,因此给定 Oxygen 或任何其他具有 Saxon 9 商业版本的环境,使用 XSLT 3.0 和 parse-xml-fragment 可能是最简单的,因为我们可以写一个模板来转换br元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0">

<xsl:output method="text"/>

<xsl:template match="classnote">
  <xsl:apply-templates select="parse-xml-fragment(.)" mode="convert"/>
</xsl:template>

<xsl:template match="br" mode="convert">
  <xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="text()" mode="convert">
  <xsl:value-of select="replace(., '&amp;#39;|‰Ûªs', '''')"/>
</xsl:template>

</xsl:stylesheet>

输入为

<classnote>Joe Smith &amp;#39;62 sent a text to his friend‰Ûªs phone &lt;br /></classnote>

Saxon 9.5 PE 输出

Joe Smith '62 sent a text to his friend' phone

如果您不想使用 XSLT 3.0,那么该示例至少显示了如何用单引号替换子字符串:replace(., '&amp;amp;#39;|‰Ûªs', '''')

【讨论】:

    猜你喜欢
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2011-02-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多