Skip to content

Commit a34be97

Browse files
author
Alexander Chen
committed
added BindingWithGrammar.md to document vscode-xml grammar binding
1 parent e903708 commit a34be97

12 files changed

+150
-23
lines changed

docs/BindingWithGrammar.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
![Binding Wizard Demo](./images/BindingWithGrammar/BindingWizardDemo.gif)
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+
![Binding Wizard Dropdown](./images/BindingWithGrammar/BindingWizardDropdown.png)
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+
![Binding Wizard File Explorer](./images/BindingWithGrammar/BindingWizardFileExplorer.png)
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+
![Bind To Grammar Command](./images/BindingWithGrammar/BindToGrammarCommand.png)
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+
![Bind To Grammar Command](./images/BindingWithGrammar/BindToGrammarCodeLens.png)
107+
108+
### Quick Fix
109+
110+
For any unbound XML document, a Quick Fix suggestion will appear beside the root element.
111+
112+
![Bind To Grammar CodeAction](./images/BindingWithGrammar/BindToGrammarCodeAction.png)
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+
![Bind To Grammar CodeAction Dropdown](./images/BindingWithGrammar/BindToGrammarCodeActionDropdown.png)
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+
![GenerateXSDFromXML](./images/BindingWithGrammar/GenerateXSDFromXML.gif)
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+
![GenerateDTDFromXML](./images/BindingWithGrammar/GenerateDTDFromXML.gif)

docs/Features/XMLFeatures.md

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ To rename a single tag and its corresponding opening/closing, highlight the tag
4444

4545
## Tag Name Highlighting
4646

47-
When placing the cursor on a XML tag, the corresponding opening/closing tag will be highlighted.
47+
When placing the cursor on an XML tag, the corresponding opening/closing tag will be highlighted.
4848

4949
![Highlight Corresponding XML](../images/Features/HighlightCorrespondingXML.gif)
5050

@@ -72,15 +72,15 @@ If an XML file is not associated to a bound grammar file, you can utilize XML co
7272

7373
### Attribute value completion
7474

75-
For a XML tag attribute, there is autocompletion support for the attribute value. For example, for path completion, type `.` or `/`.
75+
For an XML tag attribute, there is autocompletion support for the attribute value. For example, for path completion, type `.` or `/`.
7676

7777
![Attribute Completion XML](../images/Features/AttributeCompletionXML.gif)
7878

7979
### Completion based on grammar
8080

8181
#### Completion based on XSD
8282

83-
When a XML file is associated with a XSD file, there is support for completion based on the tags, attribute names and values defined in the XML Schema/XSD file.
83+
When an XML file is associated with an XSD file, there is support for completion based on the tags, attribute names and values defined in the XML Schema/XSD file.
8484

8585
![Completion Based On XSD](../images/Features/CompletionBasedOnXSD.gif)
8686

@@ -102,7 +102,7 @@ If an XML file is associated with an XSD file, there is support for jumping from
102102

103103
#### Hover based on XSD
104104

105-
When XML file is associated with a XSD file, hover documentation based on tags, attributes name and value defined in the XML Schema/XSD file are supported.
105+
When XML file is associated with an XSD file, hover documentation based on tags, attributes name and value defined in the XML Schema/XSD file are supported.
106106

107107
![Hover Based On XSD](../images/Features/HoverBasedOnXSD.gif)
108108

@@ -120,33 +120,19 @@ LemMinX will show syntax errors in your XML documentation, and will provide quic
120120

121121
![Validate XML](../images/Features/ValidationXML.gif)
122122

123-
There is also validation based on grammar when a XSD or DTD file is associated with the XML document.
123+
There is also validation based on grammar when an XSD or DTD file is associated with the XML document.
124124

125125
### Read more
126126

127127
See [XML Validation](../Validation.md#xml-validation) for more details.
128128

129-
## Generate Grammar from XML
129+
## Binding Grammar to an XML Document
130130

131-
### Generate XSD from XML
131+
For any XML document, should you need to bind a grammar or schema with the file, you can do so by either using an existing grammar file, or by generating one using the built-in schema generator.
132132

133-
Using an XML file, an XML Schema/XSD file can be generated from the opening tag in the XML file. With the cursor on the first opening tag, use `Ctrl + .` or `Quick Fix...` and select `Generate foo.xsd and bind with *` to create the file in the the same directory.
133+
See [Binding with Existing Grammar](../BindingWithGrammar.md#binding-with-existing-grammar) for info on binding to an existing schema.
134134

135-
![GenerateXSDFromXML](../images/Features/GenerateXSDFromXML.gif)
136-
137-
#### Read more
138-
139-
See [Validation with XSD Grammar](../Validation.md#validation-with-xsd-grammar) for more.
140-
141-
### Generate DTD from XML
142-
143-
Using an XML file, a Document Type Definition/DTD file can be generated from the opening tag in the XML file. With the cursor on the first opening tag, use `Ctrl + .` or `Quick Fix...` and select `Generate foo.dtd and bind with *` to create the file in the the same directory.
144-
145-
![GenerateDTDFromXML](../images/Features/GenerateDTDFromXML.gif)
146-
147-
#### Read more
148-
149-
See [Validation with DTD Grammar](../Validation.md#validation-with-dtd-grammar) for more.
135+
See [Binding with New Grammar](../BindingWithGrammar.md#binding-with-new-grammar) for info on grammar generation.
150136

151137
## Selection Range
152138

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Welcome to the [vscode-xml](https://github.com/redhat-developer/vscode-xml) docu
1212
* [Troubleshooting](Troubleshooting.md#troubleshooting): Info on troubleshooting and fixes to issues.
1313
* [Features](Features.md#features): Notable info and demos on features available to use.
1414
* [Proxy](Proxy.md#proxy): Instructions for setting up vscode-xml to work behind a proxy.
15+
* [Binding With Grammar](BindingWithGrammar.md#binding-with-grammar): Extension feature to bind an XML document to a grammar/schema file.
1516

1617
## Developer Guide
1718

4.07 KB
Loading
20.5 KB
Loading
5.52 KB
Loading
5.47 KB
Loading
197 KB
Loading
6.53 KB
Loading
32.6 KB
Loading

0 commit comments

Comments
 (0)