xml XSLT - 在输出中用转义文本替换撇号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1103205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT - Replace apostrophe with escaped text in output
提问by Neil Fenwick
I'm writing an XSLT template that need to output a valid xml file for an xml Sitemap.
我正在编写一个 XSLT 模板,它需要为 xml 站点地图输出一个有效的 xml 文件。
<url>
<loc>
<xsl:value-of select="umbraco.library:NiceUrl($node/@id)"/>
</loc>
<lastmod>
<xsl:value-of select="concat($node/@updateDate,'+00:00')"/>
</lastmod>
</url>
Unfortunately, Url that is output contains an apostrophe - /what's-new.aspx
不幸的是,输出的 Url 包含一个撇号 - /what's-new.aspx
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 有时会让我发疯。
Any ideas for a technique? (Assume I can find my way around XSLT 1.0 templates and functions)
对技术有什么想法吗?(假设我可以找到解决 XSLT 1.0 模板和函数的方法)
回答by Welbog
So you have 'in your input, but you need the string in your output?
所以你有'你的输入,但你需要 输出中的字符串?
In your XSL file, replace 'with &apos;, using this find/replace implementation(unless you are using XSLT 2.0):
在您的 XSL 文件中,替换'为&apos;,使用此查找/替换实现(除非您使用的是 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>
Call it this way:
这样称呼它:
<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>
The problem is 'is interpreted by XSL as '. &apos;will be interpreted as '.
问题被'XSL 解释为'. &apos;将被解释为'.
回答by Myster
The simple way to remove unwanted characters from your URL is to change the rules umbraco uses when it generates the NiceUrl.
从 URL 中删除不需要的字符的简单方法是更改 umbraco 在生成 NiceUrl 时使用的规则。
Edit the config/umbracoSettings.config
编辑 config/umbracoSettings.config
add a rule to remove all apostrophes from NiceUrls like so:
添加规则以从 NiceUrls 中删除所有撇号,如下所示:
<urlReplacing>
...
<char org="'"></char> <!-- replace ' with nothing -->
...
</urlReplacing>
Note: The contents of the "org" attribute is replaced with the contents of the element, here's another example:
注意:“org”属性的内容替换为元素的内容,这里是另一个例子:
<char org="+">plus</char> <!-- replace + with the word plus -->
回答by Gaurav Balyan
This will work, you just need to change TWO params as given below
这将起作用,您只需要更改下面给出的两个参数
<xsl:with-param name="replace">'</xsl:with-param>
<xsl:with-param name="by" >AnyString</xsl:with-param>
回答by Stephen Denne
Have you tried setting disable-output-escapingto yes for your xsl:value-of element:
您是否尝试将xsl:value-of 元素的disable-output-escaping设置为 yes:
<xsl:value-of disable-output-escaping="yes" select="umbraco.library:NiceUrl($node/@id)"/>
Actually - this is probably the opposite of what you want.
实际上 - 这可能与您想要的相反。
How about wrapping the xsl:value-of in an xsl:text element?
将 xsl:value-of 包装在 xsl:text 元素中怎么样?
<xsl:text><xsl:value-of select="umbraco.library:NiceUrl($node/@id)"/></xsl:text>
Perhaps you should try to translate 'to &apos;
也许你应该试着翻译'成&apos;

