Skip to content

Latest commit

 

History

History
150 lines (99 loc) · 5.29 KB

RelaxNGFeatures.md

File metadata and controls

150 lines (99 loc) · 5.29 KB

RelaxNG features

vscode-xml provides validation, completion, hover in XML file based on RelaxNG schemas since 0.22.0:

RelaxNG support

It supports both:

XML Validated with RelaxNG

You can associate your XML file with RelaxNG grammar (file or URL) using :

  • an xml-model processing instruction
<?xml-model href="addressBook.rng" type="application/xml"?>
<addressBook>   
  ...
</addressBook> 
  • an XML catalog using the same method as XSD
  • a file association using the same method as XSD

RelaxNG grammar

vscode-xml provides support for RelaxNG grammars:

RelaxNG grammar XML Syntax Overview

RelaxNG Compact Syntax Overview

RelaxNG grammar XML Syntax

vscode-xml provides support for reference between define/@name and ref/@name with codelens, definition, completion, highlighting, references, rename:

RelaxNG XML Syntax Define support

It also provides support for include/@href and externalRef/@href attributes to navigate easily to the referenced rng grammar file:

RelaxNG XML Syntax Href support

Validation

XML validation based on RelaxNG (rng, rnc) is supported:

RelaxNG Validation

Completion

XML completion based on RelaxNG (rng, rnc) is supported. The completion for rng can show the documentation:

RelaxNG Completion And Documentation

Hover

Hover based on RelaxNG rng can show the documentation:

RelaxNG Hover And Documentation

Go to Type Definition

From the XML document you can go to the type definition to navigate to the element/attribute declaration for both rnc and rng.

To do this, select an XML element/attribute and use the contextual menu Go to Type Definition:

Go to Type Definition Menu

When you click on this menu item, VS Code will open the rng or rnc grammar file and place the cursor on the proper element/attribute declaration:

Go to Type Definition result

How to start?

Using snippets

You can use snippets to quickly create:

  • an XML file associated with a RelaxNG grammar
  • a RelaxNG XML file

Here's a demo:

RelaxNG snippets support

Creating XML, rng, rnc files

You can create a RelaxNG grammar that uses the XML syntax by making a file with the extension .rng. For instance, here is a file addressBook.rng that defines a RelaxNG grammar:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
    <start>
        <element name="addressBook">
            <zeroOrMore>
                <element name="card">
                    <ref name="cardContent"/>
                </element>
            </zeroOrMore>
        </element>
    </start>
    <define name="cardContent">
        <element name="name">
            <text/>
        </element>
        <element name="email">
            <text/>
        </element>
    </define>
</grammar>

You can create a RelaxNG grammar that uses the compact syntax by making a file with the extension .rnc. For instance, here is a file addressBook.rnc that defines the same RelaxNG grammar as addressBook.rng:

element addressBook {
  element card {
    element name { text },
    element email { text }
  }*
} 

The following file, addressBook.xml, references addressBook.rng using the xml-model processing instruction. vscode-xml will provide the features based on the grammar shown in the demo at the beginning of this page when you open addressBook.xml.

<?xml-model href="addressBook.rng" type="application/xml"?>
<addressBook>   
  <card>
    <name></name>
    <email></email>
  </card>
</addressBook> 

Generate RELAX NG from XML

When an unbound XML file is open, a RELAX NG schema can be generated from the opening tag in the XML file.

See: Generate RELAX NG from XML.