New Perspectives on XML, 2nd Edition - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

New Perspectives on XML, 2nd Edition

Description:

USING NAMESPACES WITH ATTRIBUTES. No element may contain two attributes with the same name. ... two qualified attribute names with the same local part, pointing ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 34
Provided by: willa47
Category:

less

Transcript and Presenter's Notes

Title: New Perspectives on XML, 2nd Edition


1

TUTORIAL 2
  • WORKING WITH NAMESPACES

2
COMBINING XML VOCABULARIES
  • A document that combines several vocabularies is
    known as a compound document

3
WORKING WITH NAMESPACES
  • Name collision occurs when elements from two or
    more documents share the same name.
  • Name collision is not a problem if you are not
    concerned with validation. The document content
    only needs to be well-formed.
  • However, name collision will keep a document from
    being validated.

4
NAME COLLISION
  • This figure shows name collision

5
DECLARING A NAMESPACE
  • A namespace is a defined collection of element
    and attribute names.
  • Names that belong to the same namespace must be
    unique. Elements can share the same name if they
    reside in different namespaces.
  • Namespaces must be declared before they can be
    used.

6
DECLARING A NAMESPACE
  • A namespace can be declared in the prolog or as
    an element attribute. The syntax for an attribute
    used to declare a namespace in the prolog is
  • xmlnsprefixURI
  • Where URI is a Uniform Resource Identifier that
    assigns a unique name to the namespace, and
    prefix is a string of letters that associates
    each element or attribute in the document with
    the declared namespace.

7
DECLARING A NAMESPACE
  • For example,
  • ltmode1 xmlnsmodhttp//jacksonelect.com/models
    gtgt
  • Declares a namespace with the prefix mod and
    the URI http//jacksonelect.com/models
  • The URI is not a Web address. A URI identifies a
    physical or an abstract resource.

8
URIs, URLs, AND URNs
  • A physical resource is a resource one can access
    and work with such as a file, a Web page, or an
    e-mail address. A URL is one type of URI.
  • An abstract resource is one that doesnt have any
    physical existence, the URI is used as an
    identifier or an ID.

9
URIs, URLs, AND URNs
  • A proposed type of URI is the URN or Universal
    Resource Name. A URN is a persistent resource
    identifier, meaning the user need only know the
    name of a resource. An agency would then retrieve
    a copy of the resource independent of its
    location.
  • URNs take the form
  • urnNIDNSS

10
APPLYING A NAMESPACE TO AN ELEMENT
  • Once it has been declared and its URI specified,
    the namespace is applied to elements and
    attributes by inserting the namespace prefix
    before each element name that belongs to the
    namespace.
  • ltprefixelementgt
  • content
  • lt/prefixelementgt
  • Here, prefix is the namespace prefix and element
    is the local part of the element name.

11
APPLYING A NAMESPACE TO AN ELEMENT
  • Prefixed names are called qualified names and an
    element name without a namespace prefix is called
    an unqualified name.
  • Qualified names can be added to a document using
    code entered directly into the document.
  • However, the more common way is to add the xmlns
    attribute to an element.

12
DECLARING A NAMESPACE AS AN ELEMENT ATTRIBUTE
  • The syntax is
  • xmlnsprefixURI
  • Where prefix and URI are the prefix and URI for
    the namespace.

13
DECLARING A NAMESPACE AS AN ELEMENT ATTRIBUTE
  • For example, the code
  • ltmodmodel xmlnsmod"http//jacksonelect.com/mode
    ls"gt
  • ltmodtitlegtLaser4C (PR205)lt/modtitlegt
  • ltmoddescriptiongtEntry level color laser
    printerlt/moddescriptiongt
  • ltmodtypegtcolor laserlt/modtypegt
  • ltmodorderedgt320lt/modorderedgt
  • ltmodparts list"chx201,fa100-5,eng005-2,cbx-450V4
    ,tn01-53" /gt
  • lt/modmodelgt

14
DECLARING A NAMESPACE AS AN ELEMENT ATTRIBUTE
  • applies the namespace http//jacksonelect.com/mod
    els namespace to the model element and all of its
    child elements.
  • While the mod prefix was only added to the
    model element name, the XML parser considers the
    other elements parts of the model namespace and
    they inherit the namespace.

15
DECLARING A NAMESPACE AS AN ELEMENT ATTRIBUTE
  • They are unqualified elements, though, because
    they lack a namespace prefix.
  • Declaring a namespace by adding it as an
    attribute of the documents root element places
    all elements in the namespace.
  • All elements thus are children of the root
    element.

16
DECLARING A DEFAULT NAMESPACE
  • You can specify a default namespace by omitting
    the prefix in the namespace declaration.
  • The element containing the namespace attribute
    and all of its child elements are assumed to be
    part of the default namespace.

17
USING NAMESPACES WITH ATTRIBUTES
  • Attributes, like elements, can become qualified
    by adding the namespace prefix to the attribute
    name. For example,
  • ltelement xmlnsurigt contentlt/elementgt

18
USING NAMESPACES WITH ATTRIBUTES
  • No element may contain two attributes with the
    same name.
  • No element may contain two qualified attribute
    names with the same local part, pointing to
    identical namespaces, even if the prefixes are
    different.

