|
| 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 | + |
| 6 | + |
| 7 | +[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. |
| 8 | + |
| 9 | +In the following sections, we would like to bind the XML document `foo.xml`: |
| 10 | +```xml |
| 11 | +<foo> |
| 12 | + <bar /> |
| 13 | +</foo> |
| 14 | +``` |
| 15 | +with an associated grammar file. |
| 16 | + |
| 17 | +## Binding with Existing Grammar |
| 18 | + |
| 19 | +Consider an XSD document, `foo.xsd` (in the same directory as `foo.xml`), defined as follows : |
| 20 | +```xsd |
| 21 | +<?xml version="1.0" encoding="UTF-8"?> |
| 22 | +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
| 23 | + <xs:element name="foo"> |
| 24 | + <xs:complexType> |
| 25 | + <xs:sequence> |
| 26 | + <xs:element name="bar" /> |
| 27 | + </xs:sequence> |
| 28 | + </xs:complexType> |
| 29 | + </xs:element> |
| 30 | +</xs:schema> |
| 31 | +``` |
| 32 | +and a DTD document, `foo.dtd` (in the same directory as `foo.xml`), defined as follows: |
| 33 | +```dtd |
| 34 | +<!ELEMENT foo (bar)> |
| 35 | +<!ELEMENT bar EMPTY> |
| 36 | +``` |
| 37 | + |
| 38 | +Either 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). |
| 39 | + |
| 40 | +### The XML Binding Wizard |
| 41 | + |
| 42 | +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: |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +1. Standard (xsi, DOCTYPE) |
| 47 | + |
| 48 | +If the selected file is an XSD file, 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: |
| 49 | +```xml |
| 50 | +<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 51 | + xsi:noNamespaceSchemaLocation="foo.xsd"> |
| 52 | + <bar /> |
| 53 | +</foo> |
| 54 | +``` |
| 55 | + |
| 56 | +If the XSD file contains a `targetNamespace` attribute, with an `elementFormDefault` attribute with value "qualified" in the `xs:schema` tag, as follows: |
| 57 | +```xsd |
| 58 | +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo" elementFormDefault="qualified"> |
| 59 | +``` |
| 60 | +then the XML document will have the `targetNamespace` set as a `xmlns` attribute and an additional attribute for `xsi:schemaLocation`: |
| 61 | +```xml |
| 62 | +<foo xmlns="http://foo" |
| 63 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 64 | + xsi:schemaLocation="http://foo foo.xsd"> |
| 65 | + <bar /> |
| 66 | +</foo> |
| 67 | +``` |
| 68 | + |
| 69 | +If the selected file is a DTD file, this option will bind the grammar file by adding a `<!DOCTYPE <root-tag-name> SYSTEM "<grammar-file-path>">` tag at the beginning of the document: |
| 70 | +```xml |
| 71 | +<!DOCTYPE foo SYSTEM "foo.dtd"> |
| 72 | +<foo> |
| 73 | + <bar /> |
| 74 | +</foo> |
| 75 | +``` |
| 76 | + |
| 77 | +2. XML Model association |
| 78 | + |
| 79 | +This option will bind the grammar file by adding the `xml-model` tag at the beginning of the document, with an `href` value as the path to the grammar file: |
| 80 | +```xml |
| 81 | +<?xml-model href="foo.xsd"?> |
| 82 | +<foo> |
| 83 | + <bar /> |
| 84 | +</foo> |
| 85 | +``` |
| 86 | +The same applies when selecting `foo.dtd`. |
| 87 | + |
| 88 | +After selecting an option, you can select the desired grammar file using your file explorer: |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +### Command |
| 93 | + |
| 94 | +The command "XML: Bind to grammar/schema file" can be executed from the VSCode command palette, as well as be bound to a shortcut. |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +If the current XML document is not bound using an existing grammar/schema, the command will have the user select a existing `.xsd/.dtd` grammar file to bind to the XML document. |
| 99 | + |
| 100 | +Otherwise, an error will be thrown on the client if the XML document already has a bound grammar/schema. |
| 101 | + |
| 102 | +### CodeLens |
| 103 | + |
| 104 | +When [CodeLenses](./Preferences.md#code-lens) are enabled, an unbound XML document will display a CodeLens above the root element as follows: |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +### Quick Fix |
| 109 | + |
| 110 | +For any unbound XML document, a Quick Fix suggestion will appear beside the root element. |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +With the cursor on the first opening tag, use `Ctrl + .`, `Quick Fix...` or the lightbulb that appears and select the "Bind to existing grammar/schema" |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +## Binding with New Grammar |
| 119 | + |
| 120 | +Consider the case where the directory only contains `foo.xml` (i.e. there does not exist an appropriate `.xsd/.dtd` file to bind). |
| 121 | + |
| 122 | +A file can be generated using a [Quick Fix](#quick-fix-1). |
| 123 | + |
| 124 | +### Quick Fix |
| 125 | + |
| 126 | +#### Generate XSD from XML |
| 127 | + |
| 128 | +When an unbound XML file is open, an XML Schema/XSD file can be generated from the opening tag in the XML file. |
| 129 | + |
| 130 | +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. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +#### Generate DTD from XML |
| 135 | + |
| 136 | +When an unbound XML file is open, a Document Type Definition/DTD file can be generated from the opening tag in the XML file. |
| 137 | + |
| 138 | +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. |
| 139 | + |
| 140 | + |
0 commit comments