Using Code Completion in XML Files Constrained by an XML Schema

See Also

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:

  1. Check the local file system to find the schema.
  2. If not found in the local file system, perform a lookup in the runtime catalog.

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:

  1. In the IDE, open the Runtime window.
  2. Expand the DTD and XML Schema Catalogs node, then expand the catalog node, right-click the catalog entry node and choose View.
    The Source Editor opens the catalog entry in read-only mode.
  3. Make a note of the targetNamespace value (schema-targetNamespace) and the root or top-level element (schema-root-element) in the schema.
  4. In the IDE, create a new XML document by doing the following:
    1. From the main menu, choose File > New File.
      The New File wizard opens.
    2. Under Categories, select the XML node and under File Types, select the XML Document node, then click Next.
    3. Specify a name and location for the document and click Next.
    4. Select the XML Schema-Constrained Document radio button and click Next.
    5. In the Schema Options page of the wizard, click Finish without completing any of the fields.
      The IDE creates the new XML document in the specified location.
  5. In the XML document, select the root definition,
    <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:

  1. In the Source editor tab of the XML document, position the cursor where you want to start adding content and type an opening left angle bracket (<).
    If you want to use code completion in an existing element, simply position your cursor within the element and press Spacebar or type any character to invoke code completion.
  2. Pause after step 1, or press Ctrl-Spacebar.
    The code completion box opens. This box provides a scrolling list of schema elements. The list of elements is based on the XML schema file that constrains the current XML file.
  3. Select a value using the information in Selecting Values in the Code Completion Box.
    If you select a component with attributes, the next Spacebar keystroke will invoke the code completion box with the attributes.

Selecting Values in the Code Completion Box

You can use the most convenient combination of the following options to select values in the code completion box:

See Also
About XML Schema Tools
Using Code Completion
Registering a Local DTD or XML Schema Resource

Legal Notices