19
ADDING A NAMESPACE TO A STYLE SHEET DECLARING A
NAMESPACE
  • To declare a namespace in a style sheet, you add
    the following rule to the style sheet file
  • _at_namespace prefix url(uri)
  • Where prefix is the namespace previx and uri is
    the URI of the namespace
  • Example
  • _at_namespace mod url(http//jacksonelect.com/models)

20
APPLYING A NAMESPACE TO A SELECTOR
  • Once youve declared a namespace in a style
    sheet, you can associate selectors with that
    namespace using the syntax
  • prefixselector attribute1value1
    attribute2value2
  • For example
  • modtitle width 150px
  • You also can use the wildcard symbol () to apply
    a style to any element within a namespace or to
    elements across different namespaces

21
DEFINING NAMESPACES WITH THE ESCAPE CHARACTER
  • Not all browsers support the use of the
    _at_namespace rule
  • A proposal implement in the Internet Explorer
    browser was to insert the backslash escape
    character before the namespace prefix in CSS
    style sheets
  • prefix\selector attribute1value1
    attribute2value2
  • Browsers like Firefox, Opera, and Netscape do not
    support this method with XML documents

22
DECLARING AND APPLYING A NAMESPACE IN A STYLE
SHEET
  • To declare a namespace in a CSS style sheet, add
    the following rule before any style declarations
  • _at_namespace prefix url(uri)
  • where prefix is the namespace prefix and uri is
    the namespace URI. If no prefix is specified, the
    namespace URI is the default namespace for
    selectors in the style sheet.
  • To apply a namespace to a selector, use the form
    prefixselector attribute1value1
    attribute2value2 ...
  • where prefix is the namespace prefix and
    selector is a selector for an element or group of
    elements in the document.
  • For Internet Explorer browsers, use the following
    form to apply a namespace to a selector
  • prefix\selector attribute1value1
    attribute2value2 ...

23
COMBINING STANDARD VOCABULARIES
  • Standard vocabularies may be combined within
    single documents

24
CONVERTING HTML TO XHTML
  • Use your text editor to open the reptxt.htm file
    from the tutorial.02x/tutorial folder. Enter your
    name and the date in the comment section at the
    top of the document. Save the file as report.htm.
  • Insert the following xml declaration as the very
    first line in the file (above the comment
    section)
  • lt?xml version"1.0" encoding"UTF-8"
    standalone"yes" ?gt
  • Add the following attribute to the opening lthtmlgt
    tag
  • xmlns"http//www.w3.org/1999/xhtml"

25
CONVERTING HTML TO XHTML
26
ADDING THE ELEMENTS OF THE PARTS VOCABULARY
  • Return to the order.xml file in your text editor.
  • Copy the parts element from the parts namespace,
    including all of the elements and contents it
    contains.
  • Return to the report.htm file in your text editor
    and paste the copied elements directly below the
    h2 heading Parts List.
  • Add the following attribute to the opening lthtmlgt
    tag
  • xmlnspa"http//jacksonelect.com/parts"
  • Below the link element that links the report.htm
    file to the report.css style sheet, insert the
    following link element
  • ltlink rel"stylesheet" href"parts.css"
    type"text/css" /gt
  • Save the changes and open the report.htm file in
    your Web browser

27
ADDING THE ELEMENTS OF THE PARTS VOCABULARY
28
DESCRIBING THE ITEMS IN THE PARTS LIST
  • Return to the report.htm file in your text
    editor.
  • Scroll down to the first title element in the
    parts namespace. Directly after the opening
    ltpatitlegt tag, insert the text
  • ltspangtTitlelt/spangt
  • Directly after the opening ltpadescriptiongt tag
    in the next line, insert the text
  • ltspangtDescriptionlt/spangt
  • Directly after the opening ltpainstockgt tag in
    the following line, insert the text
  • ltspangtParts in Stocklt/spangt
  • Repeat the previous 3 steps, as necessary

29
DESCRIBING THE ITEMS IN THE PARTS LIST
30
DESCRIBING THE ITEMS IN THE PARTS LIST
31
ADDING ELEMENTS FROM THE MODELS VOCABULARY
  • Return to the report.htm file in your text editor
    and add the following namespace declaration to
    the opening lthtmlgt tag
  • xmlnsmod"http//jacksonelect.com/models"
  • Add the following link to the documents head
  • ltlink rel"stylesheet" href"model.css"
    type"text/css" /gt
  • In the table cell directly after the Title table
    heading, insert the element
  • ltmodtitlegtLaser4C (PR205)lt/modtitlegt

32
ADDING ELEMENTS FROM THE MODELS VOCABULARY
  • In the table cell directly after the Description
    table heading, insert the element
  • ltmoddescriptiongtEntry level color laser
    printerlt/moddescriptiongt
  • In the table cell directly after the Type table
    heading, insert the element
  • ltmodtypegtcolor laserlt/modtypegt
  • In the table cell directly after the Items to be
    Built table heading, insert the element
  • ltmodorderedgt320lt/modorderedgt

33
ADDING ELEMENTS FROM THE MODELS VOCABULARY
Write a Comment
User Comments (0)
About PowerShow.com