|
| 1 | +# Binding with Grammar |
| 2 | + |
| 3 | +[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides support for automatic XML schema/grammar binding through generation and/or using an existing `.xsd/.dtd` file. |
| 4 | + |
| 5 | +[Validation with XSD grammar](Validation.md#validation-with-xsd-grammar) and [Validation with DTD grammar](Validation.md#validation-with-dtd-grammar) provide more information on manual binding of an XML document. |
| 6 | + |
| 7 | +In the following sections, we would like to bind the XML document `foo.xml`: |
| 8 | + |
| 9 | +```xml |
| 10 | +<foo> |
| 11 | + <bar /> |
| 12 | +</foo> |
| 13 | +``` |
| 14 | + |
| 15 | +with an associated grammar file. |
| 16 | + |
| 17 | +## Binding with Existing Grammar |
| 18 | + |
| 19 | +Consider an XSD document, `foo.xsd` file (in the same directory as foo.xml) defined as follows : |
| 20 | + |
| 21 | +```xsd |
| 22 | +<?xml version="1.0" encoding="UTF-8"?> |
| 23 | +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
| 24 | + <xs:element name="foo"> |
| 25 | + <xs:complexType> |
| 26 | + <xs:sequence> |
| 27 | + <xs:element name="bar" /> |
| 28 | + </xs:sequence> |
| 29 | + </xs:complexType> |
| 30 | + </xs:element> |
| 31 | +</xs:schema> |
| 32 | +``` |
| 33 | + |
| 34 | +This file (or a corresponding `.dtd` file) can be bound using the [XML Binding Wizard](#the-xml-binding-wizard), which can be triggered by [Command](#command), [CodeLens](#codelens) or [Quick Fix](#quick-fix). |
| 35 | + |
| 36 | +### The XML Binding Wizard |
| 37 | + |
| 38 | +After opening the binding wizard using one of the methods mentioned above, a dropdown with the following 2 options will appear at the Command Palette: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +1. Standard (xsi, DOCTYPE) |
| 43 | + |
| 44 | +This option will bind the grammar file by adding the `xmlns:xsi` (with the value as "http://www.w3.org/2001/XMLSchema-instance") and `xsi:noNamespaceSchemaLocation` (with the value as the path to the grammar file) attributes to the root tag: |
| 45 | + |
| 46 | +```xml |
| 47 | +<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 48 | + xsi:noNamespaceSchemaLocation="foo.xsd"> |
| 49 | + <bar /> |
| 50 | +</foo> |
| 51 | +``` |
| 52 | + |
| 53 | +2. XML Model association |
| 54 | + |
| 55 | +This option will bind the grammar file by adding the `xml-model` tag at the beginning of the file, with an `href` value as the path to the grammar file: |
| 56 | + |
| 57 | +```xml |
| 58 | +<?xml-model href="foo.xsd"?> |
| 59 | +<foo> |
| 60 | + <bar /> |
| 61 | +</foo> |
| 62 | +``` |
| 63 | + |
| 64 | +After selecting an option, you can select the desired grammar file using your file explorer. |
| 65 | + |
| 66 | +### Command |
| 67 | + |
| 68 | +The command "XML: Bind to grammar/schema file" can be executed from the VSCode command palette, as well as be bound to a shortcut. |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +If the current XML document is not bound using an `xml-model` tag or a root element doesn't have a `xsi:noNameSpaceLocation` attribute, the command will have the user select a existing `.xsd/.dtd` grammar file to bind to the XML document. |
| 73 | + |
| 74 | +Otherwise, an error will be thrown on the client if the XML document already has a bound grammar/schema. |
| 75 | + |
| 76 | +### CodeLens |
| 77 | + |
| 78 | +When [CodeLenses](./Preferences.md#code-lens) are enabled, an unbound XML document will display a CodeLens above the root element as follows: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +### Quick Fix |
| 83 | + |
| 84 | +For any unbound XML document, a Quick Fix suggestion will appear beside the root element. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +With the cursor on the first opening tag, use `Ctrl + .`, `Quick Fix...` or the lightbulb that appears and select the "Open binding wizard to bind existing grammar/schema" |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +## Binding with New Grammar |
| 93 | + |
| 94 | +Consider the case where the directory only contains `foo.xml` (i.e. there does not exist an appropriate `.xsd/.dtd` file to bind). |
| 95 | + |
| 96 | +A file can be generated using a [Quick Fix](#quick-fix-1). |
| 97 | + |
| 98 | +### Quick Fix |
| 99 | + |
| 100 | +#### Generate XSD from XML |
| 101 | + |
| 102 | +When an unbound XML file is open, an XML Schema/XSD file can be generated from the opening tag in the XML file. |
| 103 | + |
| 104 | +With the cursor on the first opening tag, use `Ctrl + .` or `Quick Fix...` or the lightbulb that appears and select "Generate foo.xsd and bind with *" to create the file in the the same directory. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +#### Generate DTD from XML |
| 109 | + |
| 110 | +When an unbound XML file is open, a Document Type Definition/DTD file can be generated from the opening tag in the XML file. |
| 111 | + |
| 112 | +With the cursor on the first opening tag, use `Ctrl + .` or `Quick Fix...` or the lightbulb that appears and select "Generate foo.dtd and bind with *" to create the file in the the same directory. |
| 113 | + |
| 114 | + |
0 commit comments