XSLT - 在输出转义文本替换撇号(XSLT - Replace apostrophe with

2019-06-25 11:40发布

我正在写需要输出有效的XML文件的XML网站地图的XSLT模板。

<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

我要逃离“来&apos; 对于谷歌地图。 不幸的是每次我试图尝试将字符串' &apos; “就好像它是‘’”这是无效的 - 沮丧。 XSLT可以把我有时会发疯。

任何想法的技术? (假设我能找到周围XSLT我的路1.0模板和功能)

Answer 1:

所以,你必须'在你的输入,但你需要的字符串&nbsp; 在您的输出?

在您的XSL文件,替代&apos;&amp;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>

这样调用它:

<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解释为'&amp;apos; 将被解释为&apos;



Answer 2:

最简单的方法来从您的网址删除不需要的角色是改变规则一把umbraco使用时,它产生的NiceUrl。

编辑config / umbracoSettings.config

添加规则,从像这样NiceUrls删除所有的撇号:

<urlReplacing>
    ...
    <char org="'"></char>     <!-- replace ' with nothing -->
    ...
</urlReplacing>

注意:“组织”属性的内容被替换为元素的内容,这里是另一个例子:

<char org="+">plus</char> <!-- replace + with the word plus -->


Answer 3:

你有没有尝试设置禁用输出转义为yes您的xsl:value-of元素:

<xsl:value-of disable-output-escaping="yes" select="umbraco.library:NiceUrl($node/@id)"/>

其实 - 这大概是你想的正好相反。

如何包装了xsl:value-of的在一个xsl:text元素?

<xsl:text><xsl:value-of select="umbraco.library:NiceUrl($node/@id)"/></xsl:text>

也许你应该试着翻译'&amp;apos;



Answer 4:

这将工作,你只需要改变两个参数如下给出

<xsl:with-param name="replace">&apos;</xsl:with-param>
<xsl:with-param name="by" >AnyString</xsl:with-param>


文章来源: XSLT - Replace apostrophe with escaped text in output
标签: xml xslt sitemap