Skip to content

Commit 57971ff

Browse files
angelozerrdatho7561
authored andcommitted
Declare xml.references settings
Signed-off-by: azerr <[email protected]>
1 parent 56aa0e2 commit 57971ff

File tree

6 files changed

+213
-1
lines changed

6 files changed

+213
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This VS Code extension provides support for creating and editing XML documents,
1919
| ------------------ | ------------------------------------------- |
2020
| enabled by default | requires additional configuration to enable |
2121

22+
- * [XML References features](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Features/XMLReferencesFeatures.md#xml-references-features) (since v0.24.0)
2223
* [RelaxNG (experimental) support](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Features/RelaxNGFeatures.md#relaxng-features) (since v0.22.0)
2324
* [Surround with Tags, Comments, CDATA](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Refactor.md#refactor) (since v0.23.0)
2425
* Syntax error reporting

docs/Features.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
- [DTD features](Features/DTDFeatures.md#dtd-features)
88
- [RelaxNG features](Features/RelaxNGFeatures.md#relaxng-features)
99
- [XInclude features](Features/XIncludeFeatures.md#xinclude-features)
10-
- [XML Catalog features](Features/XMLCatalogFeatures.md#xml-catalog-features)
10+
- [XML Catalog features](Features/XMLCatalogFeatures.md#xml-catalog-features)
11+
- [XML References features](Features/XMLReferencesFeatures.md#xml-references-features)
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# XML References Features
2+
3+
XML References support provides the capability to reference a DOM node (attribute or text) to an another DOM node (attribute or text) with a the `xml.references` settings by using XPath expression :
4+
5+
* `foo/@attr` defines the `attr` attribute node of the `foo` element.
6+
* `foo/text()` defines the text node of the `foo` element.
7+
8+
## Attribute node references (foo/@attr)
9+
10+
Given this [docbook](https://docbook.org/) XML file sample:
11+
12+
```xml
13+
<?xml version="1.0" encoding="UTF-8"?>
14+
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.docbook.org/xml/4.4/docbookx.dtd">
15+
<book>
16+
<chapter id="chapter-1">
17+
18+
<xref linkend="chapter-1" />
19+
20+
</chapter>
21+
22+
<chapter id="chapter-2">
23+
24+
</chapter>
25+
</book>
26+
```
27+
28+
At first, please note that [resolve external entities](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Validation.md#resolve-external-entities) must be enabled for this DTD to work.
29+
30+
In this sample, `linkend` attribute in `<xref linkend="chapter-1" />` references the `chapter-1` declared in `<chapter id="chapter-1">`. [vscode-xml](https://github.com/redhat-developer/vscode-xml) provides a completion, definition, highlighting support to support this kind of references easily with the `xml.references` settings. For docbook case, you can declare this settings:
31+
32+
```json
33+
"xml.references": [
34+
{
35+
"pattern": "**/*.xml",
36+
"expressions": [
37+
{
38+
"from": "xref/@linkend",
39+
"to": "@id"
40+
}
41+
]
42+
}
43+
]
44+
```
45+
46+
After saving this setting, you will get completion, go to definition, and highlighting for `xref` in the docbook file:
47+
48+
![XML References with docbook](../images/Features/XMLReferencesWithDocbook.gif)
49+
50+
The `xml.references` settings is an array of objects with the following properties:
51+
52+
* `pattern`: matches the files that reference declared with `expressions` applies to. See [glob syntax](https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob) for more information about the pattern syntax.
53+
* `expressions`: array of reference expression:
54+
* `prefix (optional)`: the prefix to use (ex : '#') for from.
55+
* `from`: the from reference DOM node (attribute, text) declared with XPath (ex: `foo/@attr`, `foo/text()`).
56+
* `to`: the to reference DOM node (attribute, text) declared with XPath (ex: `foo/@attr`, `foo/text()`).
57+
58+
### prefix
59+
60+
Given this [TEI](https://tei-c.org/) XML file sample:
61+
62+
```xml
63+
<?xml version="1.0" encoding="UTF-8"?>
64+
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_lite.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
65+
<TEI xmlns="http://www.tei-c.org/ns/1.0">
66+
<teiHeader>
67+
<fileDesc>
68+
<titleStmt>
69+
<title>Title</title>
70+
</titleStmt>
71+
<publicationStmt>
72+
<p>Publication information</p>
73+
</publicationStmt>
74+
<sourceDesc>
75+
<p>Information about the source</p>
76+
</sourceDesc>
77+
</fileDesc>
78+
</teiHeader>
79+
<text>
80+
<body xml:id="body-id">
81+
<p xml:id="p-id" >Some text here.</p>
82+
<anchor corresp="#body-id"></anchor>
83+
</body>
84+
</text>
85+
</TEI>
86+
```
87+
88+
In this sample, `corresp` attribute in `<anchor corresp="#body-id"></anchor>` references the `body-id` (without `#`) declared in `<body xml:id="body-id">`. It means that the `corresp` attribute value (with `#`) reference `@xml:id` attribute (without `#`). To support that, you can configure settings by using `prefix`:
89+
90+
```json
91+
"xml.references": [
92+
{
93+
"pattern": "**/*.xml",
94+
"expressions": [
95+
{
96+
"prefix": "#",
97+
"from": "@resp",
98+
"to": "persName/@xml:id"
99+
},
100+
{
101+
"prefix": "#",
102+
"from": "@corresp",
103+
"to": "@xml:id"
104+
}
105+
]
106+
}
107+
]
108+
```
109+
110+
## Text node references (foo/text())
111+
112+
Given this *web.xml* sample:
113+
114+
```xml
115+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
116+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
117+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
118+
version="3.1">
119+
<servlet>
120+
<servlet-name>comingsoon</servlet-name>
121+
<servlet-class>mysite.server.ComingSoonServlet</servlet-class>
122+
</servlet>
123+
<servlet-mapping>
124+
<servlet-name>comingsoon</servlet-name>
125+
<url-pattern>/*</url-pattern>
126+
</servlet-mapping>
127+
</web-app>
128+
```
129+
130+
In this sample, `servlet-mapping/servlet-name` text in `<servlet-name>comingsoon</servlet-name>` references the `comingsoon` declared in ``servlet/servlet-name` text. To support that, you can configure settings like this:
131+
132+
```json
133+
"xml.references": [
134+
{
135+
"pattern": "**/web.xml",
136+
"expressions": [
137+
{
138+
"from": "servlet-mapping/servlet-name/text()",
139+
"to": "servlet/servlet-name/text()"
140+
}
141+
]
142+
}
143+
]
144+
```
145+
146+
![XML References with web.xml](../images/Features/XMLReferencesWithWebXML.gif)
147+
148+
### Limitation
149+
150+
XML references have some limitation:
151+
152+
* *references works only for a given XML file*: it is not possible to reference some DOM nodes coming from another XML files. However if the file uses include (like xi:include) the reference will only work in the file which has the include statement, and not in the file being included.
153+
* *multiple target is not supported*: if the `origin` attribute (which matches the `from` reference path) declares multiple targets (which matches the `to` reference path), it will not work.
154+
155+
Given this XML file where `corresp` attribute defines several targets separated with whitespace (#body-id #p-id):
156+
157+
```xml
158+
<body xml:id="body-id">
159+
<p xml:id="p-id" >Some text here.</p>
160+
<anchor corresp="#body-id #p-id"></anchor>
161+
</body>
162+
```
163+
164+
This usecase is not supported today.
165+
166+
If you need those support, please [create an issue](https://github.com/redhat-developer/vscode-xml/issues)
78.7 KB
Loading
108 KB
Loading

package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,50 @@
633633
"markdownDescription": "Allows XML symbols filter to be associated to file name patterns. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Symbols%22%2C%22section%22%3A%22xmlsymbolsfilters%22%7D%5D) for more information.. \n\nExample:\n```json\n[\n {\n \"pattern\": \"pom.xml\",\n \"expressions\": [\n {\n \"xpath\": \"//text()\"\n }\n ]\n }\n]\n```",
634634
"scope": "window"
635635
},
636+
"xml.references": {
637+
"type": "array",
638+
"default": [],
639+
"items": {
640+
"type": "object",
641+
"properties": {
642+
"pattern": {
643+
"type": "string",
644+
"markdownDescription": "matches the files that reference declared with `expressions` applies to.\n\nMore information on the glob syntax: https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob"
645+
},
646+
"expressions": {
647+
"type": "array",
648+
"default": [],
649+
"items": {
650+
"type": "object",
651+
"properties": {
652+
"prefix": {
653+
"type": "string",
654+
"description": "The prefix to use (ex : '#') for from."
655+
},
656+
"from": {
657+
"type": "string",
658+
"description": "The from reference DOM node (attribute, text) declared with XPath (ex: foo/@attr, foo/text())."
659+
},
660+
"to": {
661+
"type": "string",
662+
"description": "The to reference DOM node (attribute, text) declared with XPath (ex: foo/@attr, foo/text())."
663+
}
664+
}
665+
},
666+
"required": [
667+
"from",
668+
"to"
669+
]
670+
}
671+
},
672+
"required": [
673+
"pattern",
674+
"expressions"
675+
]
676+
},
677+
"markdownDescription": "Allows XML schemas/ DTD to be associated to file name patterns. Please refer to [XML file association with XSD](command:xml.open.docs?%5B%7B%22page%22%3A%22Validation%22%2C%22section%22%3A%22xml-file-association-with-xsd%22%7D%5D) or [XML file association with DTD](command:xml.open.docs?%5B%7B%22page%22%3A%22Validation%22%2C%22section%22%3A%22xml-file-association-with-dtd%22%7D%5D) for more information. \n\nExample:\n```json\n[{\n \"pattern\": \"file1.xml\",\n \"systemId\": \"path/to/file.xsd\"\n},\n{\n \"pattern\": \"**/*.xsd\",\n \"systemId\": \"http://www.w3.org/2001/XMLSchema.xsd\"\n}]\n```",
678+
"scope": "window"
679+
},
636680
"xml.extension.jars": {
637681
"type": "array",
638682
"default": [],

0 commit comments

Comments
 (0)