|
| 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 | + |
| 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 | + |
| 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) |
0 commit comments