SlideShare a Scribd company logo
1 of 14
Download to read offline
18
                                                                       CHAPTER


Namespaces
                                                                      ✦    ✦       ✦     ✦

  N      o XML is an island. While you might find it useful to
                                                                      In This Chapter
         write documents that use a single markup vocabulary,
  (witness the baseball examples of Chapters 4 and 5) it’s even
                                                                      What is a
  more useful to mix and match tags from different XML appli-
                                                                      namespace?
  cations. For example, you may want to include a BIOGRAPHY
  element in each PLAYER element. Since the biography con-
                                                                      Namespace syntax
  sists basically of free-form, formatted text, it’s convenient to
  write it in well-formed HTML without reinventing all the tags
                                                                      Namespaces in DTDs
  for paragraphs, line breaks, list items, bold elements, and so
  forth from scratch.
                                                                      ✦    ✦       ✦     ✦
  The problem, however, is that when mixing and matching tags
  from different XML applications, you’re likely to find the same
  tag used for two different things. Is a TITLE the title of a page
  or the title of a book? Is an ADDRESS the mailing address of a
  company or the email address of a Webmaster? Namespaces
  disambiguate these instances by associating a URI with each
  tag set, and attaching a prefix to each elementto indicate which
  tag set it belongs to. Thus, you can have both BOOK:TITLE and
  HTML:TITLE elements or POSTAL:ADDRESS and HTML:ADDRESS
  elements instead of just one kind of TITLE. or ADDRESS. This
  chapter shows you how to use namespaces.



What Is a Namespace?
  XML enables developers to create their own markup languages
  for their own projects. These languages can be shared with
  individuals working on similar projects all over the world. One
  specific example of this is XSL. XSL is itself an XML application
  for styling XML documents. The XSL transformation language
  must output arbitrary, well-formed XML, possibly including
  XSL itself. Thus, you need a clear-cut means of distinguishing
  between those XML elements that are XSL transformation
  instructions and output XML elements, even if they have the
  same names!
618   Part IV ✦ Supplemental Technologies



                Namespaces are the solution. They allow each element and attribute in a document
                to be placed in a different namespace. The XML elements that comprise XSL trans-
                formation instructions are placed in the http://www.w3.org/XSL/Transform/1.0
                namespace. The XML elements that are part of the output can reside in some other
                convenient namespace like http://www.w3.org/TR/REC-html40 or http://www.
                w3.org/XSL/Format/1.0. The exact namespace isn’t even important as long as it’s
                different.

                     If you’re familiar with the concept of namespaces as used in C++ and other pro-
      Caution
                     gramming languages, you need to put aside your preconceptions before reading
                     further. XML namespaces are similar to, but not quite the same as the namespaces
                     used in programming. In particular, XML namespaces do not necessarily form a set
                     (a collection with no duplicates).

                Listing 15-2, a transformation from a source vocabulary to XSL formatting objects,
                initially appeared in Chapter 15, XSL Formatting Objects. It displays an XSL style
                sheet that converts from input XML to XSL formatting objects. The formatting
                engine distinguishes between elements that are XSL instructions and literal data
                for the output by using namespaces. Any element in the http://www.w3.org/
                XSL/Transform/1.0 namespace represents a transformation instruction. Any
                element in the http://www.w3.org/XSL/Format/1.0 namespace comprises
                part of the output.

                  <?xml version=”1.0”?>
                  <xsl:stylesheet
                    xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”
                    xmlns:fo=”http://www.w3.org/XSL/Format/1.0”
                    result-ns=”fo” indent-result=”yes”>

                     <xsl:template match=”/”>
                       <fo:root xmlns:fo=”http://www.w3.org/XSL/Format/1.0”>

                          <fo:layout-master-set>
                            <fo:simple-page-master page-master-name=”only”>
                              <fo:region-body/>
                            </fo:simple-page-master>
                          </fo:layout-master-set>

                          <fo:page-sequence>

                           <fo:sequence-specification>
                            <fo:sequence-specifier-single page-master-name=”only”/>
                           </fo:sequence-specification>

                            <fo:flow>
                              <xsl:apply-templates select=”//ATOM”/>
                            </fo:flow>

                          </fo:page-sequence>

                       </fo:root>
                     </xsl:template>
619
                                                       Chapter 18 ✦ Namespaces



     <xsl:template match=”ATOM”>
       <fo:block font-size=”20pt” font-family=”serif”>
         <xsl:value-of select=”NAME”/>
       </fo:block>
     </xsl:template>

  </xsl:stylesheet>

More specifically, these elements exist in the http://www.w3.org/XSL/Transform/
1.0 namespace and are XSL instructions:

   ✦ stylesheet
   ✦ template
   ✦ apply-templates
   ✦ value-of

These elements, in the http://www.w3.org/XSL/Format/1.0 namespace, are XSL
formatting objects and part of the output:

   ✦ root
   ✦ layout-master-set
   ✦ simple-page-master
   ✦ region-body
   ✦ sequence-specification
   ✦ sequence-specifier-single
   ✦ page-sequence
   ✦ block

The four elements with the xsl prefix have the qualified names beginning with
the prefix:

   ✦ xsl:stylesheet
   ✦ xsl:template
   ✦ xsl:apply-templates
   ✦ xsl:value-of

However, their full names use the URL rather than the prefix:

   ✦ http://www.w3.org/XSL/Transform/1.0:stylesheet
   ✦ http://www.w3.org/XSL/Transform/1.0:template
   ✦ http://www.w3.org/XSL/Transform/1.0:apply-templates
   ✦ http://www.w3.org/XSL/Transform/1.0:value-of
620   Part IV ✦ Supplemental Technologies



                In essence, the shorter qualified names are nicknames that are used only within the
                document because URLs often contain characters like ~, %, and / that aren’t legal in
                XML names. However, qualified names do make documents a little easier to type
                and read.

                     Namespaces in an XML is an official W3C recommendation. The W3C considers it
      Caution
                     complete, aside from possible minor errors and elucidations. Nonetheless, of all
                     the XML specifications from the W3C, this one is the most controversial. Many
                     people feel very strongly that this standard contains fundamental flaws. The main
                     objection argues that namespaces are, in practice, incompatible with DTDs and
                     validation. While I don’t have a strong opinion on this one way or the other, I do
                     question the wisdom of publishing a standard when nothing approaching a con-
                     sensus has been reached. Namespaces are a crucial part of many XML related
                     specifications such as XSL and XHTML, so you need to understand them.
                     Nonetheless, a lot of developers and authors have chosen to ignore this specifica-
                     tion for their own work.




      Namespace Syntax
                Namespaces have been crafted to layer on top of the XML 1.0 specification. An XML
                1.0 processor that knows nothing about namespaces can still read a document that
                uses namespaces, and will not find any errors. Documents that use namespaces do
                not break existing XML parsers (at least ones that don’t check for validity); and
                users don’t have to wait for notoriously unpunctual software companies to release
                expensive upgrades before using namespaces.


                Definition of Namespaces
                Namespaces are defined using an xmlns:prefix attribute on the applicable
                elements they apply to. prefix is replaced by the actual prefix used for the
                namespace. The value of the attribute is the URI of the namespace. For example,
                this xsl:stylesheet tag associates the prefix xsl with the URI http://www.
                w3.org/XSL/Transform/1.0.

                  <xsl:stylesheet
                  xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”>

                The xsl prefix can then be attached to the local element and attribute names within
                the xsl:stylesheet element to identify them as belonging to the http://www.w3.
                org/XSL/Transform/1.0 namespace. The prefix is separated from the local name
                by a colon. Listing 14-2, a basic XSL style sheet for the periodic table, which was first
                shown in Chapter 14, XSL Transformations, demonstrates by using the xsl prefix on
                the stylesheet, template, and apply-templates elements.

                  <?xml version=”1.0”?>
                  <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”>
