Search code examples
xmlxsltreplacexslt-1.0translate

Replacing characters in XML


I am working on XSLT v1.0 and I have to replace these 5 key words with their ASCII values. Using replace function, I am able to replace other characters like Ñ, Ç but I am not able to replace

  • & (ampersand) with &
  • < (less than) with &lt;
  • > (greater than) with &gt;
  • “ (double quotation marks) with &quot;
  • ' (apostrophe) with &apos;

Working statement

<xsl:value-of select="translate(translate(translate(translate(InstructionGrouping/Payer/LegalEntityName,'Ñ','N'),'ñ','n'),'Ç','C'),'ç','c')"/>

Not working

<xsl:value-of select="translate(translate(translate(translate(translate(InstructionGrouping/Payer/LegalEntityName,'Ñ','N'),'ñ','n'),'Ç','C'),'ç','c'),'&','&amp;')"/>

Input XML would be something like this -->

<LegalEntityName>Legal & entity Beach & Resort LLC</LegalEntityName>

Expected Output -->

Legal &amp; entity Beach &amp; Resort LLC

Solution

  • You can't use XSLT to convert ill-formed XML into well-formed XML: the input to an XSLT processor has to be well-formed XML.

    Ideally you should never need to do this job, it's the job of any program generating XML to make sure it is well-formed. But if you have no other options, you will need to use non-XML tools for processing non-XML input.