Improved XML grammar binding and more in Red Hat VS Code XML extension 0.13.0

Improved XML grammar binding and more in Red Hat VS Code XML extension 0.13.0

Following closely on the huge 0.12.0 update, the new Red Hat XML extension for Visual Studio Code (VS Code) 0.13.0 release makes XML editing in VS Code even better. For this release, we focused on making it easier to bind and generate a new XML Schema Definition (XSD) or Document Type Definition (DTD) grammar file from an existing XML file. Other highlights include document link support for xsi:schemaLocation, XML catalog snippets, support for XML catalog path validation, and support for DTD SystemId file path completion.

This article offers a quick review and demonstrates the new features in Red Hat’s XML extension version 0.13.0 for VS Code. For the complete list of changes, see the changelog.

Note: The XML extension for VS Code, version 0.13.0, which consumes the LemMinX XML language server, is available in the Visual Studio Code Marketplace.

Binding and generating XSD and DTD grammars

Developers have previously reported issues with correctly binding an XML file with an XSD or DTD grammar. With the 0.13.0 release, we have added snippets and quick fixes to make this binding easier. The XML extension for VS Code can also generate XSD and DTD grammars from an existing XML file, which provides a great starter setup for new XML files.

The new snippets and quick fixes support two workflows.

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP

Create and bind a grammar for an existing XML file

You have a non-empty XML file, and you wish to create and bind a grammar for it. You can now use a quick fix to bind the existing XML file to a to-be-generated XSD or DTD grammar file, as demonstrated in Figure 1.

An animated demonstration shows the steps in the console to bind and generate a new XSD grammar (file.xsd) to a pre-existing XML file (file.xml).

Figure 1: Use a quick fix to bind and generate a new XSD grammar to an existing XML file.

Create an XML file from scratch

In this case, you are creating an XML file from scratch, and you wish to create and bind a grammar file for it. With the new XML extension update, you can use snippets to create an XML starter template that binds to a grammar file, then use a quick fix to generate the grammar file itself.

In the demo in Figure 2, we use the noNamespaceSchemaLocation snippet to insert the XML starter template, then use a quick fix to create the demo.xsd file with the generated XSD grammar.

An animated demonstration shows how to use insert an XML starter template, use a snippet to bind an XSD grammar, and then use a a quick fix to generate the grammar.

Figure 2: Use a snippet to bind an XSD grammar, then use a quick fix to generate the grammar.

The XML extension for VS Code now includes the following snippet prefixes to help bind a grammar:

Snippet prefix Snippet content
schemaLocation Bind to an XML grammar with xsi:schemaLocation.
noNamespaceSchemaLocation Bind to an XML grammar with xsi:noNamespaceSchemaLocation.
<!DOCTYPE Bind to a DTD grammar with PUBLIC DOCTYPE.
Bind to a DTD grammar with PUBLIC DOCTYPE with a subset.
Bind to a DTD grammar with SYSTEM DOCTYPE.
Bind to a DTD grammar with SYSTEM DOCTYPE with a subset.

Let’s take a closer look at the schema generator and how it works for various scenarios.

Characteristics of the schema generator

When generating an XSD or DTD grammar based on pre-existing XML content, the generator accounts for the element and attribute context. Consider the following examples.

Required attribute: Every element instance contains a certain attribute

If the XML content contains a certain attribute in each instance of a particular element, the grammar generator will consider this attribute to be required.

Here is the initial XML content:

<root>
	<item attr="value" />
	<item attr="value" />
	<item attr="value" />
</root>

Here is the generated XSD attribute:

<xs:attribute name="attr" use="required" />

Here is the generated DTD attribute:

<!ATTLIST item attr NMTOKEN #REQUIRED>

Fixed attribute value: The same attribute value occurs five or more times

If the XML content contains an attribute with the same value appearing five or more times, the grammar generator will consider the value to be a fixed value.

Here is the XML content:

<root>
	<item attr="A" />
	<item attr="A" />
	<item attr="A" />
	<item attr="A" />
	<item attr="A" />
