21
WEB PROGRAMMING MODULE 1 Namespaces: Organizing Elements with Namespaces 1. Namespaces and URI 2. Namespace Declarations 3. Examples 4. Types: Default and Explicit Namespaces UDAY BABU P ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING VIDYA ACADEMY OF SCIENCE AND TECHNOLOGY

XML NAMESPACE

Embed Size (px)

DESCRIPTION

XML NAMESPACE

Citation preview

  • WEB PROGRAMMING

    MODULE 1Namespaces: Organizing Elements with Namespaces

    1. Namespaces and URI2. Namespace Declarations

    3. Examples4. Types: Default and Explicit Namespaces

    UDAY BABU P

    ASSISTANT PROFESSOR

    DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

    VIDYA ACADEMY OF SCIENCE AND TECHNOLOGY

  • NAMESPACES: Definition & syntax for declaring namespaces

    An XML namespace is a collection of element and attribute names used in XML documents.

    The name of a namespace usually has the form of a uniform resource identifier(URI).

    A namespace for an element is declared as the value of the attribute xmlns.

    The form of a namespace declaration for an element is

  • NAMESPACE in detail

    For example, consider the element 'name' in an XML document. The name element could contain the name of a person, an organization, a country and so on. One of the name element in the XML document could store customer name, while another name element could store product names. This conflict can be resolved using namespaces. Namespaces can be used to uniquely identify each name element.

    Namespaces help the applications using XML documents to clearly identify its elements and attributes, resolving any conflicts with identically named elements and attributes in other XML documents.

    Namespaces are thus used to organize the elements and attributes of an XML documents into separate collections.

  • NAME SPACES in detailFor example, When an XML document have a tag for a category of furniture and a HTML tag for information tables. Clearly, software systems that process this XML document must be capable of unambiguously recognizing the element names in the XML document.

    KeralaRosewoodTamil NaduTeak

    $123Teaksquare

  • Solution using NAME SPACES

    Namespaces is used to resolve the conflict as follows:-

    KeralaRosewoodTamil NaduTeak

    $123Teaksquare

  • Namespaces and NEED FOR NAMESPACES

    XML Namespace is a mechanism to avoid name conflicts by differentiating elements or attributes within an XML document that may have identical names, but different definitions.

    XML namespaces are used for providing uniquely named elements and attributes in an XML document.

    A namespace name is a uniform resource identifier (URI) XML Namespace creates a qualified name for an XML

    Element. This provides a unique identifier for the element or attribute. The namespace is identified as a URI e.g.http://www.somecomapny.com/order/ADDR

  • NAMESPACE and URI

    URI Uniform Resource Identifier is a generic term for identifying namespaces (or any resources) and addresses on the world wide web.URL Universal Resource Locator is a unique address that identifies a web page.A URL is a URI that happens to be the Internet address of some resource.

    Namespaces are identified by a URI because it is guaranteed to be unique which means that an associated namespace is also unique.URL that you use to identify a namespace does not need to exist. The URL associated with a namespace only needs to be a unique name to identify the namespace.

  • NAMESPACE declaration

    The form of a namespace declaration for an element is

    The square brackets indicate that what is within them is

    optional. xmlns attribute identifies the namespace and makes

    association between a prefix and the created namespace. The prefix, if included, is the name that must be attached to

    the names in the declared namespace. If the prefix is not included, the namespace is the default for the document.

    Prefix and colon is needed for the opening as well as ending tags for each element .

  • NAMESPACE DECLARATION

    A prefix is used for two reasons. First, most URIs are

    too long to be typed on every occurrence of every name from the namespace.

    Second, a URI includes characters that are invalid in XML.

    Note that the element for which a namespace is declared is usually the root of a document.

  • EXAMPLE FOR NAMESPACES

    As an example of a prefixed namespace declaration, consider the following:

    Within the birds element, including all of its children elements, the

    names from the given namespace must be prefixed with bd, as in the following element:

  • EXAMPLE FOR NAMESPACES

    If an element has more than one namespace declaration, they are declared as in the following example:

    One of the namespaces can be specified as the default by omitting the prefix in any namespace declaration (as in the example in the next slide).

  • EXAMPLE FOR NAMESPACES The following example declares two namespaces. The first is

    declared to be the default namespace; the second defines the prefix, cap. One of the namespaces can be specified as the default by omitting the prefix in any namespace declaration as can be seen in this example.

    Each state element has name and population child elements from both namespaces.

  • Example for NAMESPACES

    XHTML documents declares the xmlns namespace on the root element, html:

    This declaration defines the default namespace

    for XHTML documents, which is http://www.w3.org/1999/xhtml.

  • DEFAULT NAMESPACE

    Default namespace is applied to all the elements and nested elements beneath the element that declares the namespace. Prefix may be omitted. A default namespace for an entire XML document can be selected by using the xmlns attribute in the documents root element.

  • Example for default namespaces

  • EXAMPLE FOR DEFAULT NAMESPACE : A default namespace is created by assigning URL

    www.GosselinConsulting.com/ns/renovation to the xmlns attribute in the root element

    plumbing$15,000

    electrical$11,000

  • Example for DEFAULT NAMESPACE : Here there are two default namespaces, one for the plumbing element and one for the electrical

    element

    plumbing$15,000

    electrical$11,000

  • Explicit NAMESPACE

    Namespaces that are assigned to individual elements in an XML document are called explicit namespaces.

    To explicitly declare a namespace for a specific element in an XML document, you must assign a prefix to the namespace declaration using the following syntax

    Prefix and colon is needed for the opening as well as ending tags for each element.

  • EXAMPLE FOR EXPLICIT NAMESPACE: A default and an explicit namespace within the root element

    'renovation' of the XML document

    plumbing$15,000

    electrical$11,000

  • NAMESPACE : any elements that do not contain an explicit namespace declaration belong to default namespace

    january 27, 2005 74.00 cm20

    january 28, 2005 50.00.1 cm26

  • Example for Explicit NAMESPACE

    january 27, 2005 74.00 cm20

    january 28, 2005 50.00.1 cm26

    Explicit namespace for weather in Los Angeles

    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21