问题描述
我正在编写一个 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
我需要将 ' 转义为 '
以用于 google Sitemap.不幸的是,我尝试过的每一次尝试都将字符串 ''
' 视为无效的 ''' - 令人沮丧.XSLT 有时会让我发疯.
I need to escape the ' to '
for google Sitemap. Unfortunately every attempt I've tried treats the string ''
' 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)
推荐答案
所以你的输入中有 '
,但你需要字符串
输出?
So you have '
in your input, but you need the string
in your output?
在您的 XSL 文件中,使用 (除非您使用的是 XSLT 2.0):
In your XSL file, replace '
with &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="'"/>
<xsl:with-param name="by" select="&apos;"/>
</xsl:call-template>
</loc>
问题是 '
被 XSL 解释为 '
.'
将被解释为 '
.
The problem is '
is interpreted by XSL as '
. &apos;
will be interpreted as '
.
这篇关于XSLT - 在输出中用转义文本替换撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!