XSLT 1.0 不能翻译撇号 [英] XSLT 1.0 can’t translate apostrophe

查看:21
本文介绍了XSLT 1.0 不能翻译撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XML 中有以下内容:

I have got the following content in my XML:

<MyElement> Cats&amp;apos; eyes </ MyElement >

当我在 XSLT 1.0 中使用以下代码时:

When I use the following code in my XSLT 1.0:

<xsl:value-of select="MyElement" />

输出为

Cats&apos; eyes 

但我想得到

Cats’ eyes

我试过了

<xsl:value-of select='translate(MyElement, "&amp;apos; ", "&apos; ")'  />

但是没有用,结果是有一些消除字符!!!!

But it didn’t work , the result was ca’ ey with some eliminated characters!!!!

有什么想法吗?

谢谢

推荐答案

您的第一个问题是您的内容似乎已被双重转义".撇号的实体是 &apos;,而不是 &apos;.

Your first problem is that it appears your content has been "double escaped". The entity for an apostrophe is &apos;, not &amp;apos;.

您尝试用 translate() 不起作用,因为它的工作方式与您预期的不同.它不是查找/替换方法.您在第二个参数中指定一个字符列表,然后在第三个参数中指定要转换成每个字符的相应字符.如果第三个参数的字符列表中没有对应的字符,则将其删除.

Your attempt to replace characters with translate() did not work, because it works differently than you are expecting. It is not a find/replace method. You specify a list of characters in the second argument and then the corresponding character to translate each one into in the third parameter. If there is no corresponding character in the list of characters in the third parameter then it will be removed.

您需要使用递归模板来执行字符替换.

You will need to use a recursive template to perform the character replacement.

例如:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="MyElement"/>
            <xsl:with-param name="replace" select="'&amp;apos;'"/>
            <xsl:with-param name="with" select='"&apos;"'/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="replace-string">
        <xsl:param name="text"/>
        <xsl:param name="replace"/>
        <xsl:param name="with"/>
        <xsl:choose>
            <xsl:when test="contains($text,$replace)">
                <xsl:value-of select="substring-before($text,$replace)"/>
                <xsl:value-of select="$with"/>
                <xsl:call-template name="replace-string">
                    <xsl:with-param name="text"
                        select="substring-after($text,$replace)"/>
                    <xsl:with-param name="replace" select="$replace"/>
                    <xsl:with-param name="with" select="$with"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

当然,对于 XSLT 2.0,您可以只使用 replace() 函数:

Of course, with XSLT 2.0 you could just use the replace() function:

<xsl:sequence select='replace(MyElement, "&amp;apos;", "&apos;")'/>

这篇关于XSLT 1.0 不能翻译撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