621
                                                                   Chapter 18 ✦ Namespaces



               <xsl:template match=”PERIODIC_TABLE”>
                 <html>
                   <xsl:apply-templates/>
                 </html>
               </xsl:template>

               <xsl:template match=”ATOM”>
                 <P>
                   <xsl:apply-templates/>
                 </P>
               </xsl:template>

            </xsl:stylesheet>

          The URI that defines a namespace is purely formal. Its only purpose is to group and
          disambiguate element and attribute names in the document. It does not necessarily
          point to anything. In particular, there is no guarantee that the document at the URI
          describes the syntax used in the document; or, for that matter, that any document
          exists at the URI. Having said that, if there is a canonical URI for a particular XML
          application, then that URI is a good choice for the namespace definition.

          A namespace prefix can be any legal XML name that does not contain a colon. Recall
          from Chapter 6, Well-Formed XML Documents, that a legal XML name must begin with
          a letter or an underscore (_). Subsequent letters in the name may include letters,
          digits, underscores, hyphens, and periods. They may not include whitespace.

               There are two prefixes that are specifically disallowed, xml and xmlns. The xml
 Note
               prefix is defined to refer to http://www.w3.org/XML/1998/namespace. The
               xmlns prefix is used to bind elements to namespaces, and is therefore not avail-
               able as a prefix to be bound to.

          Other than disallowing the colon character in XML names (aside from its use in
          separating prefixes and local names) namespaces have no direct effect on stan-
          dard XML syntax. A document that uses namespaces must still be well-formed
          when read by a processor that knows nothing about namespaces. If the document
          is to be validated, then it must be validated without specifically considering the
          namespaces. To an XML processor, a document that uses namespaces is just a
          funny-looking document in which some of the element and attribute names may
          have a single colon.

               Namespaces do present problems for validation. If a DTD was written without
Caution
               namespace prefixes, then it must be rewritten using the namespace prefixes
               before it can be used to validate documents that use the prefixes. For example,
               consider this element declaration:
                  <!ELEMENT DIVISION (DIVISION_NAME, TEAM+)>
               You have to rewrite it like this if the elements are all given the bb namespace
               prefix:
                  <!ELEMENT bb:DIVISION (bb:DIVISION_NAME, bb:TEAM+)>
622   Part IV ✦ Supplemental Technologies



               This means that you cannot use the same DTD for both documents with name-
               spaces and documents without, even if they use essentially the same vocabulary.
               In fact, you can’t even use the same DTD for documents that use the same tag sets
               and namespaces, but different prefixes, because DTDs are tied to the actual pre-
               fixes rather than the URIs of the namespaces.


          Multiple Namespaces
          Listing 14-2 did not actually place the HTML elements in a namespace, but that’s not
          hard to do. Listing 18-1 demonstrates. Just as xsl is the conventional prefix for XSL
          transformation instructions, html is the conventional prefix for HTML elements. In
          this example, the xsl:stylesheet element declares two different namespaces, one
          for XSL and one for HTML.



             Listing 18-1: An XSL stylesheet that uses the http://www.w3.
                           org/TR/REC-html40 namespace for output
             <?xml version=”1.0”?>
             <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”
                             xmlns:html=”http://www.w3.org/TR/REC-html40”>

               <xsl:template match=”PERIODIC_TABLE”>
                 <html:html>
                   <xsl:apply-templates/>
                 </html:html>
               </xsl:template>

               <xsl:template match=”ATOM”>
                 <html:p>
                   <xsl:apply-templates/>
                 </html:p>
               </xsl:template>

             </xsl:stylesheet>




          While it’s customary, and generally useful to place the xmlns attribute on the root
          element, it can appear on other elements as well. In this case, the namespace prefix
          is understood only within the element where it’s declared. Consider Listing 18-2.
          The html prefix is legal only in the xsl:template element where it’s declared. It
          can’t be applied in other template rules, unless they separately declare the html
          namespace.
623
                                                       Chapter 18 ✦ Namespaces




  Listing 18-2: An XSL style sheet with the http://www.w3.org/
                TR/REC-html40 namespace declared in the
                template rules
  <?xml version=”1.0”?>
  <xsl:stylesheet
  xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”>

     <xsl:template match=”PERIODIC_TABLE”
                    xmlns:html=”http://www.w3.org/TR/REC-html40”>
       <html:html>
         <xsl:apply-templates/>
       </html:html>
     </xsl:template>

     <xsl:template match=”ATOM”>
       <p>
         <xsl:apply-templates/>
       <p>
     </xsl:template>

  </xsl:stylesheet>



You can redefine a namespace in a child element. For example, consider the XSL
style sheet in Listing 18-3. Here, the xsl prefix appears in different elements to
refer to http://www.w3.org/XSL/Transform/1.0 and http://www.w3.org/
XSL/Format/1.0 alternately. Although every element has the prefix xsl, the XSL
transformation instructions and the XSL formatting objects still reside in different
namespaces because the meaning of the xsl prefix changes from element to
element.


  Listing 18-3: Redefining the xsl prefix
  <?xml version=”1.0”?>
  <xsl:stylesheet
    xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”>

     <xsl:template match=”/”>
       <xsl:root xmlns:xsl=”http://www.w3.org/XSL/Format/1.0”>

          <xsl:layout-master-set>
            <xsl:simple-page-master page-master-name=”only”>
              <xsl:region-body/>
            </xsl:simple-page-master>

                                                                         Continued
