XSLT - 在输出中用转义文本替换撇号 [英] XSLT - Replace apostrophe with escaped text in output

查看:30
本文介绍了XSLT - 在输出中用转义文本替换撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 XSLT 模板,需要为 xml 站点地图输出一个有效的 xml 文件.

I'm writing an XSLT template that need to output a valid xml file for an xml Sitemap.

<url>
<loc>
    <xsl:value-of select="umbraco.library:NiceUrl($node/@id)"/>
</loc>
<lastmod>
    <xsl:value-of select="concat($node/@updateDate,'+00:00')"/>
</lastmod>
</url>

不幸的是,输出的 Url 包含一个撇号 -/what's-new.aspx

Unfortunately, Url that is output contains an apostrophe - /what's-new.aspx

我需要将 ' 转义为 &apos; 以用于 google Sitemap.不幸的是,我尝试过的每一次尝试都将字符串 '&apos;' 视为无效的 ''' - 令人沮丧.XSLT 有时会让我发疯.

I need to escape the ' to &apos; for google Sitemap. Unfortunately every attempt I've tried treats the string '&apos;' as if it was ''' which is invalid - frustrating. XSLT can drive me mad sometimes.

对技术有什么想法吗?(假设我可以找到解决 XSLT 1.0 模板和函数的方法)

Any ideas for a technique? (Assume I can find my way around XSLT 1.0 templates and functions)

推荐答案

所以你的输入中有 ',但你需要字符串 &nbsp;输出?

So you have ' in your input, but you need the string &nbsp; in your output?

在您的 XSL 文件中,使用 此查找/替换实现(除非您使用的是 XSLT 2.0):

In your XSL file, replace &apos; with &amp;apos;, using this find/replace implementation (unless you are using XSLT 2.0):

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

这样称呼:

<loc>
  <xsl:call-template name="string-replace-all">
    <xsl:with-param name="text" select="umbraco.library:NiceUrl($node/@id)"/>
    <xsl:with-param name="replace" select="&apos;"/>
    <xsl:with-param name="by" select="&amp;apos;"/>
  </xsl:call-template>
</loc>

问题是 &apos; 被 XSL 解释为 '.&apos; 将被解释为 &apos;.

The problem is &apos; is interpreted by XSL as '. &amp;apos; will be interpreted as &apos;.

这篇关于XSLT - 在输出中用转义文本替换撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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