Skip to content

Commit a0414a5

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

File tree

10 files changed

+124
-23
lines changed

10 files changed

+124
-23
lines changed

docs/BindingWithGrammar.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
![Binding Wizard Dropdown](./images/BindingWithGrammar/BindingWizardDropdown.png)
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+
![Bind To Grammar Command](./images/BindingWithGrammar/BindToGrammarCommand.png)
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+
![Bind To Grammar Command](./images/BindingWithGrammar/BindToGrammarCodeLens.png)
81+
82+
### Quick Fix
83+
84+
For any unbound XML document, a Quick Fix suggestion will appear beside the root element.
85+
86+
![Bind To Grammar CodeAction](./images/BindingWithGrammar/BindToGrammarCodeAction.png)
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+
![Bind To Grammar CodeAction Dropdown](./images/BindingWithGrammar/BindToGrammarCodeActionDropdown.png)
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+
![GenerateXSDFromXML](./images/BindingWithGrammar/GenerateXSDFromXML.gif)
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+
![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 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
21.1 KB
Loading
5.52 KB
Loading
5.47 KB
Loading
6.53 KB
Loading

0 commit comments

Comments
 (0)