624   Part IV ✦ Supplemental Technologies




             Listing 18-3 (continued)
                    </xsl:layout-master-set>

                    <xsl:page-sequence>

                    <xsl:sequence-specification>
                     <xsl:sequence-specifier-single page-master-name=”only”/>
                    </xsl:sequence-specification>

                      <xsl:flow>
                        <xsl:apply-templates select=”//ATOM”/
                          xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”/>
                      </xsl:flow>

                    </xsl:page-sequence>

                 </xsl:root>
               </xsl:template>

               <xsl:template match=”ATOM”>
                 <xsl:block font-size=”20pt” font-family=”serif”
                    xmlns:xsl=”http://www.w3.org/XSL/Format/1.0”>
                   <xsl:value-of select=”NAME”
                     xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”/>
                 </xsl:block>
               </xsl:template>

             </xsl:stylesheet>




          This is, however, needlessly confusing and I strongly recommend that you avoid it.
          There are more than enough prefixes to go around, and almost no need to reuse
          them within the same document. The main importance of this is if two different
          documents from different authors that happen to reuse a similar prefix are being
          combined. This is a good reason to avoid short prefixes like a, m, and x that are
          likely to be reused for different purposes.


          Attributes
          Since attributes belong to particular elements, they’re more easily disambiguated
          from similarly named attributes without namespaces. Consequently, it’s not nearly as
          essential to add namespaces to attributes as to elements. For example, the April 21,
          1999 working draft of the XSL specification requires that all XSL transformation ele-
          ments fall in the http://www.w3.org/XSL/Transform/1.0 namespace. However,
          it does not require that the attributes of these elements be in any particular name-
          space. (In fact, it requires that they not be in any namespace.) Nonetheless, you
          can attach namespace prefixes to attributes if necessary. For example, this PLAYER
625
                                                                  Chapter 18 ✦ Namespaces



       element and all its attributes live in the http://metalab.unc.edu/xml/baseball
       namespace.

         <bb:PLAYER xmlns:bb=”http://metalab.unc.edu/xml/baseball”
          bb:GIVEN_NAME=”Tom” bb:SURNAME=”Glavine”
          bb:POSITION=”Starting Pitcher” bb:GAMES=”33”
          bb:GAMES_STARTED=”33” bb:WINS=”20” bb:LOSSES=”6” bb:SAVES=”0”
          bb:COMPLETE_GAMES=”4” bb:SHUT_OUTS=”3” bb:ERA=”2.47”
          bb:INNINGS=”229.1” bb:HOME_RUNS_AGAINST=”13”
          bb:RUNS_AGAINST=”67” bb:EARNED_RUNS=”63” bb:HIT_BATTER=”2”
          bb:WILD_PITCHES=”3” bb:BALK=”0” bb:WALKED_BATTER=”74”
          bb:STRUCK_OUT_BATTER=”157”/>

       This might occasionally prove useful if you need to combine attributes from two
       different XML applications on the same element.

       It is possible (though mostly pointless) to associate the same namespace URI with
       two different prefixes. There’s really no reason to do this. The only reason I bring it
       up here is simply to warn you that it is the full name of the attribute that must satisfy
       XML’s rules for an element not having more than one attribute with the same name.
       For example, this is illegal because bb:GIVEN_NAME and baseball:GIVEN_NAME are
       the same:

         <bb:PLAYER xmlns:bb=”http://metalab.unc.edu/xml”
                    xmlns:baseball=”http://metalab.unc.edu/xml”
          bb:GIVEN_NAME=”Hank” bb:SURNAME=”Aaron”
          baseball:GIVEN_NAME=”Henry” />

       On the other hand, the URI does not actually get checked to see what it points to.
       The URIs http://metalab.unc.edu/xml/ and http://www.metalab.unc.edu/
       xml/ point to the same page. However, this is legal:

         <bb:PLAYER xmlns:bb=”http://metalab.unc.edu/xml”
                    xmlns:baseball=”http://www.metalab.unc.edu/xml”
          bb:GIVEN_NAME=”Hank” bb:SURNAME=”Aaron”
          baseball:GIVEN_NAME=”Henry” />


       Default Namespaces
       In long documents with a lot of markup, all in the same namespace, you might
       find it inconvenient to add a prefix to each element name. You can attach a default
       namespace to an element and its child elements using an xmlns attribute with no
       prefix. The element itself, as well as all its children, are considered to be in the
       defined namespace unless they possess an explicit prefix. For example, Listing
       18-4 shows an XSL style sheet that does not prefix XSL transformation elements
       with xsl as is customary.

            Attributes are never in a default namespace. They must be explicitly prefixed.
Note
626   Part IV ✦ Supplemental Technologies




             Listing 18-4: An XSL stylesheet that uses default namespaces
             <?xml version=”1.0”?>
             <stylesheet
               xmlns=”http://www.w3.org/XSL/Transform/1.0”
               xmlns:fo=”http://www.w3.org/XSL/Format/1.0”
               result-ns=”fo”>

               <template match=”/”>
                 <fo:root xmlns:fo=”http://www.w3.org/XSL/Format/1.0”>

                   <fo:layout-master-set>
                     <fo:simple-page-master page-master-name=”only”>
                       <fo:region-body/>
                     </fo:simple-page-master>
                   </fo:layout-master-set>

                   <fo:page-sequence>

                    <fo:sequence-specification>
                     <fo:sequence-specifier-single page-master-name=”only”/>
                    </fo:sequence-specification>

                      <fo:flow>
                        <apply-templates select=”//ATOM”/>
                      </fo:flow>

                   </fo:page-sequence>

                 </fo:root>
               </template>

               <template match=”ATOM”>
                 <fo:block font-size=”20pt” font-family=”serif”>
                   <value-of select=”NAME”/>
                 </fo:block>
               </template>

             </stylesheet>




          Perhaps the best use of default namespaces attaches a namespace to every element
          in an existing document to which you’re now going to add tags from a different
          language. For instance, if you place some MathML in an HTML document, you only
          have to add prefixes to the MathML elements. You could put all the HTML elements
          in the http://www.w3.org/TR/REC-html40 namespace simply by replacing the
          <html> start tag with this tag:

             <html xmlns=”http://www.w3.org/TR/REC-html40”>
627
                                                         Chapter 18 ✦ Namespaces



You do not need to edit the rest of the file! The MathML tags you insert still need to
be in a separate namespace. However, as long as they aren’t mixed up with a lot of
HTML markup, you can simply declare an xmlns attribute on the root element of
the MathML. This defines a default namespace for the MathML elements that
override the default namespace of the document containing the MathML. Listing
18-5 demonstrates.


  Listing 18-5: A MathML math element embedded in a well-
                formed HTML document that uses namespaces
  <?xml version=”1.0”?>
  <html xmlns=”http://www.w3.org/TR/REC-html40”>
    <head>
      <title>Fiat Lux</title>
      <meta name=”GENERATOR” content=”amaya V1.3b” />
    </head>
    <body>

        <P>And God said,</P>

        <math xmlns=”http://www.w3.org/TR/REC-MathML/”>
          <mrow>
            <msub>
              <mi>&#x3B4;</mi>
              <mi>&#x3B1;</mi>
            </msub>
            <msup>
              <mi>F</mi>
              <mi>&#x3B1;&#x3B2;</mi>
            </msup>
            <mi></mi>
            <mo>=</mo>
            <mi></mi>
            <mfrac>
              <mrow>
                 <mn>4</mn>
                 <mi>&#x3C0;</mi>
              </mrow>
              <mi>c</mi>
            </mfrac>
            <mi></mi>
            <msup>
              <mi>J</mi>
              <mrow>
                 <mi>&#x3B2;</mi>
                 <mo></mo>
              </mrow>
            </msup>

                                                                           Continued
