Xsl: character map for replacing special characters

For an element with a value:

<xml_element>Distrib = SU &amp; Prem &amp;lt;&amp;gt; 0</xml_element> 

I need to turn &amp;lt; or &amp;gt; in &lt; or &gt; because the top-down application requires it in this format throughout the XML document. I will need this for quotes and apostrophes. I am trying to create a character map in XSLT 2.0.

 <xsl:character-map name="specialchar"> <xsl:output-character character="&apos;" string="&amp;apos;" /> <xsl:output-character character="&quot;" string="&amp;quot;" /> <xsl:output-character character="&gt;" string="&amp;gt;" /> </xsl:character-map> 
+4
source share
1 answer

The <xsl:character-map> command can be used to serialize a single character for any line. However, this problem requires more than one character (an ampersand followed by another character that needs to be replaced).

<xsl:character-map> cannot be used to solve such problems.

Here is a solution to this problem using the XPath 2.0 replace() function:

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="text()"> <xsl:value-of select= 'replace( replace( replace(., "&amp;lt;", "&lt;"), "&amp;gt;", "&gt;" ), "&amp;apos;", "&apos;" ) '/> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the following XML document :

 <xml_element>Distrib = SU &amp; &amp;apos;Prem &amp;lt;&amp;gt; 0</xml_element> 

the desired result is obtained :

 <xml_element>Distrib = SU &amp; 'Prem &lt;&gt; 0</xml_element> 
+7
source

Source: https://habr.com/ru/post/1315121/


All Articles