Sunday, August 23, 2020

Namespace in XML

 

Namespaces

namespaces are a mechanism used to avoid naming conflicts when elements or attributes from different sources or vocabularies are combined in a single XML document. Namespaces are essential when you have XML documents that include elements or attributes with the same names but belong to different domains or purposes.

->To avoid name conflict.

-> namespace are used to group all the elements as a single unit.

-> (Uniquely identify the xml components)

-> solving the name conflict using a prefix

-> when using prefixes in xml, a namespace for the prefix must be defined.


Declaring Namespaces

To use namespaces in an XML document, you need to declare them. This is typically done in the root element of the XML document using the xmlns attribute.

The XSLT namespace is the value of the xmlns:xsl attribute. 
Attributes that start with xmlns have a special significance in XML; they're known as namespace declarations.
A namespace declaration contains two parts, the namespace prefix, and the namespace URI. 
 A namespace prefix identifies the elements and their attributes in the document.
A namespace URI is the unique identifier of the namespace.

The syntax to declare a namespace is:
namespace-prefix:element-name

Declaring a Namespace

xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform
version="2.0">
<xsl:template match ="/">
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

each element contains the xsl prefix, which is declared in the <xsl:stylesheet> element with the URI http://www.w3.org/1999/XSL/Transform. The keyword xsl helps identify a particular namespace of an element or attribute in a document.

When you declare a namespace URI in a particular element node, the scope of that namespace applies to all its child and descendent nodes in the document. 

If a namespace declaration does not contain a prefix, it is called the default namespace.

Ex: 

<root xmlns:ns="http://example.com">

    <ns:element1>Content1</ns:element1>

    <ns:element2>Content2</ns:element2>

</root>

In this example xmlns:ns attribute declares a namespace prefix ns associated with the namespace URI http://example.com.

Namespace Prefix: The prefix, in this case, ns, is used to qualify element and attribute names within the specified namespace. It helps differentiate elements and attributes with the same local name but different namespaces.


By using the namespace prefix, you explicitly indicate which namespace an element or attribute belongs to. In the example above, ns:element1 and ns:element2 are in the http://example.com namespace.

Default Namespace: You can also declare a default namespace using xmlns without a prefix. Elements without a namespace prefix are assumed to belong to the default namespace.

<root xmlns="http://example.com">
    <element1>Content1</element1>
    <element2>Content2</element2>
</root>

In this example, both element1 and element2 belong to the http://example.com namespace.

Namespace Prefixes:To reference elements or attributes from a specific namespace, you can use namespace prefixes. Prefixes are arbitrary strings (often short and meaningful) followed by a colon, and they are associated with namespaces using the xmlns attribute with a xmlns:prefix declaration.

<root xmlns:ex="http://example.com/ns">
    <ex:element>...</ex:element>
</root>

In this example, the ex prefix is associated with the "http://example.com/ns" namespace, and it is used to qualify the element element.

Unprefixed Elements:Elements and attributes without a prefix are considered to be in the default namespace (if one is declared). If no default namespace is declared, they are considered to be in no namespace (the empty namespace).

Using Multiple Namespaces:

XML documents can use multiple namespaces simultaneously. Each namespace is typically associated with a specific vocabulary or schema. For example, an XML document might use namespaces for its content, XML Schema definitions, and XSLT transformations, all within the same document.

Namespace Scope:

Namespace declarations are inherited by child elements unless overridden. They define the scope of the namespace within the XML document.

Namespace URI:

The namespace URI is a unique identifier for the namespace. It is usually a URL but does not necessarily need to be resolved to a web resource. It is used to ensure that the same namespace is not accidentally used by different parties.

Namespace Usage:

Namespaces are commonly used when XML documents incorporate elements or attributes from various sources, including different XML vocabularies, XML Schema (XSD), or external data standards.

By using namespaces, you can ensure that elements and attributes in your XML document are correctly interpreted and avoid conflicts that may arise from combining XML content from different sources. Namespaces are particularly important when working with complex XML documents, web services, or standards like SOAP and XML Schema.

Target Namespace:

In XML, a target namespace is a way to uniquely identify and categorize XML elements and attributes within a document. Target namespaces are often associated with XML schema definitions (XSD) to define the structure and validation rules for XML documents.

Defining a Target Namespace:

To define a target namespace in an XML schema, you typically use the xmlns attribute in the root element of the schema. This attribute declares a namespace and associates it with a Uniform Resource Identifier (URI), which serves as a unique identifier for that namespace.

