XSLT - Replace apostrophe with escaped text in output

女生的网名这么多〃 提交于 2019-11-30 23:41:13
Welbog

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

In your XSL file, replace ' with ', 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>

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

The simple way to remove unwanted characters from your URL is to change the rules umbraco uses when it generates the NiceUrl.

Edit the config/umbracoSettings.config

add a rule to remove all apostrophes from NiceUrls like so:

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

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

Have you tried setting disable-output-escaping to yes for your xsl:value-of element:

<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 select="umbraco.library:NiceUrl($node/@id)"/></xsl:text>

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

Gaurav Balyan

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!