</root>

Here is the generated XSD attribute:

<xs:attribute name="attr" use="required" fixed="A" />

Here is the generated DTD attribute:

<!ATTLIST item attr NMTOKEN #FIXED "A">

ID attribute value: There are 10 or more unique attribute values

If every instance of an element contains an attribute with at least 10 unique attribute values, the grammar generator will consider this attribute to be an ID specifier.

Here is the XML content:

<root>
	<item attr="id1" />
	<item attr="id2" />
	<item attr="id3" />
	<item attr="id4" />
	<item attr="id5" />
	<item attr="id6" />
	<item attr="id7" />
	<item attr="id8" />
	<item attr="id9" />
	<item attr="id10" />
</root>

Here is the generated XSD attribute:

<xs:attribute name="attr" type="xs:ID" use="required" />

Here is the generated DTD attribute:

<!ATTLIST item attr ID #REQUIRED>

Attribute enum value: The same set of attribute values is used repeatedly

If the same set of distinct attribute values is used repeatedly, the grammar generator will consider these values to be enum values, therefore enforcing the possible values to be from the set. Enum values will only be generated if the attribute occurs at least 10 times, and only if the number of occurrences divided by the number of distinct values is greater than or equal to three.

Here is the XML content:

<root>
	<item attr="A" />
	<item attr="A" />
	<item attr="A" />
	<item attr="A" />
	<item attr="A" />
	<item attr="B" />
	<item attr="B" />
	<item attr="B" />
	<item attr="B" />
	<item attr="B" />
</root>
<!-- Attribute ‘attr’ occurs 10 times, with 2 distinct values -->
<!-- (10 / 2) = 5 >= 3 -->

Here is the generated XSD attribute:

<xs:attribute name="attr" use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="A" />
      <xs:enumeration value="B" />
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

Here is the generated DTD attribute:

<!ATTLIST item attr (A|B) #REQUIRED>

Document link for the schema location

The XML schema file specified in the xsi:schemaLocation is now a document link, meaning that you can Ctrl+click on the file name to quickly open it within VS Code. The demo in Figure 3 shows this action on the xsi:schemaLocation document link.

An animated demo shows what happpens when you Ctrl+click the document link for xsi:schemaLocation.

Figure 3: Ctrl+clicking on note.xsd opens the file in a new editor tab.

XML catalog snippets

We have added three catalog snippets to assist you in creating new catalogs from scratch:

  Snippet prefix   Snippet content
<catalog A new catalog without XSD or DTD.
A new catalog bound using XSD.
A new catalog bound using DTD.

The demo in Figure 4 shows these catalog snippets being displayed in an empty file.

An animated demo shows the new catalog snippets in an empty file.

Figure 4: The new catalog snippets displayed in an empty file.

XML catalog path validation

The XML extension for VS Code now verifies whether a path provided in the xml.catalogs setting refers to an existing file. If one or more file paths do not exist, a notification lists the invalid path(s). At that point, you are provided a convenient button to navigate to the xml.catalogs setting within the settings editor. (The button is especially useful when the settings editor is not open.)

In Figure 5, the first entry is an existing file, while the second and third are not. The XML extension issues an error notification for each invalid file path.

An animated demo shows one entry for an existing file and two that are invalid. When the XML extension detects the invalid file paths, it issues error notifications on those paths.

Figure 5: The XML extension issues error notifications for invalid file paths.

DTD SystemId file path completion

File paths now appear as completion options within the DTD SystemId property. As demonstrated in Figure 6, the completion options appear for both absolute and relative paths.

An animated demo shows a DTD SystemId filepath completion.

Figure 6: The XML extension completes a DTD SystemId file path.

Moving forward

This wraps up the highlights of the Red Hat XML extension for VS Code 0.13.0 release. For future releases, we plan to provide more robust and configurable formatting support for XML documents. As always, please feel free to open a GitHub issue or feature request, and let us know what we should address in the next release. Thank you for reading and stay tuned for more features and improvements!

Further information

Share