Ex:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.com/mynamespace">
   <!-- Define elements and types within this namespace -->
</xsd:schema>

In this example, the targetNamespace attribute associates the "http://example.com/mynamespace" URI with the XML schema. All elements and types defined within this schema are considered part of this namespace.

Using Elements from a Target Namespace:

When you define XML documents based on this schema, you reference elements from the target namespace by using the namespace prefix or declaring a default namespace for the entire document. You do this using the xmlns attribute in the root element of the XML document.

<myNamespaceRoot xmlns="http://example.com/mynamespace">
   <element1>Value 1</element1>
   <element2>Value 2</element2>
</myNamespaceRoot>

In this XML document, the xmlns attribute without a prefix specifies that elements within this document belong to the "http://example.com/mynamespace" namespace.

Avoiding Name Conflicts:

Target namespaces are essential for avoiding naming conflicts, especially when combining XML documents or elements from different sources. Different namespaces allow you to use the same element or attribute names without ambiguity, as long as they belong to different namespaces.

For example, two XML documents can both have an "element1" element, but if they are in different namespaces, they are treated as distinct:

<!-- Document 1 -->

<myNamespaceRoot xmlns="http://example.com/mynamespace">
   <element1>Value 1</element1>
</myNamespaceRoot>

<!-- Document 2 -->

<anotherNamespaceRoot xmlns="http://example.com/anothernamespace">
   <element1>Value 2</element1>
</anotherNamespaceRoot>

In this case, "element1" in Document 1 is not the same as "element1" in Document 2 because they belong to different namespaces.

Interoperability: Defining target namespaces and using namespaces in XML messages is essential for ensuring interoperability between different services in a SOA. It helps services understand the structure and meaning of XML data exchanged between them, even if they have different XML schemas.

By using target namespaces and XML namespaces effectively in SOA, you can achieve a high degree of flexibility and compatibility when integrating diverse services within your architecture.

->It’s like package in java (namespace of your xml file in schema.)

if anyone wanted to use particular xsd then we have to call by using that target namespace.

-> along with schema we use target namespace.

ex: <schema targetnamespace="http://empinfo.com">

->It is the namespace that is going to be assigned to the schema you are crating.

-> Each schema has one target namespace and possibly many source namespaces.

Ex:

package a; // Target Namespace

Class One{}

package b: //Target Namespace

import a.One

Class Two{}

Ex:

http://www.amazon.com/order

http://www.ebay.com/order

Prefix: we can use short-cut for namespace and later on define inside element

prefix:

xmlns:amz="http://www.amazon.com/order"

xmlns:eby="http://www.ebay.com/order"

EX:

<order xmlns:amz="http://www.amazon.com/order">

<amz:lineitem/>

<amz:shippingaddress/> 

 

<order xmlns:eby="http://www.ebay.com/order">

<eby:lineitem/>

<eby:shippingaddress/>

root element : <schema> </schema>

Ex: <schema targetNamespace="http://empinfo.com">

                <element name="employee">

                <complex type>

                <sequence>

                <element name="empName" type="string"/>

                <element name="empId" type="integer"/>

                <element name="salary" type="decimal"/>

                </sequence>

                </complex type>

                </schema>        

xml:  <e:employee xmlns:e="http://empinfo.com">

                                <e:empName>abc</e:mpName>

                                <e:empId>123</e:empId>

                                <e:salary>1000</e:salary>

elementDromDefault=”qualified”

The attribute elementFormDefault with the value "qualified" is typically used in XML Schema Definition (XSD) to specify whether elements declared in the schema should be considered as qualified or unqualified within the target namespace.

elementFormDefault="qualified":

When you set elementFormDefault to "qualified" in an XSD, it means that all elements declared in the schema are considered qualified. This means that they are associated with the target namespace of the schema itself. When you define elements in the schema, you must use a namespace prefix to specify their namespace.

Example:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://example.com/mynamespace">
    <xsd:element name="myElement">
        <!-- This element is qualified with the target namespace -->
    </xsd:element>
</xsd:schema>

In this example, "myElement" is considered qualified and belongs to the "http://example.com/mynamespace" namespace.

elementFormDefault="unqualified":

Conversely, if you set elementFormDefault to "unqualified," it means that elements declared in the schema are considered unqualified by default. This means they are not associated with the target namespace of the schema, and they can be used without a namespace prefix in XML documents.

