Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

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

July 8, 2020
David Kwon
Related topics:
Developer ToolsIDEsJava
Related products:
Developer Tools

Share:

    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.

    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 to an XML template, then use 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.
    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.
    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.
    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

    • The XML extension for VS Code, version 0.13.0, is now available in the Visual Studio Code Marketplace.
    • Explore the vscode-xml extension and the underlying XML language server, LemMinX on GitHub.
    • Visit the XML extension for VS Code changelog.
    • Open a GitHub issue to let us know about bugs or new feature requests.
    Last updated: November 8, 2023

    Recent Posts

    • How Trilio secures OpenShift virtual machines and containers

    • How to implement observability with Node.js and Llama Stack

    • How to encrypt RHEL images for Azure confidential VMs

    • How to manage RHEL virtual machines with Podman Desktop

    • Speech-to-text with Whisper and Red Hat AI Inference Server

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue