XSLT / XML: Converting an Apostrophe to a String of a Specific Object

I would like to be able to convert the apostrophe to the string ' using XSLT. I have the following XML

 <OrgName>King College</OrgName> 

I would like to be able to output the following:

 <OrgName>King&apos;s College</OrgName> 

I tried a character map, but I don't think this works, since you can replace only one character with a string.

I also tried replacing the function, but I'm not sure what I can do to make sure that the full line actually appears in the output file?

 <xsl:value-of select='replace(OrgName,"&amp;apos;","&amp;apos;")' /> 

In my final decision, I need to be able to replace the entire apostrophe in the text, and not just one node.

+4
source share
2 answers

Using a character map is as simple as this :

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output omit-xml-declaration="yes" indent="yes" use-character-maps="mApos"/> <xsl:character-map name="mApos"> <xsl:output-character character="&apos;" string="&amp;apos;"/> </xsl:character-map> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="text()"> <xsl:copy-of select="." /> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the provided XML document:

 <OrgName>King College</OrgName> 

the desired, correct result is output:

 <OrgName>King&apos;s College</OrgName> 
+3
source

This is only a partial answer:

To replace the character ' you can use the following:

 <xsl:value-of select="replace(xs:string(str), codepoints-to-string(39), '&amp;apos;')"/> 

which requires XSLT / XPath 2.0. Explanation codepoints-to-string returns a string from a sequence of code points, and 39 matches the character ' '

For doing this replacement on every node of your XML ... At the moment, I have no solution: think about it.

In any case, I hope this helps.

0
source

All Articles