XSLT - 用输出中的转义文本替换撇号

时间:2021-02-27 14:05:26

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

我正在编写一个XSLT模板,需要为xml Sitemap输出有效的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 &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有时会让我发疯。

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

对技术的任何想法? (假设我可以找到XSLT 1.0模板和函数的方法)

4 个解决方案

#1


9  

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

所以你有'输入,但你需要字符串 在你的输出?

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

在您的XSL文件中,替换'使用&',使用此查找/替换实现(除非您使用的是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="&apos;"/>
    <xsl:with-param name="by" select="&amp;apos;"/>
  </xsl:call-template>
</loc>

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

问题是'被XSL解释为'。 &安培;者;将被解释为'。

#2


1  

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 -->

#3


0  

Have you tried setting disable-output-escaping to 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:text元素中包装xsl:value-of?

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

Perhaps you should try to translate ' to &amp;apos;

也许你应该尝试翻译'to&';

#4


0  

This will work, you just need to change TWO params as given below

这将有效,您只需要更改下面给出的两个参数

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

#1


9  

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

所以你有'输入,但你需要字符串 在你的输出?

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

在您的XSL文件中,替换'使用&',使用此查找/替换实现(除非您使用的是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="&apos;"/>
    <xsl:with-param name="by" select="&amp;apos;"/>
  </xsl:call-template>
</loc>

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

问题是'被XSL解释为'。 &安培;者;将被解释为'。

#2


1  

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 -->

#3


0  

Have you tried setting disable-output-escaping to 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:text元素中包装xsl:value-of?

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

Perhaps you should try to translate ' to &amp;apos;

也许你应该尝试翻译'to&';

#4


0  

This will work, you just need to change TWO params as given below

这将有效,您只需要更改下面给出的两个参数

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