628   Part IV ✦ Supplemental Technologies




             Listing 18-5 (continued)
                   </mrow>
                 </math>

                 <P>and there was light</P>

               </body>
             </html>




          Here, math, mrow, msub, mo, mi, mfrac, mn, and msup are all in the http://www.w3.
          org/TR/REC-MathML/ namespace, even though the document that contains them
          uses the http://www.w3.org/TR/REC-html40 namespace.



      Namespaces in DTDs
          Namespaces do not get any special exemptions from the normal rules of well-
          formedness and validity. For a document that uses namespaces to be valid, the
          xmlns attributes must be declared in the DTD for those elements to which they’re
          attached. Furthermore, you must declare the elements and attributes using the
          prefixes they use in the document. For instance, if a document uses a math:subset
          element, then the DTD must declare a math:subset element, not merely a subset
          element. (Of course, these rules do not apply to the merely well-formed documents
          discussed thus far.) For example:

             <!ELEMENT math:subset EMPTY>

          Default attribute values and #IMPLIED attributes can help here. For example, this
          ATTLIST declaration places every math:subset element in the http://www.w3.
          org/TR/REC-MathML/ namespace unless specified otherwise in the document.

             <!ATTLIST math:subset
                     xmlns:math “http://www.w3.org/TR/REC-MathML/” #IMPLIED>

          When working with valid documents, default namespaces prove especially useful
          since they don’t require you to add prefixes to all the elements. Adding prefixes
          to elements from an XML application whose DTD doesn’t use prefixes will break
          validity.

          There are, however, clear limits to how far default namespaces will take you. In
          particular, they are not sufficient to differentiate between two elements that use
          an element name in incompatible ways. For example, if one DTD defines a HEAD as
          containing a TITLE and a META element, and another DTD defines a HEAD as con-
          taining #PCDATA, then you will have to use prefixes in the DTD and the document
          to distinguish the two different HEAD elements.
629
                                                               Chapter 18 ✦ Namespaces



      Two different development efforts are underway that may (or may not) eventually
      solve the problem of merging incompatible DTDs from different domains. XML
      schemas may provide a more robust replacement for DTDs. XML fragments may
      enable different documents to be combined with more distinction between which
      parts come from where. However, neither of these is even close to finished. Conse-
      quently, for now, merging incompatible DTDs will probably require you to rewrite
      the DTD and your documents to use prefixes.

           If you have a question about whether a document that uses namespaces is well-
Tip
           formed or valid, forget everything you know about namespaces. Simply treat the
           document as a normal XML document that happens to have some element and
           attribute names that contain colons. The document is as well-formed and valid as
           it is when you don’t consider namespaces.


Summary
      This chapter explained how to work with namespaces. In particular, you learned:

         ✦ Namespaces distinguish between elements and attributes of the same name
           from different XML applications.
         ✦ Namespaces are declared by an xmlns attribute whose value is the URI of the
           namespace. The document referred to by this URI need not exist.
         ✦ The prefix associated with a namespace is the part of the name of the xmlns
           attribute that follows the colon; for example xmlns:prefix.
         ✦ Prefixes are attached to all element and attribute names that belong to the
           namespace identified by the prefix.
         ✦ If an xmlns attribute has no prefix, it establishes a default namespace for that
           element and its child elements (but not for any attributes).
         ✦ DTDs must be written in such a fashion that a processor that knows nothing
           about namespaces can still parse and validate the document.

      The next chapter explores the Resource Description Framework, RDF, an XML
      application for encoding meta-data and information structures.

                                    ✦        ✦         ✦
XML Bible

More Related Content

What's hot (20)

Unit1
Unit1Unit1
Unit1
 
Day Of Dot Net Ann Arbor 2007
Day Of Dot Net Ann Arbor 2007Day Of Dot Net Ann Arbor 2007
Day Of Dot Net Ann Arbor 2007
 
XML Bible
XML BibleXML Bible
XML Bible
 
Using SPMetal for faster SharePoint development
Using SPMetal for faster SharePoint developmentUsing SPMetal for faster SharePoint development
Using SPMetal for faster SharePoint development
 
XML Bible
XML BibleXML Bible
XML Bible
 
Xmll
XmllXmll
Xmll
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
XML
XMLXML
XML
 
WEB PROGRAMMING
WEB PROGRAMMINGWEB PROGRAMMING
WEB PROGRAMMING
 
Xml data transformation
Xml data transformationXml data transformation
Xml data transformation
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Extensible Stylesheet Language
Extensible Stylesheet LanguageExtensible Stylesheet Language
Extensible Stylesheet Language
 
Xml
XmlXml
Xml
 
Extensible Stylesheet Language
Extensible Stylesheet LanguageExtensible Stylesheet Language
Extensible Stylesheet Language
 
XML
XMLXML
XML
 
A SAS&lt;sup>®&lt;/sup> Users Guide to Regular Expressions When the Data Resi...
A SAS&lt;sup>®&lt;/sup> Users Guide to Regular Expressions When the Data Resi...A SAS&lt;sup>®&lt;/sup> Users Guide to Regular Expressions When the Data Resi...
A SAS&lt;sup>®&lt;/sup> Users Guide to Regular Expressions When the Data Resi...
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
WEB TECHNOLOGIES XML
WEB TECHNOLOGIES XMLWEB TECHNOLOGIES XML
WEB TECHNOLOGIES XML
 
Sql scripting sorcerypaper
Sql scripting sorcerypaperSql scripting sorcerypaper
Sql scripting sorcerypaper
 

Viewers also liked

Viewers also liked (7)

X Path
X PathX Path
X Path
 
XMLT
XMLTXMLT
XMLT
 
XML Namespaces
XML NamespacesXML Namespaces
XML Namespaces
 
Xml
XmlXml
Xml
 
XSD
XSDXSD
XSD
 
Share point 2010-uiimprovements
Share point 2010-uiimprovementsShare point 2010-uiimprovements
Share point 2010-uiimprovements
 
4 xml namespaces and xml schema
4   xml namespaces and xml schema4   xml namespaces and xml schema
4 xml namespaces and xml schema
 

Similar to XML Bible (20)

XML/XSLT
XML/XSLTXML/XSLT
XML/XSLT
 
CTDA Workshop on XML and MODS
CTDA Workshop on XML and MODSCTDA Workshop on XML and MODS
CTDA Workshop on XML and MODS
 
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
 
XML
XMLXML
XML
 
Module 5 XML Notes.pdf
Module 5 XML Notes.pdfModule 5 XML Notes.pdf
Module 5 XML Notes.pdf
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Xml
Xml Xml
Xml
 
Xml
XmlXml
Xml
 
Introduction to XSLT
Introduction to XSLTIntroduction to XSLT
Introduction to XSLT
 
Xml tutorial
Xml tutorialXml tutorial
Xml tutorial
 
Basics of XML
Basics of XMLBasics of XML
Basics of XML
 
XML Transformations With PHP
XML Transformations With PHPXML Transformations With PHP
XML Transformations With PHP
 
