|
| 1 | +# RelaxNG features |
| 2 | + |
| 3 | +[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides validation, completion, hover in XML file based on [RelaxNG schemas](https://relaxng.org/) since `0.22.0`: |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +It supports both: |
| 8 | + |
| 9 | + * [RelaxNG XML syntax (*.rng files)](https://relaxng.org/tutorial-20011203.html). |
| 10 | + * [RelaxNG Compact syntax (*.rnc files)](https://relaxng.org/compact-tutorial-20030326.html). |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +You can associate your XML file with `RelaxNG grammar` (`file` or `URL`) using : |
| 15 | + |
| 16 | + * an `xml-model` processing instruction |
| 17 | + |
| 18 | +```xml |
| 19 | +<?xml-model href="addressBook.rng" type="application/xml"?> |
| 20 | +<addressBook> |
| 21 | + ... |
| 22 | +</addressBook> |
| 23 | +``` |
| 24 | + |
| 25 | + * an `XML catalog` using the same method as [XSD](Validation#XML-catalog-with-XSD) |
| 26 | + * a `file association` using the same method as [XSD](Validation#XML-file-association-with-xsd) |
| 27 | + |
| 28 | +## RelaxNG grammar |
| 29 | + |
| 30 | +[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides support for `RelaxNG grammars`: |
| 31 | + |
| 32 | + * for [RelaxNG XML syntax (*.rng files)](https://relaxng.org/tutorial-20011203.html), it provides the same support as XSD/DTD grammars (completion, hover, validation, etc). |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + * for [RelaxNG Compact syntax (*.rnc files)](https://relaxng.org/compact-tutorial-20030326.html) it provides only syntax coloration. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +# Validation |
| 41 | + |
| 42 | +XML validation based on RelaxNG (rng, rnc) is supported: |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +# Completion |
| 47 | + |
| 48 | +XML completion based on RelaxNG (rng, rnc) is supported. The completion for rng can show the documentation: |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +# Hover |
| 53 | + |
| 54 | +Hover based on RelaxNG rng can show the documentation: |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +# Go to Type Definition |
| 59 | + |
| 60 | +From the XML document you can go to the type definition to navigate to the element/attribute declaration for both `rnc` and `rng`. |
| 61 | + |
| 62 | +To do this, select an XML element/attribute and use the contextual menu `Go to Type Definition`: |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +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: |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +# How to start? |
| 71 | + |
| 72 | +## Using snippets |
| 73 | + |
| 74 | +You can use snippets to quickly create: |
| 75 | + |
| 76 | + * an XML file associated with a RelaxNG grammar |
| 77 | + * a RelaxNG XML file |
| 78 | + |
| 79 | +Here's a demo: |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +## Creating XML, rng, rnc files |
| 84 | + |
| 85 | +You can create a RelaxNG grammar that uses the XML syntax by making a file with the extension `.rng`. |
| 86 | +For instance, here is a file `addressBook.rng` that defines a RelaxNG grammar: |
| 87 | + |
| 88 | +```xml |
| 89 | +<?xml version="1.0" encoding="UTF-8"?> |
| 90 | +<grammar xmlns="http://relaxng.org/ns/structure/1.0"> |
| 91 | + <start> |
| 92 | + <element name="addressBook"> |
| 93 | + <zeroOrMore> |
| 94 | + <element name="card"> |
| 95 | + <ref name="cardContent"/> |
| 96 | + </element> |
| 97 | + </zeroOrMore> |
| 98 | + </element> |
| 99 | + </start> |
| 100 | + <define name="cardContent"> |
| 101 | + <element name="name"> |
| 102 | + <text/> |
| 103 | + </element> |
| 104 | + <element name="email"> |
| 105 | + <text/> |
| 106 | + </element> |
| 107 | + </define> |
| 108 | +</grammar> |
| 109 | +``` |
| 110 | + |
| 111 | +You can create a RelaxNG grammar that uses the [compact syntax](https://relaxng.org/compact-tutorial-20030326.html) by making a file with the extension `.rnc`. |
| 112 | +For instance, here is a file `addressBook.rnc` that defines the same RelaxNG grammar as `addressBook.rng`: |
| 113 | + |
| 114 | +```rnc |
| 115 | +element addressBook { |
| 116 | + element card { |
| 117 | + element name { text }, |
| 118 | + element email { text } |
| 119 | + }* |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +The following file, `addressBook.xml`, references `addressBook.rng` using the `xml-model` processing instruction. |
| 124 | +[vscode-xml](https://github.com/redhat-developer/vscode-xml) will provide the features based on the grammar shown in [the demo at the beginning of this page](#relaxng-support) when you open `addressBook.xml`. |
| 125 | + |
| 126 | +```xml |
| 127 | +<?xml-model href="addressBook.rng" type="application/xml"?> |
| 128 | +<addressBook> |
| 129 | + <card> |
| 130 | + <name></name> |
| 131 | + <email></email> |
| 132 | + </card> |
| 133 | +</addressBook> |
| 134 | +``` |
0 commit comments