I wish to generate a generic code for parsing special characters in xslt

早过忘川 提交于 2019-12-13 04:26:15

问题


I wish to generate a generic code for parsing special characters in xslt. I wish to replace all special characters found in any node of the xml document with symbols of my choice.

eg special characters= "&#174"; also" &#169"; etc

I have tried something like follows but nothing happens.

<xsl:template match="*">
    <xsl:choose>
        <xsl:when test="contains(.,'&amp;ndash;')">
            <xsl:value-of select="replace(.,'&amp;ndash;','-')" />
        </xsl:when>
    </xsl:choose>
</xsl:template>

the function of character output is:

<xsl:output-character character="&#160;" string="&amp;nbsp;" />

I wish to do the opposite i.e replace string with character(replace &amp;nbsp; with &#160; )


回答1:


If XSLT 2.0 is at your disposal, use character maps for this. You can find the relevant piece of the specification here.

Define the usage of a character map as a top-level element of your stylesheet:

<xsl:output use-character-maps="math-ops"/>

Then, determine which characters you'd like to replace with a string of your choice during serialization. A character map consists of output-character elements where the @character attribute means the character you wish to replace and @string its replacement.

The following example ensures that mathematical operators are serialized correctly (that is, for simplicity it outputs capital letters).

<xsl:character-map name="math-ops">
    <xsl:output-character character="&#x03A3;" string="SIGMA HERE"/><!--Unicode Character 'GREEK CAPITAL LETTER SIGMA' (U+03A3)-->
    <xsl:output-character character="&#x03A0;" string="PI HERE"/><!--Unicode Character 'GREEK CAPITAL LETTER PI' (U+03A0)-->
    <xsl:output-character character="&#x2713;" string="CHECK MARK"/><!--Unicode Character 'CHECK MARK' (U+2713)-->
</xsl:character-map>

Note that this only has an effect on the actual serialization, not on output you generate with xsl:result-document.

To also apply character-maps to this output, specify the use-character-maps attribute on the relevant result-document elements:

<xsl:result-document href="helloworld.xml" use-character-maps="math-ops">
  <hello>
    <world/>
  </hello>
</xsl:result-document>


来源:https://stackoverflow.com/questions/21328857/i-wish-to-generate-a-generic-code-for-parsing-special-characters-in-xslt

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