eXtensible Markup Language (By Dr.Hatem Mohamed)
eXtensible Markup Language (By Dr.Hatem Mohamed)eXtensible Markup Language (By Dr.Hatem Mohamed)
eXtensible Markup Language (By Dr.Hatem Mohamed)
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
eXtensible Markup Language
eXtensible Markup LanguageeXtensible Markup Language
eXtensible Markup Language
 
XML Tools for Perl
XML Tools for PerlXML Tools for Perl
XML Tools for Perl
 
light_xml
light_xmllight_xml
light_xml
 
xml.pptx
xml.pptxxml.pptx
xml.pptx
 

More from LiquidHub

Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0LiquidHub
 
Sharepoint 2013 upgrade process
Sharepoint 2013 upgrade processSharepoint 2013 upgrade process
Sharepoint 2013 upgrade processLiquidHub
 
Share point 2013
Share point 2013Share point 2013
Share point 2013LiquidHub
 
Microsoft office-sharepoint-server-2007-presentation-120211522467022-2
Microsoft office-sharepoint-server-2007-presentation-120211522467022-2Microsoft office-sharepoint-server-2007-presentation-120211522467022-2
Microsoft office-sharepoint-server-2007-presentation-120211522467022-2LiquidHub
 
Managing metadata in_share_point_2010
Managing metadata in_share_point_2010Managing metadata in_share_point_2010
Managing metadata in_share_point_2010LiquidHub
 
Fast search for share point
Fast search for share pointFast search for share point
Fast search for share pointLiquidHub
 
Simple Farm Server Deployment
Simple Farm Server DeploymentSimple Farm Server Deployment
Simple Farm Server DeploymentLiquidHub
 
Pre Install Databases
Pre Install DatabasesPre Install Databases
Pre Install DatabasesLiquidHub
 
Moss 2007 Deployment Detail
Moss 2007 Deployment DetailMoss 2007 Deployment Detail
Moss 2007 Deployment DetailLiquidHub
 
Moss 2007 Backup Strategies
Moss 2007 Backup StrategiesMoss 2007 Backup Strategies
Moss 2007 Backup StrategiesLiquidHub
 
How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003
How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003
How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003LiquidHub
 
5060 A 01 Demonstration Steps
5060 A 01 Demonstration Steps5060 A 01 Demonstration Steps
5060 A 01 Demonstration StepsLiquidHub
 
Working With Infopath 2007
Working With Infopath 2007Working With Infopath 2007
Working With Infopath 2007LiquidHub
 
Whats New In Microsoft Windows Share Point Services Feature Walkthrough
Whats New In Microsoft Windows Share Point Services Feature WalkthroughWhats New In Microsoft Windows Share Point Services Feature Walkthrough
Whats New In Microsoft Windows Share Point Services Feature WalkthroughLiquidHub
 
Overviewofthe2007 Microsoft Office System Components Refresh
Overviewofthe2007 Microsoft Office System Components RefreshOverviewofthe2007 Microsoft Office System Components Refresh
Overviewofthe2007 Microsoft Office System Components RefreshLiquidHub
 
Organizingand Finding Resourceswith Office Share Point Server2007 Refresh
Organizingand Finding Resourceswith Office Share Point Server2007 RefreshOrganizingand Finding Resourceswith Office Share Point Server2007 Refresh
Organizingand Finding Resourceswith Office Share Point Server2007 RefreshLiquidHub
 
Organizingand Finding Resourceswith Office Share Point Server2007
Organizingand Finding Resourceswith Office Share Point Server2007Organizingand Finding Resourceswith Office Share Point Server2007
Organizingand Finding Resourceswith Office Share Point Server2007LiquidHub
 

More from LiquidHub (20)

Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0
 
Sharepoint 2013 upgrade process
Sharepoint 2013 upgrade processSharepoint 2013 upgrade process
Sharepoint 2013 upgrade process
 
Share point 2013
Share point 2013Share point 2013
Share point 2013
 
Microsoft office-sharepoint-server-2007-presentation-120211522467022-2
Microsoft office-sharepoint-server-2007-presentation-120211522467022-2Microsoft office-sharepoint-server-2007-presentation-120211522467022-2
Microsoft office-sharepoint-server-2007-presentation-120211522467022-2
 
Managing metadata in_share_point_2010
Managing metadata in_share_point_2010Managing metadata in_share_point_2010
Managing metadata in_share_point_2010
 
Fast search for share point
Fast search for share pointFast search for share point
Fast search for share point
 
Simple Farm Server Deployment
Simple Farm Server DeploymentSimple Farm Server Deployment
Simple Farm Server Deployment
 
Pre Install Databases
Pre Install DatabasesPre Install Databases
Pre Install Databases
 
Moss 2007 Deployment Detail
Moss 2007 Deployment DetailMoss 2007 Deployment Detail
Moss 2007 Deployment Detail
 
Moss 2007 Backup Strategies
Moss 2007 Backup StrategiesMoss 2007 Backup Strategies
Moss 2007 Backup Strategies
 
How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003
How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003
How To Configure Email Enabled Lists In Moss2007 Rtm Using Exchange 2003
 
Bdc Screens
Bdc ScreensBdc Screens
Bdc Screens
 
Bdc Screens
Bdc ScreensBdc Screens
Bdc Screens
 
5060 A 01 Demonstration Steps
5060 A 01 Demonstration Steps5060 A 01 Demonstration Steps
5060 A 01 Demonstration Steps
 
5060 A 01
5060 A 015060 A 01
5060 A 01
 
Working With Infopath 2007
Working With Infopath 2007Working With Infopath 2007
Working With Infopath 2007
 
Whats New In Microsoft Windows Share Point Services Feature Walkthrough
Whats New In Microsoft Windows Share Point Services Feature WalkthroughWhats New In Microsoft Windows Share Point Services Feature Walkthrough
Whats New In Microsoft Windows Share Point Services Feature Walkthrough
 
Overviewofthe2007 Microsoft Office System Components Refresh
Overviewofthe2007 Microsoft Office System Components RefreshOverviewofthe2007 Microsoft Office System Components Refresh
Overviewofthe2007 Microsoft Office System Components Refresh
 
Organizingand Finding Resourceswith Office Share Point Server2007 Refresh
Organizingand Finding Resourceswith Office Share Point Server2007 RefreshOrganizingand Finding Resourceswith Office Share Point Server2007 Refresh
Organizingand Finding Resourceswith Office Share Point Server2007 Refresh
 
Organizingand Finding Resourceswith Office Share Point Server2007
Organizingand Finding Resourceswith Office Share Point Server2007Organizingand Finding Resourceswith Office Share Point Server2007
Organizingand Finding Resourceswith Office Share Point Server2007
 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

