microsoft / microsoft/AL

XML API - Replacement of Characters (XML Entities)

Open
#2,966 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement platform-api xml
Dominant language
PowerShell
Stars
881
Forks
285
Avg merge
3d 36m
Merged PRs (30d)
1

Description

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:

  • " --> &quot;
  • < --> &lt;
  • > --> &gt;
  • & --> &amp;

Textnodes:

  • < --> &lt;
  • > --> &gt;
  • & --> &amp;

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the XML API behavior of XmlElement.SetAttribute() and XmlText.Create(), then compare it with the requested XmlEntityReference support. The issue is complete when the AL XML API can emit the required entities without double-escaping, or clearly documents that this output is unsupported.

Written by the indexing model from the issue text.

Assessment

Tech stack
xml
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.