xmltools icon indicating copy to clipboard operation
xmltools copied to clipboard

Convert selection XML to text (<> => &lt;&&gt;)

Open Sopor opened this issue 5 years ago • 5 comments

When i convert text to XML by using the Convert selection XML to text (<> => &lt;&&gt;) it seems to not do anything for the apostrophe (single quote) even if that should be &apos;

&lt;
&gt;
&amp;
'
&quot;

Sopor avatar Dec 31 '19 19:12 Sopor

Btw, is this really correct?

image

When i want to convert text to XML i need to use Convert selection XML to text and not Convert selection text to XML. The (<> => &lt;&&gt;) seems to be correct, though.

Sopor avatar Dec 31 '19 19:12 Sopor

I tried with v. 3.0.4.3 and it works for me with &amp, though I would expect a slightly different behavior

Expected behaviour:

Function to de-/encode special chars within selected tags (which are not marked as CDATA):

chars are replaced with ' " & < > and vice versa

It also encodes tags though if selected, so I would expect it to encode only the content of a tag (Use Case: Usually I would like to select whole parts of a file and then "encode" content of all non-cdata tags)

github-canni avatar Jun 15 '20 12:06 github-canni

The de/encode is a simple text replacement algo. It has no notion of XML itself other than that it has to replace certain texts.

Lets explore the use cases:

Use case 1: I have text, which I need to insert into XML.. however it might have illegal/reserved characters that will mess up the XML format. Hence, I select the text that I inserted and run 'Convert selection XML to text'... now my text is valid XML text.

Use case 2: I have some text node in XML that is encoded. Without using a full XML parser, I select this text and then run 'Convert selection text to XML. Now my selection is regular text and I can copy/paste it somewhere.

Use case 3: I copy/pasted some & encoded text which was part of a cookie/URL-query/etc and want to decode it to XML. I select all and run 'Convert selection text to XML'. I will now have an XML document that I can work with.

Neither of these use cases require CDATA knowledge. Can you maybe describe your use case in detail?

LetMeSleepAlready avatar Aug 29 '20 18:08 LetMeSleepAlready

As described in a Python article dedicated to XML escaping [1], there are two separate cases to consider when escaping: CDATA and attributes. In the former case, there's no need to handle quotes; in the latter, quotes should be treated.

It seems logical to have dedicated functions here for the two cases; or maybe be smart to check the context (but then it would be not very flexible for cases where context in not clear, as in a temporary file without proper XML structure used for intermediate work). Currently (in version 3.0.4.3) the function Escape characters in selection (<> → &lt;&gt;) escapes everything including quotes in all positions.

[1] https://wiki.python.org/moin/EscapingXml

mikekaganski avatar Nov 05 '20 07:11 mikekaganski

For information, the xmltools options dialog let you choose which character must be handled by text/xml conversion functions:

2020-11-05_08h16_03

morbac avatar Nov 05 '20 07:11 morbac