microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
744 stars 245 forks source link

XML API - Replacement of Characters (XML Entities) #2966

Open JoshC94 opened 6 years ago

JoshC94 commented 6 years ago

Hi,

I am currently trying to generate some XML using AL's XML API. While the generation itself works just fine, I am encountering an issue when it comes to format specific requirements.

The receiver of the XML file forces me to replace the chars ', ", <, >, & with their respective XML entities &apos;, &quot;, &lt;, &gt;, &amp;, regardless of their location in the document (Attribute or Textnode value). However XmlElement.SetAttribute() and XmlText.Create() only replace some of those chars in order to keep the document wellformed.

Attributes:

Textnodes:

Example

AL Code:

el := XmlElement.Create('greeting');
el.Add(XmlText.Create('''"Hello<>&'));
el.SetAttribute('lang', '''"en-US<>&');

Actual output:

<greeting lang="'&quot;en-US&lt;&gt;&amp;">
    '"Hello&lt;&gt;&amp;
</greeting>

Required output:

<greeting lang="&apos;&quot;en-US&lt;&gt;&amp;">
    &apos;&quot;Hello&lt;&gt;&amp;
</greeting>

So ' (&apos;) is never replaced, while " (&quot;) is only replaced when used in an attribute. Although this makes perfect sense, I need to replace all of the mentioned chars as per the requirements.

I tried to manually replace the chars with their respective XML entities before adding the text to an attribute / textnode, but, for example, instead of &apos; i got &amp;apos;, because Ampersands are replaced automatically when adding the preprocessed text. In the past (using C/AL and DotNet) we solved this by using XmlEntityReference, but as there currently is no such class in AL, I can't think of a possible solution for my problem.

Can anyone think of a solution for this? Or, if there currently is no way to accomplish what i am trying to do, would it be possible to add XmlEntityReference to AL's XML API?

Thanks in advance.

Kind Regards, Josh

StanislawStempin commented 6 years ago

Would it be feasible to build up the XML using the XML APIs but then before sending the document treat it as a regular string and do the final character replacement?

XmlDocument.WriteTo(outStream);
outStream.WriteText(text);
text.Replace('"', '&quot;');
JoshC94 commented 6 years ago

Hi,

thanks for your Answer. I thought of solving it in a similar way, but I realized I can't just replace every quote as doing so would destroy the XML structure (because attribute delimiters would also be replaced). It would therefore be necessary to find a context sensitive way of post processing, to only replace characters that are part of the actual content of attributes or text nodes.

Would it be possible to include XMLEntityReference (or something similar) into AL if there are no better options?

Josh

TabrezAjaz commented 5 years ago

Would it be feasible to build up the XML using the XML APIs but then before sending the document treat it as a regular string and do the final character replacement?

XmlDocument.WriteTo(outStream);
outStream.WriteText(text);
text.Replace('"', '&quot;');

How to access each XML node one by one in AL? Please provide code if you have for reading nodes and their data one by one...

My XML Content [XmlText] snippet:

https://www.mywebsite.com/ < YourToken>0_YdG/DRSVGCKyy6fx/fdasdY6avs= 05/28/2019 11:50:14 PM 0_i1G4fADRTVRDfdsac4GjD5R5UoOPks

Thanks in Advance!