XML Bible

  • 1. 18 CHAPTER Namespaces ✦ ✦ ✦ ✦ N o XML is an island. While you might find it useful to In This Chapter write documents that use a single markup vocabulary, (witness the baseball examples of Chapters 4 and 5) it’s even What is a more useful to mix and match tags from different XML appli- namespace? cations. For example, you may want to include a BIOGRAPHY element in each PLAYER element. Since the biography con- Namespace syntax sists basically of free-form, formatted text, it’s convenient to write it in well-formed HTML without reinventing all the tags Namespaces in DTDs for paragraphs, line breaks, list items, bold elements, and so forth from scratch. ✦ ✦ ✦ ✦ The problem, however, is that when mixing and matching tags from different XML applications, you’re likely to find the same tag used for two different things. Is a TITLE the title of a page or the title of a book? Is an ADDRESS the mailing address of a company or the email address of a Webmaster? Namespaces disambiguate these instances by associating a URI with each tag set, and attaching a prefix to each elementto indicate which tag set it belongs to. Thus, you can have both BOOK:TITLE and HTML:TITLE elements or POSTAL:ADDRESS and HTML:ADDRESS elements instead of just one kind of TITLE. or ADDRESS. This chapter shows you how to use namespaces. What Is a Namespace? XML enables developers to create their own markup languages for their own projects. These languages can be shared with individuals working on similar projects all over the world. One specific example of this is XSL. XSL is itself an XML application for styling XML documents. The XSL transformation language must output arbitrary, well-formed XML, possibly including XSL itself. Thus, you need a clear-cut means of distinguishing between those XML elements that are XSL transformation instructions and output XML elements, even if they have the same names!
  • 2. 618 Part IV ✦ Supplemental Technologies Namespaces are the solution. They allow each element and attribute in a document to be placed in a different namespace. The XML elements that comprise XSL trans- formation instructions are placed in the http://www.w3.org/XSL/Transform/1.0 namespace. The XML elements that are part of the output can reside in some other convenient namespace like http://www.w3.org/TR/REC-html40 or http://www. w3.org/XSL/Format/1.0. The exact namespace isn’t even important as long as it’s different. If you’re familiar with the concept of namespaces as used in C++ and other pro- Caution gramming languages, you need to put aside your preconceptions before reading further. XML namespaces are similar to, but not quite the same as the namespaces used in programming. In particular, XML namespaces do not necessarily form a set (a collection with no duplicates). Listing 15-2, a transformation from a source vocabulary to XSL formatting objects, initially appeared in Chapter 15, XSL Formatting Objects. It displays an XSL style sheet that converts from input XML to XSL formatting objects. The formatting engine distinguishes between elements that are XSL instructions and literal data for the output by using namespaces. Any element in the http://www.w3.org/ XSL/Transform/1.0 namespace represents a transformation instruction. Any element in the http://www.w3.org/XSL/Format/1.0 namespace comprises part of the output. <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0” xmlns:fo=”http://www.w3.org/XSL/Format/1.0” result-ns=”fo” indent-result=”yes”> <xsl:template match=”/”> <fo:root xmlns:fo=”http://www.w3.org/XSL/Format/1.0”> <fo:layout-master-set> <fo:simple-page-master page-master-name=”only”> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence> <fo:sequence-specification> <fo:sequence-specifier-single page-master-name=”only”/> </fo:sequence-specification> <fo:flow> <xsl:apply-templates select=”//ATOM”/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template>
  • 3. 619 Chapter 18 ✦ Namespaces <xsl:template match=”ATOM”> <fo:block font-size=”20pt” font-family=”serif”> <xsl:value-of select=”NAME”/> </fo:block> </xsl:template> </xsl:stylesheet> More specifically, these elements exist in the http://www.w3.org/XSL/Transform/ 1.0 namespace and are XSL instructions: ✦ stylesheet ✦ template ✦ apply-templates ✦ value-of These elements, in the http://www.w3.org/XSL/Format/1.0 namespace, are XSL formatting objects and part of the output: ✦ root ✦ layout-master-set ✦ simple-page-master ✦ region-body ✦ sequence-specification ✦ sequence-specifier-single ✦ page-sequence ✦ block The four elements with the xsl prefix have the qualified names beginning with the prefix: ✦ xsl:stylesheet ✦ xsl:template ✦ xsl:apply-templates ✦ xsl:value-of However, their full names use the URL rather than the prefix: ✦ http://www.w3.org/XSL/Transform/1.0:stylesheet ✦ http://www.w3.org/XSL/Transform/1.0:template ✦ http://www.w3.org/XSL/Transform/1.0:apply-templates ✦ http://www.w3.org/XSL/Transform/1.0:value-of
  • 4. 620 Part IV ✦ Supplemental Technologies In essence, the shorter qualified names are nicknames that are used only within the document because URLs often contain characters like ~, %, and / that aren’t legal in XML names. However, qualified names do make documents a little easier to type and read. Namespaces in an XML is an official W3C recommendation. The W3C considers it Caution complete, aside from possible minor errors and elucidations. Nonetheless, of all the XML specifications from the W3C, this one is the most controversial. Many people feel very strongly that this standard contains fundamental flaws. The main objection argues that namespaces are, in practice, incompatible with DTDs and validation. While I don’t have a strong opinion on this one way or the other, I do question the wisdom of publishing a standard when nothing approaching a con- sensus has been reached. Namespaces are a crucial part of many XML related specifications such as XSL and XHTML, so you need to understand them. Nonetheless, a lot of developers and authors have chosen to ignore this specifica- tion for their own work. Namespace Syntax Namespaces have been crafted to layer on top of the XML 1.0 specification. An XML 1.0 processor that knows nothing about namespaces can still read a document that uses namespaces, and will not find any errors. Documents that use namespaces do not break existing XML parsers (at least ones that don’t check for validity); and users don’t have to wait for notoriously unpunctual software companies to release expensive upgrades before using namespaces. Definition of Namespaces Namespaces are defined using an xmlns:prefix attribute on the applicable elements they apply to. prefix is replaced by the actual prefix used for the namespace. The value of the attribute is the URI of the namespace. For example, this xsl:stylesheet tag associates the prefix xsl with the URI http://www. w3.org/XSL/Transform/1.0. <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”> The xsl prefix can then be attached to the local element and attribute names within the xsl:stylesheet element to identify them as belonging to the http://www.w3. org/XSL/Transform/1.0 namespace. The prefix is separated from the local name by a colon. Listing 14-2, a basic XSL style sheet for the periodic table, which was first shown in Chapter 14, XSL Transformations, demonstrates by using the xsl prefix on the stylesheet, template, and apply-templates elements. <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”>
  • 5. 621 Chapter 18 ✦ Namespaces <xsl:template match=”PERIODIC_TABLE”> <html> <xsl:apply-templates/> </html> </xsl:template> <xsl:template match=”ATOM”> <P> <xsl:apply-templates/> </P> </xsl:template> </xsl:stylesheet> The URI that defines a namespace is purely formal. Its only purpose is to group and disambiguate element and attribute names in the document. It does not necessarily point to anything. In particular, there is no guarantee that the document at the URI describes the syntax used in the document; or, for that matter, that any document exists at the URI. Having said that, if there is a canonical URI for a particular XML application, then that URI is a good choice for the namespace definition. A namespace prefix can be any legal XML name that does not contain a colon. Recall from Chapter 6, Well-Formed XML Documents, that a legal XML name must begin with a letter or an underscore (_). Subsequent letters in the name may include letters, digits, underscores, hyphens, and periods. They may not include whitespace. There are two prefixes that are specifically disallowed, xml and xmlns. The xml Note prefix is defined to refer to http://www.w3.org/XML/1998/namespace. The xmlns prefix is used to bind elements to namespaces, and is therefore not avail- able as a prefix to be bound to. Other than disallowing the colon character in XML names (aside from its use in separating prefixes and local names) namespaces have no direct effect on stan- dard XML syntax. A document that uses namespaces must still be well-formed when read by a processor that knows nothing about namespaces. If the document is to be validated, then it must be validated without specifically considering the namespaces. To an XML processor, a document that uses namespaces is just a funny-looking document in which some of the element and attribute names may have a single colon. Namespaces do present problems for validation. If a DTD was written without Caution namespace prefixes, then it must be rewritten using the namespace prefixes before it can be used to validate documents that use the prefixes. For example, consider this element declaration: <!ELEMENT DIVISION (DIVISION_NAME, TEAM+)> You have to rewrite it like this if the elements are all given the bb namespace prefix: <!ELEMENT bb:DIVISION (bb:DIVISION_NAME, bb:TEAM+)>
  • 6. 622 Part IV ✦ Supplemental Technologies This means that you cannot use the same DTD for both documents with name- spaces and documents without, even if they use essentially the same vocabulary. In fact, you can’t even use the same DTD for documents that use the same tag sets and namespaces, but different prefixes, because DTDs are tied to the actual pre- fixes rather than the URIs of the namespaces. Multiple Namespaces Listing 14-2 did not actually place the HTML elements in a namespace, but that’s not hard to do. Listing 18-1 demonstrates. Just as xsl is the conventional prefix for XSL transformation instructions, html is the conventional prefix for HTML elements. In this example, the xsl:stylesheet element declares two different namespaces, one for XSL and one for HTML. Listing 18-1: An XSL stylesheet that uses the http://www.w3. org/TR/REC-html40 namespace for output <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0” xmlns:html=”http://www.w3.org/TR/REC-html40”> <xsl:template match=”PERIODIC_TABLE”> <html:html> <xsl:apply-templates/> </html:html> </xsl:template> <xsl:template match=”ATOM”> <html:p> <xsl:apply-templates/> </html:p> </xsl:template> </xsl:stylesheet> While it’s customary, and generally useful to place the xmlns attribute on the root element, it can appear on other elements as well. In this case, the namespace prefix is understood only within the element where it’s declared. Consider Listing 18-2. The html prefix is legal only in the xsl:template element where it’s declared. It can’t be applied in other template rules, unless they separately declare the html namespace.
  • 7. 623 Chapter 18 ✦ Namespaces Listing 18-2: An XSL style sheet with the http://www.w3.org/ TR/REC-html40 namespace declared in the template rules <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”> <xsl:template match=”PERIODIC_TABLE” xmlns:html=”http://www.w3.org/TR/REC-html40”> <html:html> <xsl:apply-templates/> </html:html> </xsl:template> <xsl:template match=”ATOM”> <p> <xsl:apply-templates/> <p> </xsl:template> </xsl:stylesheet> You can redefine a namespace in a child element. For example, consider the XSL style sheet in Listing 18-3. Here, the xsl prefix appears in different elements to refer to http://www.w3.org/XSL/Transform/1.0 and http://www.w3.org/ XSL/Format/1.0 alternately. Although every element has the prefix xsl, the XSL transformation instructions and the XSL formatting objects still reside in different namespaces because the meaning of the xsl prefix changes from element to element. Listing 18-3: Redefining the xsl prefix <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”> <xsl:template match=”/”> <xsl:root xmlns:xsl=”http://www.w3.org/XSL/Format/1.0”> <xsl:layout-master-set> <xsl:simple-page-master page-master-name=”only”> <xsl:region-body/> </xsl:simple-page-master> Continued
  • 8. 624 Part IV ✦ Supplemental Technologies Listing 18-3 (continued) </xsl:layout-master-set> <xsl:page-sequence> <xsl:sequence-specification> <xsl:sequence-specifier-single page-master-name=”only”/> </xsl:sequence-specification> <xsl:flow> <xsl:apply-templates select=”//ATOM”/ xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”/> </xsl:flow> </xsl:page-sequence> </xsl:root> </xsl:template> <xsl:template match=”ATOM”> <xsl:block font-size=”20pt” font-family=”serif” xmlns:xsl=”http://www.w3.org/XSL/Format/1.0”> <xsl:value-of select=”NAME” xmlns:xsl=”http://www.w3.org/XSL/Transform/1.0”/> </xsl:block> </xsl:template> </xsl:stylesheet> This is, however, needlessly confusing and I strongly recommend that you avoid it. There are more than enough prefixes to go around, and almost no need to reuse them within the same document. The main importance of this is if two different documents from different authors that happen to reuse a similar prefix are being combined. This is a good reason to avoid short prefixes like a, m, and x that are likely to be reused for different purposes. Attributes Since attributes belong to particular elements, they’re more easily disambiguated from similarly named attributes without namespaces. Consequently, it’s not nearly as essential to add namespaces to attributes as to elements. For example, the April 21, 1999 working draft of the XSL specification requires that all XSL transformation ele- ments fall in the http://www.w3.org/XSL/Transform/1.0 namespace. However, it does not require that the attributes of these elements be in any particular name- space. (In fact, it requires that they not be in any namespace.) Nonetheless, you can attach namespace prefixes to attributes if necessary. For example, this PLAYER
  • 9. 625 Chapter 18 ✦ Namespaces element and all its attributes live in the http://metalab.unc.edu/xml/baseball namespace. <bb:PLAYER xmlns:bb=”http://metalab.unc.edu/xml/baseball” bb:GIVEN_NAME=”Tom” bb:SURNAME=”Glavine” bb:POSITION=”Starting Pitcher” bb:GAMES=”33” bb:GAMES_STARTED=”33” bb:WINS=”20” bb:LOSSES=”6” bb:SAVES=”0” bb:COMPLETE_GAMES=”4” bb:SHUT_OUTS=”3” bb:ERA=”2.47” bb:INNINGS=”229.1” bb:HOME_RUNS_AGAINST=”13” bb:RUNS_AGAINST=”67” bb:EARNED_RUNS=”63” bb:HIT_BATTER=”2” bb:WILD_PITCHES=”3” bb:BALK=”0” bb:WALKED_BATTER=”74” bb:STRUCK_OUT_BATTER=”157”/> This might occasionally prove useful if you need to combine attributes from two different XML applications on the same element. It is possible (though mostly pointless) to associate the same namespace URI with two different prefixes. There’s really no reason to do this. The only reason I bring it up here is simply to warn you that it is the full name of the attribute that must satisfy XML’s rules for an element not having more than one attribute with the same name. For example, this is illegal because bb:GIVEN_NAME and baseball:GIVEN_NAME are the same: <bb:PLAYER xmlns:bb=”http://metalab.unc.edu/xml” xmlns:baseball=”http://metalab.unc.edu/xml” bb:GIVEN_NAME=”Hank” bb:SURNAME=”Aaron” baseball:GIVEN_NAME=”Henry” /> On the other hand, the URI does not actually get checked to see what it points to. The URIs http://metalab.unc.edu/xml/ and http://www.metalab.unc.edu/ xml/ point to the same page. However, this is legal: <bb:PLAYER xmlns:bb=”http://metalab.unc.edu/xml” xmlns:baseball=”http://www.metalab.unc.edu/xml” bb:GIVEN_NAME=”Hank” bb:SURNAME=”Aaron” baseball:GIVEN_NAME=”Henry” /> Default Namespaces In long documents with a lot of markup, all in the same namespace, you might find it inconvenient to add a prefix to each element name. You can attach a default namespace to an element and its child elements using an xmlns attribute with no prefix. The element itself, as well as all its children, are considered to be in the defined namespace unless they possess an explicit prefix. For example, Listing 18-4 shows an XSL style sheet that does not prefix XSL transformation elements with xsl as is customary. Attributes are never in a default namespace. They must be explicitly prefixed. Note
  • 10. 626 Part IV ✦ Supplemental Technologies Listing 18-4: An XSL stylesheet that uses default namespaces <?xml version=”1.0”?> <stylesheet xmlns=”http://www.w3.org/XSL/Transform/1.0” xmlns:fo=”http://www.w3.org/XSL/Format/1.0” result-ns=”fo”> <template match=”/”> <fo:root xmlns:fo=”http://www.w3.org/XSL/Format/1.0”> <fo:layout-master-set> <fo:simple-page-master page-master-name=”only”> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence> <fo:sequence-specification> <fo:sequence-specifier-single page-master-name=”only”/> </fo:sequence-specification> <fo:flow> <apply-templates select=”//ATOM”/> </fo:flow> </fo:page-sequence> </fo:root> </template> <template match=”ATOM”> <fo:block font-size=”20pt” font-family=”serif”> <value-of select=”NAME”/> </fo:block> </template> </stylesheet> Perhaps the best use of default namespaces attaches a namespace to every element in an existing document to which you’re now going to add tags from a different language. For instance, if you place some MathML in an HTML document, you only have to add prefixes to the MathML elements. You could put all the HTML elements in the http://www.w3.org/TR/REC-html40 namespace simply by replacing the <html> start tag with this tag: <html xmlns=”http://www.w3.org/TR/REC-html40”>
  • 11. 627 Chapter 18 ✦ Namespaces You do not need to edit the rest of the file! The MathML tags you insert still need to be in a separate namespace. However, as long as they aren’t mixed up with a lot of HTML markup, you can simply declare an xmlns attribute on the root element of the MathML. This defines a default namespace for the MathML elements that override the default namespace of the document containing the MathML. Listing 18-5 demonstrates. Listing 18-5: A MathML math element embedded in a well- formed HTML document that uses namespaces <?xml version=”1.0”?> <html xmlns=”http://www.w3.org/TR/REC-html40”> <head> <title>Fiat Lux</title> <meta name=”GENERATOR” content=”amaya V1.3b” /> </head> <body> <P>And God said,</P> <math xmlns=”http://www.w3.org/TR/REC-MathML/”> <mrow> <msub> <mi>&#x3B4;</mi> <mi>&#x3B1;</mi> </msub> <msup> <mi>F</mi> <mi>&#x3B1;&#x3B2;</mi> </msup> <mi></mi> <mo>=</mo> <mi></mi> <mfrac> <mrow> <mn>4</mn> <mi>&#x3C0;</mi> </mrow> <mi>c</mi> </mfrac> <mi></mi> <msup> <mi>J</mi> <mrow> <mi>&#x3B2;</mi> <mo></mo> </mrow> </msup> Continued
  • 12. 628 Part IV ✦ Supplemental Technologies Listing 18-5 (continued) </mrow> </math> <P>and there was light</P> </body> </html> Here, math, mrow, msub, mo, mi, mfrac, mn, and msup are all in the http://www.w3. org/TR/REC-MathML/ namespace, even though the document that contains them uses the http://www.w3.org/TR/REC-html40 namespace. Namespaces in DTDs Namespaces do not get any special exemptions from the normal rules of well- formedness and validity. For a document that uses namespaces to be valid, the xmlns attributes must be declared in the DTD for those elements to which they’re attached. Furthermore, you must declare the elements and attributes using the prefixes they use in the document. For instance, if a document uses a math:subset element, then the DTD must declare a math:subset element, not merely a subset element. (Of course, these rules do not apply to the merely well-formed documents discussed thus far.) For example: <!ELEMENT math:subset EMPTY> Default attribute values and #IMPLIED attributes can help here. For example, this ATTLIST declaration places every math:subset element in the http://www.w3. org/TR/REC-MathML/ namespace unless specified otherwise in the document. <!ATTLIST math:subset xmlns:math “http://www.w3.org/TR/REC-MathML/” #IMPLIED> When working with valid documents, default namespaces prove especially useful since they don’t require you to add prefixes to all the elements. Adding prefixes to elements from an XML application whose DTD doesn’t use prefixes will break validity. There are, however, clear limits to how far default namespaces will take you. In particular, they are not sufficient to differentiate between two elements that use an element name in incompatible ways. For example, if one DTD defines a HEAD as containing a TITLE and a META element, and another DTD defines a HEAD as con- taining #PCDATA, then you will have to use prefixes in the DTD and the document to distinguish the two different HEAD elements.
  • 13. 629 Chapter 18 ✦ Namespaces Two different development efforts are underway that may (or may not) eventually solve the problem of merging incompatible DTDs from different domains. XML schemas may provide a more robust replacement for DTDs. XML fragments may enable different documents to be combined with more distinction between which parts come from where. However, neither of these is even close to finished. Conse- quently, for now, merging incompatible DTDs will probably require you to rewrite the DTD and your documents to use prefixes. If you have a question about whether a document that uses namespaces is well- Tip formed or valid, forget everything you know about namespaces. Simply treat the document as a normal XML document that happens to have some element and attribute names that contain colons. The document is as well-formed and valid as it is when you don’t consider namespaces. Summary This chapter explained how to work with namespaces. In particular, you learned: ✦ Namespaces distinguish between elements and attributes of the same name from different XML applications. ✦ Namespaces are declared by an xmlns attribute whose value is the URI of the namespace. The document referred to by this URI need not exist. ✦ The prefix associated with a namespace is the part of the name of the xmlns attribute that follows the colon; for example xmlns:prefix. ✦ Prefixes are attached to all element and attribute names that belong to the namespace identified by the prefix. ✦ If an xmlns attribute has no prefix, it establishes a default namespace for that element and its child elements (but not for any attributes). ✦ DTDs must be written in such a fashion that a processor that knows nothing about namespaces can still parse and validate the document. The next chapter explores the Resource Description Framework, RDF, an XML application for encoding meta-data and information structures. ✦ ✦ ✦