Example:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://example.com/mynamespace">
    <xsd:element name="myElement">
        <!-- This element is unqualified and can be used without a namespace prefix -->
    </xsd:element>
</xsd:schema>

In this example, "myElement" is considered unqualified and doesn't have to be prefixed with the target namespace when used in XML documents.

The choice between "qualified" and "unqualified" for elementFormDefault depends on your schema design and whether you want elements to be associated with the target namespace or not. It's important to make this choice carefully to ensure that your XML documents are correctly validated against the schema.

Q) What will happen if we remove default namespace?

Default namespace is used to make element as unique. nothing will happen if you remove default namespace provided all elements of all your schema have different name.

Target namespace is used for import and include properties if you wish to use schema in other schema or WSDLs.

Namespaces in XML Schema:

XML Schema (XSD) allows you to define and validate elements and attributes in specific namespaces. You can specify the target namespace of an XML Schema to indicate which namespace it governs.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns">
    <!-- Define elements and attributes within this namespace -->
</xs:schema>

Namespaces in XML are essential for ensuring that elements and attributes are correctly interpreted and preventing naming conflicts in documents that combine content from various sources or use different XML vocabularies. They enable interoperability and extensibility in XML documents and data interchange.

include & import:

In XML, include and import are mechanisms used in XML Schema Definition (XSD) to manage modular schemas and namespaces. These constructs allow you to reuse schema components across multiple schemas, promoting modularity and maintainability.

include Directive:The include directive is used to incorporate schema components from an external schema file into the current schema. It is typically used when you want to reuse components such as complex types, simple types, or elements from another schema within your current schema.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="external-schema.xsd"/>
  <!-- Define your schema components here -->
</xs:schema>

In the above example, the include directive is used to include components from the external-schema.xsd file into the current schema.

import Directive:

The import directive is used when you want to import schema components from a different namespace into the current schema. It allows you to use components defined in an external schema with a different target namespace.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://example.com/external" schemaLocation="external-schema.xsd"/>
  <!-- Define your schema components here -->
</xs:schema>

In the above example, the import directive is used to import components from the external-schema.xsd file with the target namespace http://example.com/external into the current schema.

include vs. import:

include is used to include components from a schema with the same namespace.

import is used to import components from a schema with a different namespace.

Multiple include and import Statements:

You can have multiple include and import statements in a schema to bring in components from multiple external schemas or files.

Namespace Declaration:

Ensure that the appropriate namespace declarations are present in both the external schema and the importing schema for correct referencing of components.

Using include and import directives in XML Schema allows for modular schema design, making it easier to manage complex schemas and collaborate on large XML projects involving multiple namespaces and components.

Handling Namespaces

You need to create namespaces in the output document using the <xsl:namespace> element. In addition, you need to create an alias for the namespace of the source document in the resultant document. You need to access the namespace URI of the source document using the namespace-uri() function.

Creating Namespaces

The XSLT 2.0 specification defines the use of the <xsl:namespace> element to create namespaces in the resultant document. The syntax for the <xsl:namespace> element is:

<xsl:namespace name ={ ncname }> <!-- Content: sequence-constructor --></xsl:namespace>

The attribute name contains the name of the namespace node. If the name attribute contains an empty string, the namespace is considered as a default namespace of the element.

Accessing Namespaces

To access the namespace in the source document, use the namespace-uri() function. This function returns the namespace URI of nodes in the source document. The syntax of the namespace-URI function is:

namespace-uri() namespace-uri(node)

When you pass the node set of a source document as an argument to the namespace-uri() function, the function returns the namespace value to the resultant document, depending on the type of the node. If the node set does not contain any node, an empty string is returned. If the node set contains more than one node, the namespace of the first node of the node set in the document is returned.

If the node type returns values, such as root, text, processing-instruction, or comment, the namespace-uri() returns an empty string. If the node type is an element and it contains namespace as the prefix, the corresponding namespace URI is returned to the resultant document. If the node type is an attribute, the corresponding namespace URI is returned. If the attribute name does not contain a prefix, an empty string is returned.

the namespace-uri() function to return the namespace URI of the source document to each element in the resultant document.

Deleting Namespaces

During the transformation process, you need to remove the namespace URI and its prefix from the source document and generate the remaining part of the resultant document.

Note

You can also use the xsl:exclude-result-prefixes attribute to remove the namespaces from the source document. This attribute is specified in the <xsl:stylesheet> element and you cannot use them to remove actual namespaces used in the resultant document.

No comments:

Post a Comment

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...