lemminx icon indicating copy to clipboard operation
lemminx copied to clipboard

Smart & quick XML generator

Open angelozerr opened this issue 5 years ago • 6 comments

In vscode-xml, we have a lot of issues from users who don't know how to bind XML with XML Schema/ DTD. The idea of this issue is to provide:

  • a smart XML generator with different usecases:

    • XML Schema / DTD grammar doesn't exists. The idea is to write the XML and generate the XML Schema/DTD based on this XML.
    • XML Schema / DTD grammar exists.The idea is to generate the XML from this grammar.
  • a quick XML generator means that with one or two actions we should bind a XML with a grammar with the generation.

To explain more the idea, let's describe several usecases by taking sample from https://www.w3schools.com/xml/schema_howto.asp.

Generate XML bound with DTD without existing of DTD file

In this usecase, the idea is to generate the XML:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
</note>

and the DTD file note.dtd:

<!ELEMENT to (#PCDATA)>

with one completion and one code action.

  • create an empty note.xml file
  • type note in the editor and open completion
  • the completion should provide a snippet like Generate XML bind with external DTD, when completion is applied, the following content should be generated:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
</note>
  • the note.dtd is highlighted as an error because DTD doesn't exist. A code action should generate this DTD file and fill the content of the file with:

<!ELEMENT to (#PCDATA)>

Generate XML bound to DTD with existing DTD file

Generate XML bound to XSD without existing XSD file, with xsi:noSchemaNamespaceLocation

Generate XML bound to XSD with existing XSD file, with xsi:noSchemaNamespaceLocation

Generate XML bound to XSD without existing XSD file, with xsi:schemaLocation

Generate XML bound to XSD with existing XSD file with, xsi:schemaLocation

angelozerr avatar Apr 29 '20 08:04 angelozerr

This sounds like a really useful feature.

I'm just wondering right now: If I got the description right, it sounds like it would be done via code completion. This means that the user first has to create a new XML file, then type something, then trigger the code completion.
Wouldn't it make more sense to have that functionality as a command in VS Code:

  • hit Ctrl + Shift + P
  • chose the command Generate New XML File
  • then it checks if an empty XML file is open in the editor
    • if so, the file name of this file is used
    • if not, an InputBox opens, where the user can enter a file name (the file is then created)
  • then a couple of QuickPick boxes are suggested: XML bound to existing DTD file, XML bound to non-existing DTD file, etc.
  • then it reacts accordingly. E.g.:
    • when an existing DTD/XSD file is used, it checks if a file with the same name and according extension (i.e. for note.xml it checks for note.dtd) is there; if not, it opens a FileDialog.
    • when DTD/XSD is non-existing, it offers to create the same file name with other extension (note.xsd), or a standard name (schema.xsd) or opening a SaveAs-Dialog
  • finally, it adds all the necessary content to the XML file

No idea if that's a good idea. But I think it might emulate the behavior of other editors of the type "create new XML file", and therefor be familiar to users.
And I think it would be faster than having to create a file first and then triggering the completion.

This mechanic could even be extended to things like "Create New TEI File" or "Create new DocBook" where it automatically adds the default document structure, the schema, potentially stylesheet information, etc.

BalduinLandolt avatar Jul 03 '20 16:07 BalduinLandolt

This sounds like a really useful feature.

We have implemented this feature with Code action. have you tried it?

angelozerr avatar Jul 03 '20 16:07 angelozerr

We have implemented this feature with Code action. have you tried it?

woops... not yet. Got to try it out!
In that case: cool! never mind. :)

BalduinLandolt avatar Jul 03 '20 16:07 BalduinLandolt

Here several demos:

  • XML2XSDDemo

  • XML2DTDDemo

  • NoGrammarConstraintsDemo

angelozerr avatar Jul 03 '20 17:07 angelozerr

But any feedback are welcome!

angelozerr avatar Jul 03 '20 17:07 angelozerr

very cool indeed.

BalduinLandolt avatar Jul 03 '20 17:07 BalduinLandolt