0

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
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • 1
    Please edit your question and add an example of the input XML and the **exact** output you expect to get - see: [mcve]. – michael.hor257k Feb 15 '23 at 05:51
  • 1
    Probably unrelated to your question, but `translate(translate(translate(translate(InstructionGrouping/Payer/LegalEntityName,'Ñ','N'),'ñ','n'),'Ç','C'),'ç','c')"/>` can be expressed shortly as `translate(InstructionGrouping/Payer/LegalEntityName, 'ÑñÇç', 'NnCc')`. – y.arazim Feb 15 '23 at 09:40
  • Thanks @y.arazim - I would make this suggested change. – Saurabh Maheshwari Feb 15 '23 at 09:55
  • @michael.hor257k - I have edited the question with a sample xml value and what is expected. Pls advise. – Saurabh Maheshwari Feb 15 '23 at 10:00
  • 4
    If that's really what your input looks like, then it is not XML and you won't be able to process it using XSLT at all. An XML document cannot contain an unescaped ampersand character. I am not sure if your output should be text or another XML - but at this point it doesn't matter. – michael.hor257k Feb 15 '23 at 10:08

2 Answers2

0

The replacements you want to perform are not done with XSLT, but are a prerequisite when creating the XML document that is the input for the XSL transformation, as michael.hor257k said.

They are performed automatically, for example, if you construct the XML document with Javascript running in the browser:

var xml = new DOMParser().parseFromString("<LegalEntityName/>","text/xml");
xml.documentElement.textContent = "Legal & entity Beach & Resort LLC";
console.log(new XMLSerializer().serializeToString(xml));

outputs

'<LegalEntityName>Legal &amp; entity Beach &amp; Resort LLC</LegalEntityName>'
Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
0

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164