How can I translate to apostrophe in xslt
Relevant parts of the code:
<xsl:variable name="apos">'</xsl:variable> <xsl:variable name="and" select='"'"' /> <xsl:value-of select="translate(products_name/node(),$and,$apos)"/> I think this should be a simple thing and that the above code should work, but it does not affect the result at all.
(I used variables because names cannot begin in an ampersand, and using only an apostrophe causes a compilation error.)
I checked the code to make sure the translation works using strings, and there are no errors there.
Any help would be greatly appreciated.
You're on the right track, but not there: Your problem is that XSL is a language that itself is written using XML. For all XML languages, the parser automatically decodes XML objects. After that, the XSLT engine appears.
As a result, the XSLT engine does not and cannot distinguish whether you are written ' or ' - same. For your problem, this has two effects:
You must use a variable containing an apostrophe - this is because the apostrophe itself is reserved for string literals in expressions that may contain functions. Even for
<xsl:value-of select="translate(products_name/node(),$and,''')"/>XML parser converts the object to an apostrophe, that is,<xsl:value-of select="translate(products_name/node(),$and,''')"/>You need to avoid the ampersand used in the string you are looking for: for the XSL engine, the variable "and" contains the value
', that is, you replace the apostrophe with an apostrophe.
Working solution:
<xsl:variable name="apos">'</xsl:variable> <xsl:value-of select='translate(text(), "&#039;", $apos)'/> Technically, there is no difference in any XML between ' , ' and ' , these are different ways of representing exactly the same thing. Therefore, the translate call should not do anything.
It depends on how you convert it, where is the result (attribute value or element?), And how the output is serialized into text, but your problem is not related to your XSLT.