alek-sys / sublimetext_indentxml

Plugin for Sublime Text editor for reindenting XML and JSON files
MIT License
534 stars 136 forks source link

TypeError: String required when indenting xml with namespaces #106

Open kanngard opened 6 years ago

kanngard commented 6 years ago

When trying to indent XML including namespaces in Sublime 3 Build 3143, I get the following in the console:

Traceback (most recent call last):
  File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 818, in run_
    return self.run(edit)
  File "indentxml in C:\Users\<username>\AppData\Roaming\Sublime Text 3\Installed Packages\Indent XML.sublime-package", line 47, in run
  File "C:\Program Files\Sublime Text 3\sublime.py", line 823, in replace
    sublime_api.view_replace(self.view_id, edit.edit_token, r, text)
TypeError: String required

Example of XML that generates the above error:

<xsd:element>
<xsd:annotation><xsd:documentation>
My documentation
</xsd:documentation></xsd:annotation>
</xsd:element>
Xjs commented 6 years ago

+1

raunn commented 6 years ago

Fix for this is critical - most of the XML I work with includes namespaces. SAML metadata fails due to tags like "<md:EntityDescriptor ...".

alek-sys commented 6 years ago

Just to clarify - the "String required" error is misleading and I'm not sure where is it coming from. The real problem is that the piece of XML you are trying to format has to be a valid XML, i.e. it should define all the namespaces.

For example,

<xsd:element>
<xsd:annotation><xsd:documentation>
My documentation
</xsd:documentation></xsd:annotation>
</xsd:element>

technically is not a valid XML - xsd namespace is not defined. To make it 'parseable' and so 'indentable' namespaces need to be added:

<xsd:element xmlns:xsd="https://tempuri.org">
<xsd:annotation><xsd:documentation>
My documentation
</xsd:documentation></xsd:annotation>
</xsd:element>

So this problem can appear when you are trying to indent not a whole document, but part of it (or a selection).

I understand this explanation doesn't quite help to solve the problem, I just want to show that fixing this is tricky (namespaces need to be 'mocked' for indentiting or something). Also this is not in the list of my priorities at the moment, sorry. However I'll try to see if there is anything I can do quickly.

raunn commented 6 years ago

That makes sense, thank you for the reply.

In SAML metadata files, the namespace is always inferred, and I’ve never seen it explicitly declared, so I’ll wait for now. It might be nice to have a non-strict style for your indent plugin, where it only looks for matching open/close or self-closing tags and ignores unknown / undefined tags names.

Regardless, thank you for your work on this.

Regards,

Raun

From: Alexey Nesterov [mailto:notifications@github.com] Sent: Monday, January 29, 2018 1:57 AM To: alek-sys/sublimetext_indentxml sublimetext_indentxml@noreply.github.com Cc: Raun Nohavitza raun.nohavitza@centrify.com; Comment comment@noreply.github.com Subject: Re: [alek-sys/sublimetext_indentxml] TypeError: String required when indenting xml with namespaces (#106)

Just to clarify - the "String required" error is misleading and I'm not sure where is it coming from. The real problem is that the piece of XML you are trying to format has to be a valid XML, i.e. it should define all the namespaces.

For example,

My documentation technically is not a valid XML - xsd namespace is not defined. To make it 'parseable' and so 'indentable' namespaces need to be added: My documentation So this problem can appear when you are trying to indent not a whole document, but part of it (or a selection). I understand this explanation doesn't quite help to solve the problem, I just want to show that fixing this is tricky (namespaces need to be 'mocked' for indentiting or something). Also this is not in the list of my priorities at the moment, sorry. However I'll try to see if there is anything I can do quickly. — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
kanngard commented 6 years ago

Thanks for clarifying. I often work on XML snippets and can not have the namespaces declared. So instead of working on XML files with thousands of lines, I only have a few lines to minimize the clutter and workload in Sublime. I see that this is actually a duplicate of #105 Found a plugin that handles my requirements, IndentX, https://github.com/socsieng/IndentX