Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

XMLNode namespace informations



Several methods in XMLNode class will only return a not null result if the namespace informations are present in the XMLNode.

It concerns:
  • The bound prefixes
  • The schema location

Bound prefixes

The XMLNode.getBoundPrefixes() return the map from URIs to their prefixes. It will only be null if namespace informations are present in the XMLNode. The XMLNode.hasBoundPrefixes() will only return true if there are prefixes.

The XMLNode.getBoundURIs() return the map from prefixes. to their URIs. It will only be null if namespace informations are present in the XMLNode. The XMLNode.hasBoundURIs() will only return true if there are URIs.

For example, for the following XML file:
   <root xsi:schemaLocation="http://my/schema.xsd xmlnode.xsd" xmlns="http://my/schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <child name="first">
         <child1 name2="second"/>  
         <child1 name2="third"/>
      </child>
   </root>
If we parse the file with the XMLNodeUtilitiesOptions.NAMESPACE_AWARE option:
   XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>, XMLNodeUtilitiesOptions.NAMESPACE_AWARE);
Then we will have:
  • 2 bound prefixes:
    • root.getBoundPrefix("http://www.w3.org/2001/XMLSchema-instance") is "xsi"
    • root.getBoundPrefix("http://my/schema.xsd") is ""
  • 2 bound URIs:
    • root.getBoundURI("xsi") is "http://www.w3.org/2001/XMLSchema-instance"
    • root.getBoundURI("") is "http://my/schema.xsd"
The XMLNode.getSchemaInstancePrefix() return the prefix of the http://www.w3.org/2001/XMLSchema-instance URI. For example in the previous example it will return "xsi".

The XMLNode.getNamespaceURI() method return the namespace URI of the node. For example in the previous example it will return "http://my/schema.xsd".

The XMLNode.getPrefix() method return the prefix of namespace of the node. For example in the previous example it will return "".

Schema location

The XMLNode.getSchemaLocationAttributeValue() method return the attribute value for the shema location. For example in the previous example it will return "http://my/schema.xsd xmlnode.xsd".

The XMLNode.getSchemaLocationValue() method return the shema location. For example in the previous example it will return "http://my/schema.xsd".

If there is no schema location, the getSchemaLocationValue() method will return null. If there is more than one, it will return only the first one.

Get all schema locations

The XMLNode.getSchemaLocations() method return a Map with all the schema locations.

  • The keys of the map are the URI of the schema
  • The values of the map are the associated XMLSchemaLocation
The XMLSchemaLocation has methgods to:
If there is no schema location, the getSchemaLocations() method will return null.


For example, with this xml file:
   <a661_df xmlns="http://mySchema.com/a661"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:ext="http://myExtendedSchema.com/a661"
            xsi:schemaLocation="http://mySchema.com/a661 a661.xsd
                          http://myExtendedSchema.com/a661 a661Extension.xsd"
            name="Layer" library_version="0" supp_version="7">
   </a661_df>
The following code will get the nodes structure:
   XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>, XMLNodeUtilities.NAMESPACE_AWARE);
To get the schema locations:
   Map<String, XMLSchemaLocation> locations = root.getSchemaLocations();
      
   XMLSchemaLocation location = locations.get("http://mySchema.com/a661");
   // location.getURI() is "http://mySchema.com/a661"
   // location.getPrefix() is ""
   // location.getLocation() is "a661.xsd"
      
   location = locations.get("http://myExtendedSchema.com/a661");
   // location.getURI() is "http://myExtendedSchema.com/a661"
   // location.getPrefix() is "ext"
   // location.getLocation() is "a661Extension.xsd"      

See also


Categories: Packages | Xml

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences