You can use code completion in XML files if the XML document is constrained by an XML schema. In the schema source file, this constraint is represented by a xsi:schemaLocation or a xsi:noNamespaceSchemaLocation attribute in the root element of the schema. Typically, xsi:schemaLocation is used for schemas with a target namespaces and xsi:noNamespaceSchemaLocation is used for schemas with no target namespace.
As a general rule, if the root elements provide the necessary information to locate the schemas, the IDE will be able to offer code completion based on those schemas. The IDE will attempt to locate the schemas in the following order:
If the IDE does not find the schemas in the local file system or the runtime catalog, code completion will not be available.
The code completion functionality provides two types of code completion:
XML schema allows the use of wildcards, xsd:any and xsd:anyAttribute. When using these wildcards, the IDE will limit the information in the code completion box by namespace. Both xsd:any and xsd:anyAttribute come with an optional namespace attribute. The table below shows how the IDE substitutes wildcards based on the namespace attribute value.
| namespace value | Substitution |
|---|---|
| ##any | Any element from any namespace. |
| ##other | Any element from other namespaces other than the targetNamespace. |
| ##targetNamespace | Any element from targetNamespace. |
| ##local | Any unqualified element (no namespace). |
| List of URIs | Elements from the specified namespaces. |
For example, assume you have the following:
<a:RootA xmlns:a="http://xml.netbeans.org/schema/A" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xml.netbeans.org/schema/A A.xsd http://xml.netbeans.org/schema/B B.xsd http://xml.netbeans.org/schema/C C.xsd"> < (current cursor position) </a:RootA>
In this example, RootA is one of the root elements defined in schema A.xsd. If RootA had a xsd:any child element, then at the cursor position you would see items appearing from various namespaces as per substitution rule shown in the table above. The same applies for xsd:anyAttribute.
When you use wildcards and you select an element from another namespace, the IDE will automatically insert a namespace declaration for that element. The IDE will try different prefixes, starting with ns1, and if a match is found, a declaration is added as follows: xmlns:ns1="targetNamespace-of-selected-element".
To use an existing XML catalog to constrain a new XML document:
<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='null'> </root>and replace it with the following:
<prefix:schema-root-element xmlns:prefix='schema-targetNamespace' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='schema-targetNamespace schema-targetNamespace'> </prefix:schema-root-element>In the XML document, you can now invoke code completion.
To complete an XML tag in an XML document:
You can use the most convenient combination of the following options to select values in the code completion box: