新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 关于 XML 的一般性技术讨论,提供 XML入门资料 和 XML教程
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 XML基础 』 → [转载]XML Namespaces FAQ 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4919 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [转载]XML Namespaces FAQ 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 XML基础 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 [转载]XML Namespaces FAQ

    http://www.rpbourret.com/xml/NamespacesFAQ.htm

    XML Namespaces FAQ
    Maintained by Ronald Bourret

    Copyright 2000-2004 by Ronald Bourret
    (Last updated January, 2004)

    Table of Contents
    PART I: OVERVIEW

          SECTION 1: EXECUTIVE SUMMARY
                1.1) Can you give me an executive summary of what XML namespaces are?
                1.2) Can you give me an executive summary of what XML namespaces are not?

          SECTION 2: TRADITIONAL NAMESPACES
                2.1) What is a traditional namespace?
                2.2) What is the relationship between different traditional namespaces?
                2.3) What are traditional namespaces used for?

          SECTION 3: XML NAMESPACES
                3.1) What is the purpose of XML namespaces?
                3.2) What is an XML namespace?
                3.3) Does XML namespaces recommendation define anything except a two-part naming system for element types and attributes?
                3.4) What do XML namespaces actually contain?
                3.5) Are the names of all element types and attributes in some XML namespace?
                3.6) Do XML namespaces apply to entity names, notation names, or processing instruction targets?
                3.7) Who can create an XML namespace?
                3.8) Do I need to use XML namespaces?
                3.9) What is the relationship between XML namespaces and the XML 1.0 recommendation?
                3.10) What is the difference between versions 1.0 and 1.1 of the XML namspaces recommendation?

    PART II: DECLARING AND USING XML NAMESPACES

          SECTION 4: DECLARING XML NAMESPACES IN AN XML DOCUMENT
                4.1) How do I declare an XML namespace in an XML document?
                4.2) Where can I declare an XML namespace?
                4.3) Can I use an attribute default in a DTD to declare an XML namespace?
                4.4) Do the default values of xmlns attributes declared in the DTD apply to the DTD?
                4.5) How do I override an XML namespace declaration that uses a prefix?
                4.6) How do I override a default XML namespace declaration?
                4.7) How do I undeclare an XML namespace prefix?
                4.8) How do I undeclare the default XML namespace?
                4.9) Why are special attributes used to declare XML namespaces?
                4.10) How do different XML technologies treat XML namespace declarations?

          SECTION 5: USING XML NAMESPACES IN AN XML DOCUMENT
                5.1) How do I use prefixes to refer to element type and attribute names in an XML namespace?
                5.2) How do I use the default XML namespace to refer to element type names in an XML namespace?
                5.3) How do I use the default XML namespace to refer to attribute names in an XML namespace?
                5.4) When should I use the default XML namespace instead of prefixes?

          SECTION 6: SCOPE OF XML NAMESPACE DECLARATIONS
                6.1) What is the scope of an XML namespace declaration?
                6.2) Does the scope of an XML namespace declaration include the element it is declared on?
                6.3) If an element or attribute is in the scope of an XML namespace declaration, is its name in that namespace?
                6.4) What happens when an XML namespace declaration goes out of scope?
                6.5) What happens if no XML namespace declaration is in scope?
                6.6) Can multiple XML namespace declarations be in scope at the same time?
                6.7) How can I declare XML namespaces so that all elements and attributes are in their scope?
                6.8) Does the scope of an XML namespace declaration ever include the DTD?

          SECTION 7: XML NAMESPACES AND DTDs
                7.1) Can I use XML namespaces in DTDs?
                7.2) Do XML namespace declarations apply to DTDs?
                7.3) Can I use qualified names in DTDs?
                7.4) Can the content model in an element type declaration contain element types whose names come from other XML namespaces?
                7.5) Can the attribute list of an element type contain attributes whose names come from other XML namespaces?
                7.6) How can I construct an XML document that is valid and conforms to the XML namespaces recommendation?
                7.7) How can I validate an XML document that uses XML namespaces?
                7.8) If I start using XML namespaces, do I need to change my existing DTDs?

          SECTION 8: USING DOCUMENTS THAT USE XML NAMESPACES
                8.1) How do I create documents that use XML namespaces?
                8.2) How can I check that a document conforms to the XML namespaces recommendation?
                8.3) Can I use the same document with both namespace-aware and namespace-unaware applications?
                8.4) What software is needed to process XML namespaces?
                8.5) How can I use XML namespaces to combine documents that use different element type and attribute names?
                8.6) How do I use XML namespaces with Internet Explorer 5.0 and/or the MSXML parser?

          SECTION 9: PROCESSING XML NAMESPACES IN XML APPLICATIONS
                9.1) How do applications process documents that use XML namespaces?
                9.2) How do I use XML namespaces with SAX 1.0?
                9.3) How do I use XML namespaces with SAX 2.0?
                9.4) How do I use XML namespaces with DOM level 1?
                9.5) How do I use XML namespaces with DOM level 2?
                9.6) Can an application process documents that use XML namespaces and documents that don't use XML namespaces?
                9.7) Can an application be both namespace-aware and namespace-unaware?
                9.8) What does a namespace-aware application do when it encounters an error?

    PART III: NAMES, PREFIXES, AND URIs

          SECTION 10: NAMES
                10.1) What is a qualified name?
                10.2) What characters are allowed in a qualified name?
                10.3) Where can qualified names appear?
                10.4) Can qualified names be used in attribute values?
                10.5) How are qualified names mapped to names in XML namespaces?
                10.6) What is a prefixed name?
                10.7) What is an unprefixed name?
                10.8) Are unprefixed names in an XML namespace?
                10.9) What is a local name?
                10.10) What is a namespace name?
                10.11) What is a universal name?
                10.12) How are universal names represented?
                10.13) Are universal names universally unique?

          SECTION 11: XML NAMESPACE PREFIXES
                11.1) What is an XML namespace prefix?
                11.2) What characters are allowed in an XML namespace prefix?
                11.3) Are prefixes significant?
                11.4) Can I use the same prefix for more than one XML namespace?
                11.5) Can I use more than one prefix for the same XML namespace?
                11.6) How are prefixes declared?
                11.7) Can I undeclare a prefix -- that is, dissociate a prefix from an XML namespace?
                11.8) What happens if I use a prefix that is not declared?
                11.9) What happens if there is no prefix on an element type name?
                11.10) What happens if there is no prefix on an attribute name?

          SECTION 12: XML NAMESPACE NAMES (URIs)
                12.1) What is an XML namespace URI?
                12.2) What is an XML namespace name?
                12.3) What does the URI used as an XML namespace name point to?
                12.4) Can I resolve the URI used as an XML namespace name?
                12.5) Can I use a relative URI as a namespace name?

    PART IV: MISCELLANEOUS

          SECTION 13: POLITICS AND REVOLUTIONS
                13.1) Why are XML namespaces so hard to understand and use?
                13.2) Are there any alternatives to XML namespaces?
                13.3) How controversial are XML namespaces?

          SECTION 14: XML NAMESPACE RESOURCES
                14.1) What resources are available for learning about XML namespaces?

          SECTION 15: COMMENTS, COMPLAINTS, AND SUGGESTIONS


    PART I: OVERVIEW
    1) SECTION 1: EXECUTIVE SUMMARY
    1.1) Can you give me an executive summary of what XML namespaces are?
    Sure. Here it is in five easy bullet points.

    The XML namespaces recommendation defines a way to distinguish between duplicate element type and attribute names. Such duplication might occur, for example, in an XSLT stylesheet or in a document that contains element types and attributes from two different DTDs.

    An XML namespace is a collection of element type and attribute names. The namespace is identified by a unique name, which is a URI. Thus, any element type or attribute name in an XML namespace can be uniquely identified by a two-part name: the name of its XML namespace and its local name. This two-part naming system is the only thing defined by the XML namespaces recommendation.

    XML namespaces are declared with an xmlns attribute, which can associate a prefix with the namespace. The declaration is in scope for the element containing the attribute and all its descendants. For example:

      <!-- Declares two XML namespaces. Their scope is the A and B elements. -->
      <A xmlns:foo="http://www.foo.org/" xmlns="http://www.bar.org/">
         <B>abcd</B>
      </A>
    If an XML namespace declaration contains a prefix, you refer to element type and attribute names in that namespace with the prefix. For example:

      <!-- A and B are in the http://www.foo.org/ namespace, which is associated with the foo prefix. -->
      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B>abcd</foo:B>
      </foo:A>
    If an XML namespace declaration does not contain a prefix, the namespace is the default XML namespace and you refer to element type names in that namespace without a prefix. For example:

      <!-- This is equivalent to the previous example but uses a default namespace instead of the foo prefix. -->
      <A xmlns="http://www.foo.org/">
         <B>abcd<B>
      </A>
    1.2) Can you give me an executive summary of what XML namespaces are not?
    They aren't a cure of cancer, they aren't a way to win the lottery, and they aren't a direct cause of world peace. They also aren't very difficult to understand or use. Two things that XML namespaces are not have caused a lot of confusion, so we'll mention them here:

    XML namespaces are not a technology for joining XML documents that use different DTDs. Although they might be used in such a technology, they don't provide it themselves.

    The URIs used as XML namespace names are not guaranteed to point to schemas, information about the namespace, or anything else -- they're just identifiers. URIs were used simply because they're a well-known system for creating unique identifiers. Don't even think about trying to resolve these URIs. (For details, see question 12.4.)

    2) SECTION 2: TRADITIONAL NAMESPACES
    2.1) What is a traditional namespace?
    Before discussing XML namespaces, it is useful to discuss namespaces in general. In this FAQ, we will refer to such namespaces as traditional namespaces. We will refer to XML namespaces as XML namespaces. The word namespace can refer to either a traditional namespace or an XML namespace, depending on context, but will generally refer to an XML namespace.

    A traditional namespace is a set of zero or more names, each of which must be unique within the namespace and constructed according to the rules (if any) of the namespace. For example, the names of element types in an XML document inhabit a traditional namespace, as do the names of tables in a relational database and the names of class variables in a Java class. Traditional namespaces also occur outside the field of computer science -- for example, the names of people could be thought to inhabit a traditional namespace, as could the names of species.

    2.2) What is the relationship between different traditional namespaces?
    They are disjoint -- that is, they are not related. Because of this, a name in one traditional namespace does not collide with the same name in a different traditional namespace. This property is useful to applications that have multiple sets of names. By assigning each set of names to a different traditional namespace, they can allow the same name to occur in each set of names without fear of collision.

    For example, in the following XML document, there is no conflict between the three different uses of the name Value.

      <AuctionItem>
         <Title Value="486Laptop"/>
         <Category Value="Computers"/>
         <Value>$100</Value>
      </AuctionItem>
    This is because an XML document has one traditional namespace for element type names and, for each element type, one traditional namespace for the names of the attributes that apply to that element type. Thus, the two Value attribute names don't conflict because each is assigned to a different traditional namespace -- the first to the attribute namespace for the Title element type and the second to the attribute namespace for the Category element type. Furthermore, neither of the Value attribute names conflicts with the Value element type name because element type names are kept in a traditional namespace that is separate from the attribute namespaces.

    Other examples of applications that use multiple traditional namespaces include:

    Java classes. In a Java class, there is one traditional namespace for the names of class variables, one traditional namespace for the names of methods, and, for each method, one traditional namespace for the names of variables local to that method.

    Relational databases. In a relational database, there is one traditional namespace for the names of tables and, for each table, one traditional namespace for the names of columns in that table.

    2.3) What are traditional namespaces used for?
    Perhaps the most common (computer science) use of traditional namespaces is to provide a container for a set of identifiers. For example, traditional namespaces are used to contain the names (identifiers) of element types in an XML document, the names of class variables in a Java class, and the names of tables in a relational database. Traditional namespaces are useful in this regard because of their requirement that each name in the namespace be unique. Thus, when a new name (identifier) is added to the namespace, the uniqueness of the identifier can be verified by checking that the name does not already exist in the namespace.

    (Note that just because a set of objects draws its names from some traditional namespace does not mean that those names uniquely identify the objects. For example, two different people can share the same name, as can two different element nodes in a DOM tree, which use element type names as their names. For the names in a traditional namespace to uniquely identify the objects in a set, the objects in the set must draw their names only from that namespace and no name can be applied to more than one object. In practice, the names from a single traditional namespace are only used to identify the objects in a single set; otherwise, additional information must be stored stating which names apply to which set.)

    3) SECTION 3: XML NAMESPACES
    3.1) What is the purpose of XML namespaces?
    XML namespaces are designed to provide universally unique names for elements and attributes. This allows people to do a number of things, such as:

    Combine fragments from different documents without any naming conflicts. (See example below.)

    Write reusable code modules that can be invoked for specific elements and attributes. Universally unique names guarantee that such modules are invoked only for the correct elements and attributes.

    Define elements and attributes that can be reused in other schemas or instance documents without fear of name collisions. For example, you might use XHTML elements in a parts catalog to provide part descriptions. Or you might use the nil attribute defined in XML Schemas to indicate a missing value.

    As an example of how XML namespaces are used to resolve naming conflicts in XML documents that contain element types and attributes from multiple XML languages, consider the following two XML documents:

      <?xml version="1.0" ?>
      <Address>
         <Street>Wilhelminenstr. 7</Street>
         <City>Darmstadt</City>
         <State>Hessen</State>
         <Country>Germany</Country>
         <PostalCode>D-64285</PostalCode>
      </Address>
    and:

      <?xml version="1.0" ?>
      <Server>
         <Name>OurWebServer</Name>
         <Address>123.45.67.8</Address>
      </Server>
    Each document uses a different XML language and each language defines an Address element type. Each of these Address element types is different -- that is, each has a different content model, a different meaning, and is interpreted by an application in a different way. This is not a problem as long as these element types exist only in separate documents. But what if they are combined in the same document, such as a list of departments, their addresses, and their Web servers? How does an application know which Address element type it is processing?

    One solution is to simply rename one of the Address element types -- for example, we could rename the second element type IPAddress. However, this is not a useful long term solution. One of the hopes of XML is that people will standardize XML languages for various subject areas and write modular code to process those languages. By reusing existing languages and code, people can quickly define new languages and write applications that process them. If we rename the second Address element type to IPAddress, we will break any code that expects the old name.

    A better answer is to assign each language (including its Address element type) to a different namespace. This allows us to continue using the Address name in each language, but to distinguish between the two different element types. The mechanism by which we do this is XML namespaces.

    (Note that by assigning each Address name to an XML namespace, we actually change the name to a two-part name consisting of the name of the XML namespace plus the name Address. This means that any code that recognizes just the name Address will need to be changed to recognize the new two-part name. However, this only needs to be done once, as the two-part name is universally unique. For more information about the uniqueness of universal name, see question 10.13; for more information about processing universal names, see question 9.1.)

    3.2) What is an XML namespace?
    An XML namespace is a collection of element type and attribute names. The collection itself is unimportant -- in fact, a reasonable argument can be made that XML namespaces don't actually exist as physical or conceptual entities (see myth #1 in "Namespace Myths Exploded"). What is important is the name of the XML namespace, which is a URI. This allows XML namespaces to provide a two-part naming system for element types and attributes. The first part of the name is the URI used to identify the XML namespace -- the namespace name. The second part is the element type or attribute name itself -- the local part, also known as the local name. Together, they form the universal name.

    This two-part naming system is the only thing defined by the XML namespaces recommendation.

    By using multiple XML namespaces, multiple element types with the same local name can inhabit the same XML document. For example, the following document uses XML namespaces to distinguish between two different element types named Address.

      <Department>
         <Name>DVS1</Name>
         <addr:Address xmlns:addr="http://www.tu-darmstadt.de/ito/addresses">
            <addr:Street>Wilhelminenstr. 7</addr:Street>
            <addr:City>Darmstadt</addr:City>
            <addr:State>Hessen</addr:State>
            <addr:Country>Germany</addr:Country>
            <addr:PostalCode>D-64285</addr:PostalCode>
         </addr:Address>
         <serv:Server xmlns:serv="http://www.tu-darmstadt.de/ito/servers">
            <serv:Name>OurWebServer</serv:Name>
            <serv:Address>123.45.67.8</serv:Address>
         </serv:Server>
      </Department>
    The first Address element type name belongs to the http://www.tu-darmstadt.de/ito/addresses XML namespace. It has a universal (two-part) name of "http://www.tu-darmstadt.de/ito/addresses" plus "Address". (Following the convention proposed by James Clark in his paper XML Namespaces (see question 14.1), we write this as {http://www.tu-darmstadt.de/ito/addresses}Address.) The second Address element type name belongs to the http://www.tu-darmstadt.de/ito/servers XML namespace and has a universal name of {http://www.tu-darmstadt.de/ito/servers}Address. Thus, each universal name is unique, meeting the requirement that each element type in an XML document have a unique name.

    3.3) Does the XML namespaces recommendation define anything except a two-part naming system for element types and attributes?
    No.

    This is a very important point and a source of much confusion, so we will repeat it:

       THE XML NAMESPACES RECOMMENDATION DOES NOT
       DEFINE ANYTHING EXCEPT A TWO-PART NAMING SYSTEM
       FOR ELEMENT TYPES AND ATTRIBUTES.

    In particular, they do not provide or define any of the following:

    A way to merge two documents that use different DTDs. (See question 8.5.)

    A way to associate XML namespaces and schema information. (See question 12.2 and myth #8 in "Namespace Myths Exploded".)

    A way to validate documents that use XML namespaces. (See question 7.6.)

    A way to associate element type or attribute declarations in a DTD with an XML namespace. (See question 7.2.)

    3.4) What do XML namespaces actually contain?
    XML namespaces are collections of names, nothing more. That is, they contain the names of element types and attributes, not the elements or attributes themselves. For example, consider the following document.

      <foo:A xmlns:foo="http://www.foo.org/">
         <B foo:C="foo" D="bar"/>
      </foo:A>
    The element type name A and the attribute name C are in the http://www.foo.org/ namespace because they are mapped there by the foo prefix. The element type name B and the attribute name D are not in any XML namespace because no prefix maps them there. On the other hand, the elements A and B and the attributes C and D are not in any XML namespace, even though they are physically within the scope of the http://www.foo.org/ namespace declaration. This is because XML namespaces contain names, not elements or attributes.

    XML namespaces also do not contain the definitions of the element types or attributes. This is an important difference, as many people are tempted to think of an XML namespace as a schema, which it is not. (For more information, see myth #8 in "Namespace Myths Exploded".)

    (For information about the structure of an XML namespace, see myth #6 in "Namespace Myths Exploded".)

    3.5) Are the names of all element types and attributes in some XML namespace?
    No.

    If an element type or attribute name is not specifically declared to be in an XML namespace -- that is, it is unprefixed and (in the case of element type names) there is no default XML namespace -- then that name is not in any XML namespace. If you want, you can think of it as having a null URI as its name, although no "null" XML namespace actually exists. For example, in the following, the element type name B and the attribute names C and E are not in any XML namespace:

      <foo:A xmlns:foo="http://www.foo.org/">
         <B C="bar"/>
         <foo:D E="bar"/>
      </foo:A>
    For more information about unprefixed attribute names and XML namespaces, see myth #4 in "Namespace Myths Exploded".

    3.6) Do XML namespaces apply to entity names, notation names, or processing instruction targets?
    No.

    XML namespaces apply only to element type and attribute names. Furthermore, in an XML document that conforms to the XML namespaces recommendation, entity names, notation names, and processing instruction targets must not contain colons.

    3.7) Who can create an XML namespace?
    Anybody can create an XML namespace -- all you need to do is assign a URI as its name and decide what element type and attribute names are in it. The URI must be under your control and should not be being used to identify a different XML namespace, such as by a coworker.

    (In practice, most people that create XML namespaces also describe the element types and attributes whose names are in it -- their content models and types, their semantics, and so on. However, this is not part of the process of creating an XML namespace, nor does the XML namespace include or provide a way to discover such information.)

    3.8) Do I need to use XML namespaces?
    Maybe, maybe not.

    If you don't have any naming conflicts in the XML documents you are using today, as is often the case with documents used inside a single organization, then you probably don't need to use XML namespaces. However, if you do have conflicts today, or if you expect conflicts in the future due to distributing your documents outside your organization or bringing outside documents into your organization, then you should probably use XML namespaces.

    Regardless of whether you use XML namespaces in your own documents, it is likely that you will use them in conjunction with some other XML technology, such as XSL, XHTML, or XML Schemas. For example, the following XSLT (XSL Transformations) stylesheet uses XML namespaces to distinguish between element types defined in XSLT and those defined elsewhere:

      <xsl:stylesheet version="1.0"
                     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:template match="Address">
         <!-- The Addresses element type is not part of the XSLT namespace. -->
         <Addresses>
            <xsl:apply-templates/>
         </Addresses>
      </xsl:stylesheet>
    3.9) What is the relationship between XML namespaces and the XML 1.0 recommendation?
    Although the XML 1.0 recommendation anticipated the need for XML namespaces by noting that element type and attribute names should not include colons, it did not actually support XML namespaces. Thus, XML namespaces are layered on top of XML 1.0. In particular, any XML document that uses XML namespaces is a legal XML 1.0 document and can be interpreted as such in the absence of XML namespaces. For example, consider the following document:

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B foo:C="bar"/>
      </foo:A>
    If this document is processed by a namespace-unaware processor, that processor will see two elements whose names are foo:A and foo:B. The foo:A element has an attribute named xmlns:foo and the foo:B element has an attribute named foo:C. On the other hand, a namespace-aware processor will see two elements with universal names {http://www.foo.org}A and {http://www.foo.org}B. The {http://www.foo.org}A does not have any attributes; instead, it has a namespace declaration that maps the foo prefix to the URI http://www.foo.org. The {http://www.foo.org}B element has an attribute named {http://www.foo.org}C.

    Needless to say, this has led to a certain amount of confusion. One area of confusion is the relationship between XML namespaces and validating XML documents against DTDs. This occurs because the XML namespaces recommendation did not describe how to use XML namespaces with DTDs. (For more information, see section 7.) Fortunately, a similar situation does not occur with XML schema languages, as all of these support XML namespaces.

    The other main area of confusion is in recommendations and specifications such as DOM and SAX whose first version predates the XML namespaces recommendation. Although these have since been updated to include XML namespace support, the solutions have not always been pretty due to backwards compatibility requirements. All recommendations in the XML family now support XML namespaces.

    3.10) What is the difference between versions 1.0 and 1.1 of the XML namspaces recommendation?
    There are only two differences between XML namespaces 1.0 and XML namespaces 1.1:

    Version 1.1 adds a way to undeclare prefixes. For more information, see question 4.7.

    Version 1.1 uses IRIs (Internationalized Resource Identifiers) instead of URIs. Basically, URIs are restricted to a subset of ASCII characters, while IRIs allow much broader use of Unicode characters. For complete details, see section 9 of Namespaces in XML 1.1.

    NOTE: As of this writing (February, 2003), Namespaces in XML 1.1 is still a candidate recommendation and not widely used.


    [此贴子已经被作者于2004-2-25 11:28:28编辑过]

       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/2/25 11:21:00
     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 XML基础 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客2
    发贴心情 
    PART II: DECLARING AND USING XML NAMESPACES
    4) SECTION 4: DECLARING XML NAMESPACES IN AN XML DOCUMENT
    4.1) How do I declare an XML namespace in an XML document?
    To declare an XML namespace, you use an attribute whose name has the form:

      xmlns:prefix
            --OR--
      xmlns
    These attributes are often called xmlns attributes and their value is the name of the XML namespace being declared; this is a URI. The first form of the attribute (xmlns:prefix) declares a prefix to be associated with the XML namespace. The second form (xmlns) declares that the specified namespace is the default XML namespace.

    For example, the following declares two XML namespaces, named http://www.tu-darmstadt.de/ito/addresses and http://www.tu-darmstadt.de/ito/servers. The first declaration associates the addr prefix with the http://www.tu-darmstadt.de/ito/addresses namespace and the second declaration states that the http://www.tu-darmstadt.de/ito/servers namespace is the default XML namespace.

      <Department
           xmlns:addr="http://www.tu-darmstadt.de/ito/addresses"
           xmlns="http://www.tu-darmstadt.de/ito/servers">
    NOTE: Technically, xmlns attributes are not attributes at all -- they are XML namespace declarations that just happen to look like attributes. Unfortunately, they are not treated consistently by the various XML recommendations, which means that you must be careful when writing an XML application.

    For example, in the XML Information Set (http://www.w3.org/TR/xml-infoset), xmlns "attributes" do not appear as attribute information items. Instead, they appear as namespace declaration information items. On the other hand, both DOM level 2 and SAX 2.0 treat namespace attributes somewhat ambiguously. In SAX 2.0, an application can instruct the parser to return xmlns "attributes" along with other attributes, or omit them from the list of attributes. Similarly, while DOM level 2 sets namespace information based on xmlns "attributes", it also forces applications to manually add namespace declarations using the same mechanism the application would use to set any other attributes.

    4.2) Where can I declare an XML namespace?
    You can declare an XML namespace on any element in an XML document. The namespace is in scope for that element and all its descendants unless it is overridden (see question 4.5 and question 4.6) or undeclared (see question 4.8). For more information about scope, see section 6.

    4.3) Can I use an attribute default in a DTD to declare an XML namespace?
    Yes.

    For example, the following uses the FIXED attribute xmlns:foo on the A element type to associate the foo prefix with the http://www.foo.org/ namespace. The effect of this is that both A and B are in the http://www.foo.org/ namespace.

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (foo:B)>
         <!ATTLIST foo:A
                   xmlns:foo CDATA #FIXED "http://www.foo.org/">
         <!ELEMENT foo:B (#PCDATA)>
      ]>
      <!-- foo prefix declared through default attribute. -->
      <foo:A>
         <foo:B>abc</foo:B>
      </foo:A>
    IMPORTANT: You should be very careful about placing XML namespace declarations in external entities (external DTDs), as non-validating parsers are not required to read these. For example, suppose the preceding DTD was placed in an external entity (foo.dtd) and that the document was processed by a non-validating parser that did not read foo.dtd. This would result in a namespace error because the foo prefix was never declared:

      <?xml version="1.0" ?>
      <!-- foo.dtd might not be read by non-validating parsers. -->
      <!DOCTYPE foo:A SYSTEM "foo.dtd">
      <!-- foo prefix not declared unless foo.dtd is read. -->
      <foo:A>
         <foo:B>abc</foo:B>
      </foo:A>
    4.4) Do the default values of xmlns attributes declared in the DTD apply to the DTD?
    No.

    Declaring a default value of an xmlns attribute in the DTD does not declare an XML namespace for the DTD. (In fact, no XML namespace declarations apply to DTDs.) Instead, these defaults (declarations) take effect only when the attribute is instantiated on an element. For example:

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (foo:B)>
         <!ATTLIST foo:A
                   xmlns:foo CDATA #FIXED "http://www.foo.org/">
         <!ELEMENT foo:B (#PCDATA)>
      ]>
      <foo:A>  <========== Namespace declaration takes effect here.
         <foo:B>abc</foo:B>
      </foo:A>  <========= Namespace declaration ends here.
    For more information, see question 7.2. (Note that an earlier version of MSXML (the parser used by Internet Explorer) did use fixed xmlns attribute declarations as XML namespace declarations, but that this was removed in MSXML 4. For more information, see question 8.6.)

    4.5) How do I override an XML namespace declaration that uses a prefix?
    To override the prefix used in an XML namespace declaration, you simply declare another XML namespace with the same prefix. For example, in the following, the foo prefix is associated with the http://www.foo.org/ namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are in the http://www.bar.org/ namespace.

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B>
            <foo:C xmlns:foo="http://www.bar.org/">
               <foo:D>abcd</foo:D>
            </foo:C>
         </foo:B>
      </foo:A>
    In general, this leads to documents that are confusing to read and should be avoided.

    4.6) How do I override a default XML namespace declaration?
    To override the current default XML namespace, you simply declare another XML namespace as the default. For example, in the following, the default XML namespace is the http://www.foo.org/ namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are in the http://www.bar.org/ namespace.

      <A xmlns="http://www.foo.org/">
         <B>
            <C xmlns="http://www.bar.org/">
               <D>abcd</D>
            </C>
         </B>
      </A>
    Using multiple default XML namespaces can lead to documents that are confusing to read and should be done carefully. For more information, see question 5.4.

    4.7) How do I undeclare an XML namespace prefix?
    In version 1.0 of the XML namespaces recommendation, you cannot "undeclare" an XML namespace prefix. It remains in scope until the end of the element on which it was declared unless it is overridden. Furthermore, trying to undeclare a prefix by redeclaring it with an empty (zero-length) name (URI) results in a namespace error. For example:

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B>
            <foo:C xmlns:foo="">   <==== This is an error in v1.0, legal in v1.1.
               <foo:D>abcd</foo:D>
            </foo:C>
         </foo:B>
      </foo:A>
    In version 1.1 of the XML namespaces recommendation [currently a candidate recommendation -- February, 2003], you can undeclare an XML namespace prefix by redeclaring it with an empty name. For example, in the above document, the XML namespace declaration xmlns:foo="" is legal and removes the mapping from the foo prefix to the http://www.foo.org URI. Because of this, the use of the foo prefix in the foo:D element results in a namespace error.

    4.8) How do I undeclare the default XML namespace?
    To "undeclare" the default XML namespace, you declare a default XML namespace with an empty (zero-length) name (URI). Within the scope of this declaration, unprefixed element type names do not belong to any XML namespace. For example, in the following, the default XML namespace is the http://www.foo.org/ for the A and B elements and there is no default XML namespace for the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are not in any XML namespace.

      <A xmlns="http://www.foo.org/">
         <B>
            <C xmlns="">
               <D>abcd</D>
            </C>
         </B>
      </A>
    4.9) Why are special attributes used to declare XML namespaces?
    I don't know the answer to this question, but the likely reason is that the hope that they would simplify the process of moving fragments from one document to another document. An early draft of the XML namespaces recommendation proposed using processing instructions to declare XML namespaces. While these were simple to read and process, they weren't easy to move to other documents. Attributes, on the other hand, are intimately attached to the elements being moved.

    Unfortunately, this hasn't worked as well as was hoped. For example, consider the following XML document:

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B>
            <foo:C>bar</foo:C>
         </foo:B>
      </foo:A>
    Simply using a text editor to cut the fragment headed by the <B> element from one document and paste it into another document results in the loss of namespace information because the namespace declaration is not part of the fragment -- it is on the parent element (<A>) -- and isn't moved.

    Even when this is done programmatically, the situation isn't necessarily any better. For example, suppose an application uses DOM level 2 to "cut" the fragment from the above document and "paste" it into a different document. Although the namespace information is transferred (it is carried by each node), the namespace declaration (xmlns attribute) is not, again because it is not part of the fragment. Thus, the application must manually add the declaration before serializing the document or the new document will be invalid.

    4.10) How do different XML technologies treat XML namespace declarations?
    This depends on the technology -- some treat them as attributes and some treat them as namespace declarations. For example, SAX1 treats them as attributes and SAX2 can treat them as attributes or namespace declarations, depending on how the parser is configured. (For more information, see question 9.3 and the SAX2 documentation.) DOM levels 1 and 2 treat them as attributes, but DOM level 2 also interprets them as namespace declarations. (For more information, see question 9.5.) XPath, XSLT, and XML Schemas treat them as namespaces declarations.

    The reason that different technologies treat these differently is that many of these technologies predate XML namespaces. Thus, newer versions of them need to worry both about XML namespaces and backwards compatibility issues.

    5) SECTION 5: USING XML NAMESPACES IN AN XML DOCUMENT
    5.1) How do I use prefixes to refer to element type and attribute names in an XML namespace?
    Make sure you have declared the prefix (see question 4.1) and that it is still in scope (see section 6). All you need to do then is prefix the local name of an element type or attribute with the prefix and a colon. The result is a qualified name (see question 10.1), which the application parses to determine what XML namespace the local name belongs to.

    For example, suppose you have associated the serv prefix with the http://www.tu-darmstadt.de/ito/servers namespace and that the declaration is still in scope. In the following, serv:Address refers to the Address name in the http://www.tu-darmstadt.de/ito/servers namespace. (Note that the prefix is used on both the start and end tags.)

      <!-- serv refers to the http://www.tu-darmstadt.de/ito/servers namespace. -->
      <serv:Address>123.45.67.8</serv:Address>
    Now suppose you have associated the xslt prefix with the http://www.w3.org/1999/XSL/Transform namespace. In the following, xslt:version refers to the version name in the http://www.w3.org/1999/XSL/Transform namespace:

      <!-- xslt refers to the http://www.w3.org/1999/XSL/Transform namespace. -->
      <html xslt:version="1.0">
    5.2) How do I use the default XML namespace to refer to element type names in an XML namespace?
    Make sure you have declared the default XML namespace (see question 4.1) and that that declaration is still in scope (see section 6). All you need to do then is use the local name of an element type. Even though it is not prefixed, the result is still a qualified name (see question 10.1), which the application parses to determine what XML namespace it belongs to.

    For example, suppose you declared the http://www.tu-darmstadt.de/ito/addresses namespace as the default XML namespace and that the declaration is still in scope. In the following, Address refers to the Address name in the http://www.tu-darmstadt.de/ito/addresses namespace.

      <!-- http://www.tu-darmstadt.de/ito/addresses is the default XML namespace. -->
      <Address>123.45.67.8</Address>
    For information about how to use the default XML namespace with attribute names, see question 5.3.

    5.3) How do I use the default XML namespace to refer to attribute names in an XML namespace?
    You can't.

    The default XML namespace only applies to element type names, so you can refer to attribute names that are in an XML namespace only with a prefix. For example, suppose that you declared the http://www.tu-darmstadt.de/ito/addresses namespace as the default XML namespace. In the following, the type attribute name does not refer to that namespace, although the Address element type name does. That is, the Address element type name is in the http://www.tu-darmstadt.de/ito/addresses namespace, but the type attribute name is not in any XML namespace.

      <!-- http://www.tu-darmstadt.de/ito/addresses is the default XML namespace. -->
      <Address type="home">
    To understand why this is true, remember that the purpose of XML namespaces is to uniquely identify element and attribute names. Unprefixed attribute names can be uniquely identified based on the element type to which they belong, so there is no need identify them further by including them in an XML namespace. In fact, the only reason for allowing attribute names to be prefixed is so that attributes defined in one XML language can be used in another XML language.

    For information about how to use the default XML namespace with element type names, see question 5.2. For more information about unprefixed attribute names and XML namespaces, see myth #4 in "Namespace Myths Exploded".

    5.4) When should I use the default XML namespace instead of prefixes?
    This is purely a matter of choice, although your choice may affect the readability of the document. When elements whose names all belong to a single XML namespace are grouped together, using a default XML namespace might make the document more readable. For example:

      <!-- A, B, C, and G are in the http://www.foo.org/ namespace. -->
      <A xmlns="http://www.foo.org/">
         <B>abcd</B>
         <C>efgh</C>
         <!-- D, E, and F are in the http://www.bar.org/ namespace. -->
         <D xmlns="http://www.bar.org/">
            <E>1234</E>
            <F>5678</F>
         </D>
         <!-- Remember! G is in the http://www.foo.org/ namespace. -->
         <G>ijkl</G>
      </A>
    When elements whose names are in multiple XML namespaces are interspersed, default XML namespaces definitely make a document more difficult to read and prefixes should be used instead. For example:

      <A xmlns="http://www.foo.org/">
         <B xmlns="http://www.bar.org/">abcd</B>
         <C xmlns="http://www.foo.org/">efgh</C>
         <D xmlns="http://www.bar.org/">
            <E xmlns="http://www.foo.org/">1234</E>
            <F xmlns="http://www.bar.org/">5678</F>
         </D>
         <G xmlns="http://www.foo.org/">ijkl</G>
      </A>
    In some cases, default namespaces can be processed faster than namespace prefixes, but the difference is certain to be negligible in comparison to total processing time.

    6) SECTION 6: SCOPE OF XML NAMESPACE DECLARATIONS
    6.1) What is the scope of an XML namespace declaration?
    The scope of an XML namespace declaration is that part of an XML document to which the declaration applies. An XML namespace declaration remains in scope for the element on which it is declared and all of its descendants, unless it is overridden or undeclared on one of those descendants (see questions 4.5, 4.6, and 4.8).

    For example, in the following, the scope of the declaration of the http://www.foo.org/ namespace is the element A and its descendants (B and C). The scope of the declaration of the http://www.bar.org/ namespace is only the element C.

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B>
            <bar:C xmlns:bar="http://www.bar.org/" />
         </foo:B>
      </foo:A>
    6.2) Does the scope of an XML namespace declaration include the element it is declared on?
    Yes.

    For example, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://www.foo.org/ namespace. This is because the declaration that associates the foo prefix with the http://www.bar.org/ namespace occurs on the B element, overriding the declaration on the A element that associates it with the http://www.foo.org/ namespace.

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B xmlns:foo="http://www.bar.org/">
            <foo:C>abcd</foo:C>
         </foo:B>
      </foo:A>
    Similarly, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://www.foo.org/ namespace because the declaration declaring http://www.bar.org/ as the default XML namespace occurs on the B element, overriding the declaration on the A element.

      <A xmlns="http://www.foo.org/">
         <B xmlns="http://www.bar.org/">
            <C>abcd</C>
         </B>
      </A>
    A final example is that, in the following, the attribute name D is in the http://www.bar.org/ namespace.

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B foo:D="In http://www.bar.org/ namespace"
                 xmlns:foo="http://www.bar.org/">
            <C>abcd</C>
         </foo:B>
      </foo:A>
    One consequence of XML namespace declarations applying to the elements they occur on is that they actually apply before they appear. Because of this, software that processes qualified names should be particularly careful to scan the attributes of an element for XML namespace declarations before deciding what XML namespace (if any) an element type or attribute name belongs to.

    6.3) If an element or attribute is in the scope of an XML namespace declaration, is its name in that namespace?
    Not necessarily.

    When an element or attribute is in the scope of an XML namespace declaration, the element or attribute's name is checked to see if it has a prefix that matches the prefix in the declaration. Whether the name is actually in the XML namespace depends on whether the prefix matches. For example, in the following, the element type names A, B, and D and the attribute names C and E are in the scope of the declaration of the http://www.foo.org/ namespace. While the names A, B, and C are in that namespace, the names D and E are not.

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B foo:C="foo" />
         <bar:D bar:E="bar" />
      </foo:A>
    6.4) What happens when an XML namespace declaration goes out of scope?
    When an XML namespace declaration goes out of scope, it simply no longer applies. For example, in the following, the declaration of the http://www.foo.org/ namespace does not apply to the C element because this is outside its scope. That is, it is past the end of the B element, on which the http://www.foo.org/ namespace was declared.

      <!-- B is in the http://www.foo.org/ namespace; C is not in any XML namespace. -->
      <A>
         <B xmlns="http://www.foo.org/">abcd</B>
         <C>efgh</C>
      </A>
    In addition to the declaration no longer applying, any declarations that it overrode come back into scope. For example, in the following, the declaration of the http://www.foo.org/ namespace is brought back into scope after the end of the B element. This is because it was overridden on the B element by the declaration of the http://www.bar.org/ namespace.

      <!-- A and C are in the http://www.foo.org/ namespace. B is in the http://www.bar.org/ namespace. -->
      <A xmlns="http://www.foo.org/">
         <B xmlns="http://www.bar.org/">abcd</B>
         <C>efgh</C>
      </A>
    6.5) What happens if no XML namespace declaration is in scope?
    If no XML namespace declaration is in scope, then any prefixed element type or attribute names result in namespace errors. For example, in the following, the names foo:A and foo:B result in namespace errors.

      <?xml version="1.0" ?>
      <foo:A foo:B="error" />
    In the absence of an XML namespace declaration, unprefixed element type and attribute names do not belong to any XML namespace. For example, in the following, the names A and B are not in any XML namespace. For more information, see question 3.5.

      <?xml version="1.0" ?>
      <A B="no error" />
    6.6) Can multiple XML namespace declarations be in scope at the same time?
    Yes, as long as they don't use the same prefixes and at most one of them is the default XML namespace. For example, in the following, the http://www.foo.org/ and http://www.bar.org/ namespaces are both in scope for all elements:

      <A xmlns:foo="http://www.foo.org/"
         xmlns:bar="http://www.bar.org/">
         <foo:B>abcd</foo:B>
         <bar:C>efgh</bar:C>
      </A>
    One consequence of this is that you can place all XML namespace declarations on the root element and they will be in scope for all elements. This is the simplest way to use XML namespaces.

    6.7) How can I declare XML namespaces so that all elements and attributes are in their scope?
    XML namespace declarations that are made on the root element are in scope for all elements and attributes in the document. This means that an easy way to declare XML namespaces is to declare them only on the root element. For example:

      <Department
             xmlns:addr="http://www.tu-darmstadt.de/ito/addresses"
             xmlns:serv="http://www.tu-darmstadt.de/ito/servers">
         <Name>DVS1</Name>
         <addr:Address>
            <addr:Street>Wilhelminenstr. 7</addr:Street>
            <addr:City>Darmstadt</addr:City>
            <addr:State>Hessen</addr:State>
            <addr:Country>Germany</addr:Country>
            <addr:PostalCode>D-64285</addr:PostalCode>
         </addr:Address>
         <serv:Server>
            <serv:Name>OurWebServer</serv:Name>
            <serv:Address>123.45.67.8</serv:Address>
         </serv:Server>
      </Department>
    6.8) Does the scope of an XML namespace declaration ever include the DTD?
    No.

    XML namespaces can be declared only on elements and their scope consists only of those elements and their descendants. Thus, the scope can never include the DTD. For more information, see question 7.2.

    7) SECTION 7: XML NAMESPACES AND DTDs
    7.1) Can I use XML namespaces in DTDs?
    Yes and no.

    In particular, DTDs can contain qualified names (see question 7.3) but XML namespace declarations do not apply to DTDs (see question 7.2).

    This has a number of consequences. Because XML namespace declarations do not apply to DTDs:

    There is no way to determine what XML namespace a prefix in a DTD points to. Which means...

    Qualified names in a DTD cannot be mapped to universal names. Which means...

    Element type and attribute declarations in a DTD are expressed in terms of qualified names, not universal names. Which means...

    Validation cannot be redefined in terms of universal names as might be expected.

    This situation has caused numerous complaints but, as XML namespaces are already a recommendation, is unlikely to change. The long term solution to this problem is an XML schema language: all of the proposed XML schema languages provide a mechanism by which the local name in an element type or attribute declaration can be associated with an XML namespace. This makes it possible to redefine validity in terms of universal names.

    7.2) Do XML namespace declarations apply to DTDs?
    No.

    In particular, an xmlns attribute declared in the DTD with a default is not an XML namespace declaration for the DTD. For more information, see question 4.4. (Note that an earlier version of MSXML (the parser used by Internet Explorer) did use such declarations as XML namespace declarations, but that this was removed in MSXML 4. For more information, see question 8.6.)

    7.3) Can I use qualified names in DTDs?
    Yes.

    For example, the following is legal:

      <!ELEMENT foo:A (foo:B)>
      <!ATTLIST foo:A
                foo:C CDATA #IMPLIED>
      <!ELEMENT foo:B (#PCDATA)>
    However, because XML namespace declarations do not apply to DTDs (see question 7.2), qualified names in the DTD cannot be converted to universal names. As a result, qualified names in the DTD have no special meaning. For example, foo:A is just foo:A -- it is not A in the XML namespace to which the prefix foo is mapped.

    The reason qualified names are allowed in the DTD is so that validation will continue to work. For more information, see question 7.6.

    7.4) Can the content model in an element type declaration contain element types whose names come from other XML namespaces?
    Yes and no.

    The answer to this question is yes in the sense that a qualified name in a content model can have a different prefix than the qualified name of the element type being declared. For example, the following is legal:

      <!ELEMENT foo:A (bar:B, baz:C)>
    The answer to this question is no in the sense that XML namespace declarations do not apply to DTDs so the prefixes used in an element type declaration are technically meaningless. In particular, they do not specify that the name of a certain element type belongs to a certain namespace. Nevertheless, the ability to mix prefixes in this manner is crucial when: a) you have a document whose names come from multiple XML namespaces (see question 8.5), and b) you want to construct that document in a way that is both valid and conforms to the XML namespaces recommendation (see question 7.6).

    7.5) Can the attribute list of an element type contain attributes whose names come from other XML namespaces?
    Yes and no, for the reasons listed in question 7.4.

    For example, the following is legal:

      <!ATTLIST foo:A
                bar:B CDATA #IMPLIED>
    7.6) How can I construct an XML document that is valid and conforms to the XML namespaces recommendation?
    In answering this question, it is important to remember that:

    Validity is a concept defined in XML 1.0,

    XML namespaces are layered on top of XML 1.0 (see myth #11 in "Namespace Myths Exploded"), and

    The XML namespaces recommendation does not redefine validity, such as in terms of universal names (see myth #9 in "Namespace Myths Exploded").

    Thus, validity is the same for a document that uses XML namespaces and one that doesn't. In particular, with respect to validity:

    xmlns attributes are treated as attributes, not XML namespace declarations.

    Qualified names are treated like other names. For example, in the name foo:A, foo is not treated as a namespace prefix, the colon is not treated as separating a prefix from a local name, and A is not treated as a local name. The name foo:A is treated simply as the name foo:A.

    Because of this, XML documents that you might expect to be valid are not. For example, the following document is not valid because the element type name A is not declared in the DTD, in spite of the fact both foo:A and A share the universal name {http://www.foo.org/}A:

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (#PCDATA)>
         <!ATTLIST foo:A
                   xmlns:foo CDATA #FIXED "http://www.foo.org/"
                   xmlns CDATA #FIXED "http://www.foo.org/">
      ]>
      <A/>
    Similarly, the following is not valid because the xmlns attribute is not declared in the DTD:

      <?xml version="1.0" ?>
      <!DOCTYPE A [
         <!ELEMENT A (#PCDATA)>
      ]>
      <A xmlns="http://www.foo.org/" />
    Furthermore, documents that you might expect to be invalid are valid. For example, the following document is valid but contains two definitions of the element type with the universal name {http://www.foo.org/}A:

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (bar:A)>
         <!ATTLIST foo:A
                   xmlns:foo CDATA #FIXED "http://www.foo.org/">
         <!ELEMENT bar:A (#PCDATA)>
         <!ATTLIST bar:A
                   xmlns:bar CDATA #FIXED "http://www.foo.org/">
      ]>
      <foo:A>
         <bar:A>abcd</bar:A>
      </foo:A>
    Finally, validity has nothing to do with correct usage of XML namespaces. For example, the following document is valid but does not conform to the XML namespaces recommendation because the foo prefix is never declared:

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (#PCDATA)>
      ]>
      <foo:A />
    Therefore, when constructing an XML document that uses XML namespaces, you need to do both of the following if you want the document to be valid:

    Declare xmlns attributes in the DTD.
    Use the same qualified names in the DTD and the body of the document.
    For example:

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (foo:B)
         <!ATTLIST foo:A
                   xmlns:foo CDATA #FIXED "http://www.foo.org/">
         <!ELEMENT foo:B (#PCDATA)>
      ]>
      <foo:A>
         <foo:B>abcd</foo:B>
      </foo:A>
    There is no requirement that the same prefix always be used for the same XML namespace. For example, the following is also valid:

      <?xml version="1.0" ?>
      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (bar:B)>
         <!ATTLIST foo:A
                   xmlns:foo CDATA #FIXED "http://www.foo.org/">
         <!ELEMENT bar:B (#PCDATA)>
         <!ATTLIST bar:B
                   xmlns:bar CDATA #FIXED "http://www.foo.org/">
      ]>
      <foo:A>
         <bar:B />
      </foo:A>
    However, documents that use multiple prefixes for the same XML namespace or the same prefix for multiple XML namespaces are confusing to read and thus prone to error. They also allow abuses such as defining an element type or attribute with a given universal name more than once, as was seen earlier. Therefore, a better set of guidelines for writing documents that are both valid and conform to the XML namespaces recommendation is:

    Declare all xmlns attributes in the DTD.
    Use the same qualified names in the DTD and the body of the document.
    Use one prefix per XML namespace.
    Do not use the same prefix for more than one XML namespace.
    Use at most one default XML namespace.
    The latter three guidelines guarantee that prefixes are unique. This means that prefixes fulfill the role normally played by namespace names (URIs) -- uniquely identifying an XML namespace -- and that qualified names are equivalent to universal names, so a given universal name is always represented by the same qualified name. Although this is contrary to the spirit of prefixes, which were designed for their flexibility, there is no other solution at this point.

    7.7) How can I validate an XML document that uses XML namespaces?
    When people ask this question, they usually assume that validity is different for documents that use XML namespaces and documents that don't. In fact, it isn't -- it's the same for both. Thus, there is no difference between validating a document that uses XML namespaces and validating one that doesn't. In either case, you simply use a validating parser or other software that performs validation.

    For information about how to construct an XML document that is valid and conforms to the XML namespace recommendation, see question 7.6.

    7.8) If I start using XML namespaces, do I need to change my existing DTDs?
    Probably.

    If you want your XML documents to be both valid and conform to the XML namespaces recommendation, you need to declare any xmlns attributes and use the same qualified names in the DTD as in the body of the document. (For more information, see question question 7.6.)

    If your DTD contains element type and attribute names from a single XML namespace, the easiest thing to do is to use your XML namespace as the default XML namespace. To do this, declare the attribute xmlns (no prefix) for each possible root element type. If you can guarantee that the DTD is always read (see question 4.3), set the default value in each xmlns attribute declaration to the URI used as your namespace name. Otherwise, declare your XML namespace as the default XML namespace on the root element of each instance document.

    If your DTD contains element type and attribute names from multiple XML namespaces, you need to choose a single prefix for each XML namespace and use these consistently in qualified names in both the DTD and the body of each document. You also need to declare your xmlns attributes in the DTD and declare your XML namespaces. As in the single XML namespace case, the easiest way to do this is add xmlns attributes to each possible root element type and use default values if possible.

    Note that you should only need to make these changes once.

    8) SECTION 8: USING DOCUMENTS THAT USE XML NAMESPACES
    8.1) How do I create documents that use XML namespaces?
    The same as you create documents that don't use XML namespaces. If you're currently using Notepad on Windows or emacs on Linux, you can continue using Notepad or emacs. If you're using an XML editor that is not namespace-aware, you can also continue to use that, as qualified names are legal names in XML documents and xmlns attributes are legal attributes. And if you're using an XML editor that is namespace-aware, it will probably provide features such as automatically declaring XML namespaces and keeping track of prefixes and the default XML namespace for you.

    8.2) How can I check that a document conforms to the XML namespaces recommendation?
    Unfortunately, I know of no software that only checks for conformance to the XML namespaces recommendation. It is possible that some namespace-aware validating parsers (such as those from DataChannel (Microsoft), IBM, Oracle, or Sun) check XML namespace conformance as part of parsing and validating. Thus, you might be able to run your document through such parsers as a way of testing conformance.

    Note that writing an application to check conformance to the XML namespaces recommendation is not as easy as it might seem. The problem is that most parsers do not make DTD information available to the application, so it might not be possible to check conformance in the DTD. Also note that writing a SAX 1.0 application that checks conformance in the body of the document (as opposed to the DTD) should be an easy thing to do. (John Cowan's Namespace SAX Filter probably already does this. For more information, see question 9.2.)

    8.3) Can I use the same document with both namespace-aware and namespace-unaware applications?
    Yes.

    This situation is quite common, such as when a namespace-aware application is built on top of a namespace-unaware parser. Another common situation is when you create an XML document with a namespace-unaware XML editor but process it with a namespace-aware application.

    Using the same document with both namespace-aware and namespace-unaware applications is possible because XML namespaces use XML syntax. That is, an XML document that uses XML namespaces is still an XML document and is recognized as such by namespace-unaware software.

    The only thing you need to be careful about when using the same document with both namespace-aware and namespace-unaware applications is when the namespace-unaware application requires the document to be valid. In this case, you must be careful to construct your document in a way that is both valid and conforms to the XML namespaces recommendation. (It is possible to construct documents that conform to the XML namespaces recommendation but are not valid and vice versa.) For information about how to do this, see question 7.6.

    8.4) What software is needed to process XML namespaces?
    From a document author's perspective, this is generally not a relevant question. Most XML documents are written in a specific XML language and processed by an application that understands that language. If the language uses an XML namespace, then the application will already use that namespace -- there is no need for any special XML namespace software.

    For information about the software application writers use to process XML namespaces, see section 9.

    8.5) How can I use XML namespaces to combine documents that use different element type and attribute names?
    Before we answer this question, we need to clear up a common misconception about XML namespaces. It is often believed that XML namespaces provide some sort of magic that allows you to automatically combine XML documents that use separate sets of element types and attributes. This is not true. Although XML namespaces provide some of the tools you need to do this, you still need to do most of the work yourself, as you will see in the answer to this question.

    To combine documents that use separate sets of element types and attributes, all you really need to do is decide where the elements and attributes go in the final document. For example, does element type A from the first document go inside element type B? Is it a sibling? Or are they completely unrelated? Although the procedure for actually combining two documents can be easily automated, deciding how to combine them is not likely to ever be automated. Instead, it is a strictly human problem, requiring somebody to make choices.

    For example, suppose we have a document containing addresses:

      <Address>
         <Street>Wilhelminenstr. 7</Street>
         <City>Darmstadt</City>
         <State>Hessen</State>
         <Country>Germany</Country>
         <PostalCode>D-64285</PostalCode>
      </Address>
    and a document containing Web server information:

      <Server>
         <Name>OurWebServer</Name>
         <Address>123.45.67.8</Address>
      </Server>
    Now suppose we would like a document of departments, their addresses, and their Web servers, which we can make from the above documents plus a little additional information:

      <Departments>
         <Department>
            <Name>DVS1</Name>
            <Address>
               <Street>Wilhelminenstr. 7</Street>
               <City>Darmstadt</City>
               <State>Hessen</State>
               <Country>Germany</Country>
               <PostalCode>D-64285</PostalCode>
            </Address>
            <Server>
               <Name>OurWebServer</Name>
               <Address>123.45.67.8</Address>
            </Server>
         </Department>
         ...
      </Departments>
    Unfortunately, we have a problem: there are two Address element types and two Name element types. (Although the Name element types both have a content model of PCDATA, they have different meanings -- one is the name of the department and the other is the name of the server -- so we would like to recognize them as different element types.)

    To solve these conflicts, we can use XML namespaces. We assign the address information to the http://www.tu-darmstadt.de/ito/addresses namespace, the server information to the http://www.tu-darmstadt.de/ito/servers namespace, and the newly added departmental information to the http://www.tu-darmstadt.de/ito/depts namespace. Thus, our new document looks like:

      <dept:Departments
            xmlns:dept="http://www.tu-darmstadt.de/ito/depts
            xmlns:addr="http://www.tu-darmstadt.de/ito/addresses
            xmlns:serv="http://www.tu-darmstadt.de/ito/servers>
         <dept:Department>
            <dept:Name>DVS1</dept:Name>
            <addr:Address>
               <addr:Street>Wilhelminenstr. 7</addr:Street>
               <addr:City>Darmstadt</addr:City>
               <addr:State>Hessen</addr:State>
               <addr:Country>Germany</addr:Country>
               <addr:PostalCode>D-64285</addr:PostalCode>
            </addr:Address>
            <serv:Server>
               <serv:Name>OurWebServer</serv:Name>
               <serv:Address>123.45.67.8</serv:Address>
            </serv:Server>
         </dept:Department>
         ...
      </dept:Departments>
    Although we could have assigned only the conflicting element type names to XML namespaces, it is generally a better idea to keep the names of related element types in a single XML namespace. This avoids questions of having to remember which names have been placed in XML namespaces and which haven't, as well as avoiding future conflicts that might occur when these types are used elsewhere. (This does not necessarily mean that the names of all related element types and attributes should be placed in a single XML namespace. If you have a large and complex set of element types and attributes and expect to reuse subsets of it elsewhere, it might make more sense to place the names of each reusable subset in its own XML namespace.)

    This example was fairly simple in that it did not require us to change any existing content models. However, suppose we wanted to keep information only about servers: their name, IP address, and physical location. In this case, we might want to add the Address element type that describes street address to the content of the Server element type:

      <serv:Servers
            xmlns:addr="http://www.tu-darmstadt.de/ito/addresses
            xmlns:serv="http://www.tu-darmstadt.de/ito/servers2>
         <serv:Server>
            <serv:Name>OurWebServer</serv:Name>
            <serv:Address>123.45.67.8</serv:Address>
            <addr:Address>
               <addr:Street>Wilhelminenstr. 7</addr:Street>
               <addr:City>Darmstadt</addr:City>
               <addr:State>Hessen</addr:State>
               <addr:Country>Germany</addr:Country>
               <addr:PostalCode>D-64285</addr:PostalCode>
            </addr:Address>
         </serv:Server>
         ...
      </serv:Servers>
    Notice that we have changed the name of the XML namespace for the server information to http://www.tu-darmstadt.de/ito/servers2. This is because we changed the content model of the Server element type. In other words, the content models of the two Server element types are different, so they are two different element types with the same name and should reside in separate XML namespaces. Had we not made this change, software searching for the old Server element type would have incorrectly processed the new Server element type and vice versa.

    As you can see, there is nothing magic about combining documents. It is simply a matter of deciding how to fit them together and, although there are likely to be tools in the future that make this as easy as drag-and-drop, the decisions about exactly how to combine them are likely to always require human intervention.

    You can also see that XML namespaces played a vital role in this process -- they allowed us to combine the documents without changing the local names of any of the element types. This is important, as without XML namespaces we would endlessly have to invent new ways of renaming simple element types like Address. However, it is also important to understand that resolving duplicate names is the only role XML namespaces played in this process -- the rest was left to the people making the decisions.

    8.6) How do I use XML namespaces with Internet Explorer 5.0 and/or the MSXML parser?
    WARNING! The following applies only to earlier versions of MSXML. It does not apply to MSXML 4, which is the currently shipping version [July, 2002].

    An early version of the MSXML parser, which was shipped as part of Internet Explorer 5.0, required that every XML namespace prefix used in an element type or attribute declaration had to be "declared" in the attribute declaration for that element type. This had to be done with a fixed xmlns attribute declaration. For example, the following was accepted by MSXML and both xmlns:foo attributes were required:

       <!ELEMENT foo:A (#PCDATA)>
       <!ATTLIST foo:A
                 xmlns:foo CDATA #FIXED "http://www.foo.org/">
       <!ELEMENT foo:B (#PCDATA)>
       <!ATTLIST foo:B
                 xmlns:foo CDATA #FIXED "http://www.foo.org/">
    MSXML returned an error for the following because the second foo prefix was not "declared":

       <!ELEMENT foo:A (#PCDATA)>
       <!ATTLIST foo:A
                 xmlns:foo CDATA #FIXED "http://www.foo.org/">
       <!ELEMENT foo:B (#PCDATA)>
    The reason for this restriction was so that MSXML could use universal names to match element type and attribute declarations to elements and attributes during validation. Although this would have simplified many of the problems of writing documents that are both valid and conform to the XML namespaces recommendation (see question 7.6), some users complained about it because it was not part of the XML namespaces recommendation. In response to these complaints, Microsoft removed this restriction in later versions, which are now shipping. Ironically, the idea was later independently derived as a way to resolve the problems of validity and namespaces. However, it has not been implemented by anyone.

    9) SECTION 9: PROCESSING XML NAMESPACES IN XML APPLICATIONS
    9.1) How do applications process documents that use XML namespaces?
    Applications process documents that use XML namespaces in almost exactly the same way they process documents that don't use XML namespaces. For example, if a namespace-unaware application adds a new sales order to a database when it encounters a SalesOrder element, the equivalent namespace-aware application does the same. The only difference is that the namespace-aware application:

    Might need to check for xmlns attributes and parse qualified names. Whether it does this depends on whether such processing is already done by lower-level software, such as a namespace-aware DOM implementation.

    Uses universal (two-part) names instead of local (one-part) names. For example, the namespace-aware application might add a new sales order in response to an {http://www.tu-darmstadt.de/ito/sales}SalesOrder element instead of a SalesOrder element.

    9.2) How do I use XML namespaces with SAX 1.0?
    The easiest way to use XML namespaces with SAX 1.0 is to use John Cowan's Namespace SAX Filter (see http://www.ccil.org/~cowan/XML). This is a SAX filter that keeps track of XML namespace declarations, parses qualified names, and returns element type and attribute names as universal names in the form:

      URI^local-name
    For example:

      http://www.tu-darmstadt.de/ito/sales^SalesOrder
    Your application can then base its processing on these longer names. For example, the code:

       public void startElement(String elementName, AttributeList attrs)
          throws SAXException
       {
          ...
          if (elementName.equals("SalesOrder"))
          {
             // Add new database record.
          }
          ...
       }
    might become:

       public void startElement(String elementName, AttributeList attrs)
          throws SAXException
       {
          ...
          if (elementName.equals("http://www.tu-darmstadt.de/sales^SalesOrder"))
          {
             // Add new database record.
          }
          ...
       }
    or:

       public void startElement(String elementName, AttributeList attrs)
          throws SAXException
       {
          ...
          // getURI() and getLocalName() are utility functions to parse universal names.

          if (getURI(elementName).equals("http://www.tu-darmstadt.de/ito/sales"))
          {
             if (getLocalName(elementName).equals("SalesOrder"))
             {
                // Add new database record.
             }
          }
          ...
       }
    If you do not want to use the Namespace SAX Filter, then you will need to do the following in addition to identifying element types and attributes by their universal names:

    In startElement, scan the attributes for XML namespace declarations before doing any other processing. You will need to maintain a table of current prefix-to-URI mappings (including a null prefix for the default XML namespace).

    In startElement and endElement, check whether the element type name includes a prefix. If so, use your mappings to map this prefix to a URI. Depending on how your software works, you might also check if the local part of the qualified name includes any colons, which are illegal.

    In startElement, check whether attribute names include a prefix. If so, process as in the previous point.

    9.3) How do I use XML namespaces with SAX 2.0?
    SAX 2.0 primarily supports XML namespaces through the following methods:

    startElement and endElement in the ContentHandler interface return namespace names (URIs) and local names as well as qualified names.

    getValue, getType, and getIndex in the Attributes interface can retrieve attribute information by namespace name (URI) and local name as well as by qualified name.

    For example, the namespace-unaware SAX 1.0 code:

       public void startElement(String elementName, AttributeList attrs)
          throws SAXException
       {
          ...
          if (elementName.equals("SalesOrder"))
          {
             // Add new database record.
          }
          ...
       }
    might become:

       public void startElement(String namespaceURI,
                                String localName,
                                String qualifiedName,
                                Attributes attrs)
          throws SAXException
       {
          ...
          if (namespaceURI.equals("http://www.tu-darmstadt.de/sales") &&
              localName.equals("SalesOrder"))
          {
             // Add new database record.
          }
          ...
       }
    Although the above methods are sufficient for most applications, SAX 2.0 also supports the following:

    startPrefixMapping and endPrefixMapping in the ContentHandler interface return scope information about individual prefixes.

    getURI, getLocalName, and getQName in the Attributes interface return the namespace name (URI), local name, and qualified name of an attribute by index.

    The http://xml.org/features/namespaces and http://xml.org/features/namespace-prefixes properties allow applications to request whether parsers return qualified names and xmlns attributes.

    The NamespaceSupport class helps applications track the currently declared namespace prefixes and names (URIs).

    For complete details, see the SAX 2.0 specification (http://www.saxproject.org/).

    9.4) How do I use XML namespaces with DOM level 1?
    This depends on what DOM level 1 implementation you are using. If you are using a namespace-aware DOM implementation, such as those from Data Channel (Microsoft), IBM, Oracle, or Sun, then all you need to do is use the methods provided by those implementations to retrieve the namespace name (URI) and local names. For example, the namespace-unaware code:

      // Check the local name.
      // getNodeName() is a DOM level 1 method.

      if (elementNode.getNodeName().equals("SalesOrder"))
      {
         // Add new database record.
      }
    might become the following namespace-aware code when using Oracle's DOM implementation:

      // Check the XML namespace name (URI).
      // getNamespace() and NSElement are Oracle-specific.

      if ((NSElement)elementNode).getNamespace().equals("http://www.tu-darmstadt.de/ito/sales"))
      {

         // Check the local name.
         // getLocalName() is Oracle-specific.

         if ((NSElement)elementNode.getLocalName().equals("SalesOrder"))
         {
            // Add new database record.
         }
      }
    Because DOM level 1 does not include XML namespace support, each DOM level 1 implementation provides a slightly different interface for accessing XML namespace information. Most of these implementations include some combination of methods for getting the qualified name, namespace name (URI), local name, and prefix. Therefore, if you are writing a DOM-neutral application, you will need to define your own XML namespace interface and write converters for each DOM implementation you support. Fortunately, this is easy to do.

    If you are using a namespace-unaware DOM implementation, such as Docuverse version 1 or OpenXML version 1 (these may support XML namespaces in a later version), you will need to perform namespace processing yourself. Although this is largely the same as in a SAX 1.0 application (see question 9.2) -- checking for xmlns attributes on each Element node and converting qualified names on Element and Attr nodes to universal names -- it is potentially more difficult to determine what XML namespace declarations are in scope for any given node. If your application traverses the DOM tree in order from the root, this is fairly easy, as you can maintain prefix-to-URI mappings as you go. However, if you access the tree randomly, you may want to construct a parallel tree with mapping information before you do any other processing or rebuild the DOM tree using prefixes that map to known URIs.

    9.5) How do I use XML namespaces with DOM level 2?
    DOM level 2 supports XML namespaces through a number of new methods and attributes. The most important of these are the namespaceURI and localName attributes in the Node interface; these allow applications to identify nodes by their namespace name (URI) and local name rather than by qualified name. For example, the namespace-unaware code:

      // Check the local name.
      // getNodeName() is a DOM level 1 method.

      if (elementNode.getNodeName().equals("SalesOrder"))
      {
         // Add new database record.
      }
    might become the following namespace-aware code:

      // Check the XML namespace name (URI).
      // getNamespaceURI() is a DOM level 2 method.

      if (elementNode.getNamespaceURI().equals("http://www.tu-darmstadt.de/ito/sales"))
      {

         // Check the local name.
         // getLocalName() is a DOM level 2 method.

         if (elementNode.getLocalName().equals("SalesOrder"))
         {
            // Add new database record.
         }
      }
    Note that, unlike SAX 2.0, DOM level 2 treats xmlns attributes as normal attributes.

    For complete details, see the DOM level 2 recommendation (http://www.w3.org/TR/DOM-Level-2/).

    9.6) Can an application process documents that use XML namespaces and documents that don't use XML namespaces?
    Yes.

    This is a common situation for generic applications, such as editors, browsers, and parsers, that are not wired to understand a particular XML language. Such applications simply treat all element type and attribute names as qualified names. Those names that are not mapped to an XML namespace -- that is, unprefixed element type names in the absence of a default XML namespace and unprefixed attribute names -- are simply processed as one-part names, such as by using a null XML namespace name (URI).

    Note that such applications must decide how to treat documents that do not conform to the XML namespaces recommendation. For example, what should the application do if an element type name contains a colon (thus implying the existence of a prefix), but there are no XML namespace declarations in the document? The application can choose to treat this as an error, or it can treat the document as one that does not use XML namespaces, ignore the "error", and continue processing.

    9.7) Can an application be both namespace-aware and namespace-unaware?
    Yes.

    However, there is generally no reason to do this. The reason is that most applications understand a particular XML language, such as one used to transfer sales orders between companies. If the element type and attribute names in the language belong to an XML namespace, the application must be namespace-aware; if not, the application must be namespace-unaware. For example, such an application should never need to recognize the element type names SalesOrder and {http://www.tu-darmstadt.de/ito/sales}SalesOrder.

    For a few applications, being both namespace-aware and namespace-unaware makes sense. For example, a parser might choose to redefine validity in terms of universal names (see myth #9 in "Namespace Myths Exploded") and have both namespace-aware and namespace-unaware validation modes. However, such applications are uncommon.

    9.8) What does a namespace-aware application do when it encounters an error?
    The XML namespaces recommendation does not specify what a namespace-aware application does when it encounters a document that does not conform to the recommendation. Therefore, the behavior is application-dependent. For example, the application could stop processing, post an error to a log and continue processing, or ignore the error.

    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/2/25 11:29:00
     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 XML基础 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客3
    发贴心情 
    PART III: NAMES, PREFIXES, AND URIs
    10) SECTION 10: NAMES
    10.1) What is a qualified name?
    A qualified name is a name of the following form. It consists of an optional prefix and colon, followed by the local part, which is sometimes known as a local name.

      prefix:local-part
            --OR--
      local-part
    For example, both of the following are qualified names. The first name has a prefix of serv; the second name does not have a prefix. For both names, the local part (local name) is Address.

      serv:Address
      Address
    In most circumstances, qualified names are mapped to universal names. For more information, see question 10.5.

    10.2) What characters are allowed in a qualified name?
    The prefix can contain any character that is allowed in the Name [5] production in XML 1.0 except a colon. The same is true of the local name. Thus, there can be at most one colon in a qualified name -- the colon used to separate the prefix from the local name.

    10.3) Where can qualified names appear?
    Qualified names can appear anywhere an element type or attribute name can appear: in start and end tags, as the document element type, and in element type and attribute declarations in the DTD. For example:

      <!DOCTYPE foo:A [
         <!ELEMENT foo:A (foo:B)>
         <!ATTLIST foo:A
                   foo:C CDATA #IMPLIED>
         <!ELEMENT foo:B (#PCDATA)>
      ]>
      <foo:A xmlns:foo="http://www.foo.org/" foo:C="bar">
         <foo:B>abcd</foo:B>
      <foo:A>
    Qualified names cannot appear as entity names, notation names, or processing instruction targets.

    10.4) Can qualified names be used in attribute values?
    Yes, but they have no special significance. That is, they are not necessarily recognized as such and mapped to universal names. For example, the value of the C attribute in the following is the string "foo:D", not the universal name {http://www.foo.org/}D.

      <foo:A xmlns:foo="http://www.foo.org/">
         <foo:B C="foo:D"/>
      <foo:A>
    In spite of this, there is nothing to stop an application from recognizing a qualified name in an attribute value and processing it as such. This done in technologies being developed today. For example, in the following XML Schemas definition, the attribute value xsd:string identifies the type of the foo attribute as the universal name {http://www.w3.org/1999/XMLSchema}string.

       <xsd:attribute name="foo" type="xsd:string" />
    There are two potential problems with this. First, the application must be able to retrieve the prefix mappings currently in effect. Fortunately, both SAX 2.0 and DOM level 2 support this capability. Second, any general purpose transformation tool, such as one that writes an XML document in canonical form and changes namespace prefixes in the process, will not recognize qualified names in attribute values and therefore not transform them correctly. Although this may be solved in the future by the introduction of the QName (qualified name) data type in XML Schemas, it is a problem today.

    10.5) How are qualified names mapped to names in XML namespaces?
    If a qualified name in the body of a document (as opposed to the DTD) includes a prefix, then that prefix is used to map the local part of the qualified name to a universal name -- that is, a name in an XML namespace. (Note that the prefix must be in scope -- see section 6.) For example, in the following, the prefix foo is used to map the local names A, B, and C to names in the http://www.foo.org/ namespace:

      <?xml version="1.0" ?>
      <foo:A xmlns:foo="http://www.foo.org/" foo:C="bar">
         <foo:B>abcd</foo:B>
      <foo:A>
    If a qualified name in the body of a document does not include a prefix and a default XML namespace is in scope then one of two things happens. If the name is used as an element tag, it is mapped to a name in the default XML namespace. If it is used as an attribute name, it is not in any XML namespace (see myth #4 in "Namespace Myths Exploded"). For example, in the following, A and B are in the http://www.foo.org/ namespace and C is not in any XML namespace:

      <?xml version="1.0" ?>
      <A xmlns="http://www.foo.org/" C="bar">
         <B>abcd</B>
      <A>
    If a qualified name in the body of a document does not include a prefix and no default XML namespace is in scope, then that name is not in any XML namespace. For example, in the following, A, B, and C are not in any XML namespace:

      <?xml version="1.0" ?>
      <A C="bar">
         <B>abcd</B>
      <A>
    Qualified names in the DTD are never mapped to names in an XML namespace because they are never in the scope of an XML namespace declaration. (For more information, see question 7.2.)

    10.6) What is a prefixed name?
    A prefixed name is a qualified name (see question 10.1) that contains a prefix.

    10.7) What is an unprefixed name?
    An unprefixed name is a qualified name (see question 10.1) that does not contain a prefix.

    10.8) Are unprefixed names in an XML namespace?
    Only if they are used in the body of a document (as opposed to the DTD) as an element tag and a default XML namespace is in scope. For more information, see question 5.2.

    10.9) What is a local name?
    This is another term for the local part of a qualified name. For more information, see question 10.1.

    10.10) What is a namespace name?
    This is the name used to identify a namespace. It is a URI. For more information, see question 12.2.

    10.11) What is a universal name?
    A universal name is a two part name consisting of an XML namespace name (URI) and a local name. For example, in the following, the universal name is the namespace name "http://www.tu-darmstadt.de/ito/servers" plus the local name "Address":

      <serv:Address xmlns:serv="http://www.tu-darmstadt.de/ito/servers">123.45.67.8</serv:Address>
    The ability to specify universal names is the only function of XML namespaces.

    10.12) How are universal names represented?
    There is no standard way to represent a universal name. However, three representations are common.

    The first representation keeps the XML namespace name (URI) and the local name separate. For example, many DOM level 1 implementations have different methods for returning the XML namespace name (URI) and the local name of an element or attribute node.

    The second representation concatenates the namespace name (URI) and the local name with caret (^). The result is a universally unique (see question 10.12) name, since carets are not allowed in URIs or local names. This is the method used by John Cowan's Namespace SAX Filter (see question 9.2). For example, the universal name that has the URI http://www.tu-darmstadt.de/ito/servers and the local name Address would be represented as:

      http://www.tu-darmstadt.de/ito/servers^Address
    The third representation, which we use in this FAQ, was suggested by James Clark in his XML Namespaces paper (see question 14.1). It places the XML namespace name (URI) in braces and concatenates this with the local name. This notation is suggested only for documentation and I am aware of no code that uses it. For example, the above name would be represented as:

      {http://www.tu-darmstadt.de/ito/servers}Address
    10.13) Are universal names universally unique?
    No, but it is reasonable to assume they are.

    Universal element type and attribute names are not guaranteed to be universally unique -- that is, unique within the space of all XML documents -- because it is possible for two different people, each defining their own XML namespace, to use the same URI and the same element type or attribute name. However, this occurs only if:

    One or both people use a URI that is not under their control, such as somebody outside Netscape using the URI http://www.netscape.com/, or

    Both people have control over a URI and both use it.

    The first case means somebody is cheating when assigning URIs (a process governed by trust) and the second case means that two people within an organization are not paying attention to each other's work. For widely published element type and attribute names, neither case is very likely. Thus, it is reasonable to assume that universal names are universally unique. (Since both cases are possible, applications that present security risks should be careful about assuming that universal names are universally unique.)

    For information about the ability of universal names to uniquely identify element types and attributes (as opposed to the names themselves being unique), see myth #2 in "Namespace Myths Exploded".

    11) SECTION 11: XML NAMESPACE PREFIXES
    11.1) What is an XML namespace prefix?
    An XML namespace prefix is a prefix used to specify that a local element type or attribute name is in a particular XML namespace. For example, in the following, the serv prefix specifies that the Address element type name is in the http://www.tu-darmstadt.de/ito/addresses namespace:

      <serv:Addresses xmlns:serv="http://www.tu-darmstadt.de/ito/addresses">
    11.2) What characters are allowed in an XML namespace prefix?
    The prefix can contain any character that is allowed in the Name [5] production in XML 1.0 except a colon.

    11.3) Are prefixes significant?
    No.

    For example, the following are equivalent:

      <serv:Addresses xmlns:serv="http://www.tu-darmstadt.de/ito/addresses">
    and

      <foo:Addresses xmlns:foo="http://www.tu-darmstadt.de/ito/addresses">
    However, prefixes provide a visual cue to readers. Because of this, software (especially editors) should consider preserving prefixes. For example, this is much easier to read:

      <xsl:stylesheet version="1.0"
                      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      xmlns:html="http://www.w3.org/TR/xhtml1/strict">
      <xsl:template match="/">
         <html:html>
            <html:head><html:title>Title</html:title></html:head>
            <html:body>
               <xsl:apply-templates/>
            </html:body>
      <xsl:template>
      ...
      </xsl:stylesheet>
    than this:

      <aaa:stylesheet version="1.0"
                      xmlns:aaa="http://www.w3.org/1999/XSL/Transform"
                      xmlns:bbb="http://www.w3.org/TR/xhtml1/strict">
      <aaa:template match="/">
         <bbb:html>
            <bbb:head><bbb:title>Title</bbb:title></bbb:head>
            <bbb:body>
               <aaa:apply-templates/>
            </bbb:body>
      <aaa:template>
      ...
      </aaa:stylesheet>
    or (much worse yet) this:

      <html:stylesheet version="1.0"
                      xmlns:html="http://www.w3.org/1999/XSL/Transform"
                      xmlns:xsl="http://www.w3.org/TR/xhtml1/strict">
      <html:template match="/">
         <xsl:html>
            <xsl:head><xsl:title>Title</xsl:title></xsl:head>
            <xsl:body>
               <html:apply-templates/>
            </xsl:body>
      <html:template>
      ...
      </html:stylesheet>
    In spite of this, people are likely to read significance into prefixes, so it is a good idea to use prefixes appropriate to their XML namespace, such as xslt for the XSLT namespace.

    Finally, although prefixes are not significant in element and attribute names, they may be significant when used elsewhere in an XML document, such as in a DTD (see question 7.6) or an attribute value. Because such usage is common -- for example, XML Schemas uses qualified names in attribute values -- it is tempting to think that it is defined by the XML namespaces recommendation. This is not the case: the XML namespaces recommendation only defines the use of prefixes in element and attribute names. The use of prefixes elsewhere in an XML document is defined outside the XML namespaces recommendation and is entirely the responsibility of the defining application. For more information, see question 10.4.

    11.4) Can I use the same prefix for more than one XML namespace?
    Yes.

    For example, in the following, B is in the http://www.foo.org/ namespace and C is in the http://www.bar.org/ namespace:

      <A>
         <foo:B xmlns:foo="http://www.foo.org/">abc</foo:B>
         <foo:C xmlns:foo="http://www.bar.org/">abc</foo:C>
      </A>
    Remember, however, that the prefix in an XML namespace declaration overrides the same prefix if that prefix is declared on an ancestor. For example, in the following, C is in the http://www.bar.org/ namespace, not the http://www.foo.org/ namespace:

      <A>
         <foo:B xmlns:foo="http://www.foo.org/">
            <foo:C xmlns:foo="http://www.bar.org/">abc</foo:C>
         </foo:B>
      </A>
    In general, it is a good idea to use a prefix for only one XML namespace, as this reduces confusion when reading the document. In spite of this, there is a very real possibility that the same prefix will be used for multiple XML namespaces when combining fragments from different documents. For more information, see question 4.9.

    11.5) Can I use more than one prefix for the same XML namespace?
    Yes.

    For example, in the following, both B and C are in the http://www.foo.org/ namespace:

      <A xmlns:foo="http://www.foo.org/"
         xmlns:bar="http://www.foo.org/">
         <foo:B>abc</foo:B>
         <bar:C>abc</bar:C>
      </A>
    In general, it is a good idea to use only one prefix for an XML namespace, as this reduces confusion when reading the document.

    11.6) How are prefixes declared?
    Prefixes are declared in an XML namespace declaration (xmlns attribute). For more information, see question 4.1.

    11.7) Can I undeclare a prefix -- that is, dissociate a prefix from an XML namespace?
    No.

    You can override a prefix (see question 4.5) or the XML namespace declaration that declares the prefix can go out of scope (see question 6.4), but you can't undeclare the prefix. (Note that you can undeclare the default XML namespace; for more information, see question 4.8.)

    11.8) What happens if I use a prefix that is not declared?
    This results in a namespace error. For example, the following document does not conform to the XML namespaces recommendation.

      <?xml version="1.0" ?>
      <foo:A />
    However, the XML namespaces recommendation does not specify what a namespace-aware application does when it encounters a non-conformant document, so the behavior is application-dependent. For example, the application could stop processing, post an error to a log and continue processing, or ignore the error.

    11.9) What happens if there is no prefix on an element type name?
    If a default XML namespace declaration is in scope, then the element type name is in the default XML namespace. Otherwise, the element type name is not in any XML namespace. For example, in the following, B and C are in the http://www.foo.org/ namespace and A is not in any XML namespace:

      <A>
         <B xmlns="http://www.foo.org/">
            <C>abc</C>
         </B>
      </A>
    11.10) What happens if there is no prefix on an attribute name?
    The attribute name is not in any XML namespace. That is, the default XML namespace does not apply to unprefixed attribute names.

    For more information, see myth #4 in "Namespace Myths Exploded".

    12) SECTION 12: XML NAMESPACE NAMES (URIs)
    12.1) What is an XML namespace URI?
    Technically, the term namespace URI is never defined in the XML namespaces recommendation. In practice, this is the term commonly used to denote the URI used as an XML namespace name. That is, it is a synonym for the term namespace name. Because of the widespread use of this term, we tend to use the term "namespace name (URI)" in this FAQ.

    Note: Version 1.1 of the XML namespaces recommendation uses IRIs (Internationalized Resource Identifiers) instead of URIs. However, because version 1.1 is not yet a full recommendation [February, 2003] and because the IRI RFC is not yet complete, this document continues to refer to URIs instead of IRIs.

    12.2) What is an XML namespace name?
    An XML namespace name is a URI that uniquely identifies the namespace. URIs are used because they are widely understood and well documented. Because people may only allocate URIs under their control, it is easy to ensure that no two XML namespaces are identified by the same URI.

    12.3) What does the URI used as an XML namespace name point to?
    The URI used as an XML namespace name is simply an identifier. It is not guaranteed to point to anything and, in general, it is a bad idea to assume that it does. This point causes a lot of confusion, so we'll repeat it here:

       URIs USED AS XML NAMESPACE NAMES ARE JUST
       IDENTIFIERS. THEY ARE NOT GUARANTEED TO POINT
       TO ANYTHING.

    While this might be confusing when URLs are used as namespace names, it is obvious when other types of URIs are used as namespace names. For example, the following namespace declaration uses an ISBN URN:

       xmlns:xbe="urn:ISBN:0-7897-2504-5"
    and the following namespace declaration uses a UUID URN:

       xmlns:foo="urn:uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
    Clearly, neither namespace name points to anything on the Web.

    NOTE: Namespace URIs that are URLs may point to RDDL documents, although this does not appear to be widely implemented. For details, see the next question.

    NOTE: An early version of the W3C's XML Schemas used namespace URIs to point to an XML Schema document containing the definitions of the element types and attributes named in the namespace. However, this proved very controversial and the idea has been withdrawn.

    12.4) Can I resolve the URI used as an XML namespace name?
    Yes.

    You can also eat a tractor, but that doesn't mean it's a good idea. The URIs used as XML namespace names are not guaranteed to point to anything, so there is generally no reason to resolve them. Furthermore, there is nothing in the processing of XML namespaces that requires you to resolve these URIs. Finally, as was noted in the previous question, many types of namespace URIs are unresolvable on the Web.

    That said, some people have advocated placing RDDL (Resource Directory Description Language, pronounced "riddle") documents at the locations pointed to by namespace URIs. A RDDL document "provides a text description of some class of resources and of individual resources related to that class", including such things as a human-readable description of what the namespace describes and links to resources such as schemas (in a variety of languages), stylesheets, and code. RDDL documents apparently are being used, although it is not clear how widely.

    For more information about RDDL and the ideas behind it, see:

    Resource Directory Description Language

    RDDL Me This: What Does a Namespace URL Locate?

    Architectural Theses on Namespaces and Namespace Documents

    12.5) Can I use a relative URI as a namespace name?
    Yes. However, such usage is deprecated, so you should never do it.

    The original XML namespaces recommendation does not explicitly disallow the use of relative URIs as namespace names. Instead, it states that two namespace names are identical if their URIs match character for character. It then notes that non-identical URIs might be functionally equivalent, such as when they differ only in case. Combined with the URI specification (RFC2396), which states that relative URIs take on the base URI of their containing document, this provided a loophole with which to use relative URIs.

    When this was discovered more than a year after the recommendation was released it proved hugely controversial. For example, some people wanted to use relative URIs as namespace names so that elements and attributes would have different XML namespace names (URIs) depending on what document they were in. The objection to this was that it would undermine the purpose of XML namespaces. That is, XML namespaces would no longer provide universally unique names.

    After more than two months of discussion, it was decided that relative URIs are not expanded when used as namespace names and that their use was deprecated. That is, suppose that the following document has a URI of "http://www.foo.org/xml/relativeExample.xml".

      <A xmlns:foo="../relativeURI">
         <foo:B>abc</foo:B>
         <bar:C>abc</bar:C>
      </A>
    Although the URI "../relativeURI" does expand to the URI "http://www.foo.org/relativeURI", the name of the XML namespace associated with the foo prefix is "../relativeURI", not the expanded URI.

    It was also decided that:

    The infoset recommendation does not define an infoset for documents that use relative URIs as XML namespace names.

    The value of the namespaceURI attribute of the Node interface in the DOM is undefined if the URI is relative.

    The return value of the namespace-uri() function in XPath is undefined if the URI is relative.

    All new W3C recommendations should include a statement saying that they do not apply to XML documents that use relative URIs as XML namespace names.

    For the archives of the mailing list discussing the use of relative URIs as namespace names, see http://lists.w3.org/Archives/Public/xml-uri/. For the results of the W3C XML Plenary Ballot on the use of relative URIs in XML namespace names, see http://www.w3.org/2000/09/xppa.

    PART IV: MISCELLANEOUS
    13) SECTION 13: POLITICS AND REVOLUTIONS
    13.1) Why are XML namespaces so hard to understand and use?
    Actually, they aren't. When trying to understand XML namespaces, all you need to do is remember one thing:

    XML namespaces provide a two-part naming system for element types and attributes.
    That's it. They don't do anything else and what little complexity there is comes from the flexibility of declaration and scoping rules. You can minimize this complexity by doing the following:

    Declare all XML namespaces on the root element.
    Use one prefix per XML namespace.
    Processing XML namespaces isn't that hard either. In a SAX 1.0 application, you can use John Cowan's Namespace SAX Filter to return universal names; SAX 2.0 does this for you. In a DOM level 1 application, you can usually ask the DOM implementation for namespace information; DOM level 2 always provides this information. If you do your own processing, all you need to remember is to keep the prefix-to-URI mappings as part of your state, scan the attributes for xmlns attributes before doing anything else, and scan element type and attribute names for prefixes.

    13.2) Are there any alternatives to XML namespaces?
    Yes. You could resolve name clashes by simply renaming the offending element types or attributes and then, during processing, using architectural forms to transform them to names that are recognizeable by different modules in the application. Although you may have to rename some element types or attributes, you won't have to change your application modules. A discussion of architectural forms is beyond the scope of this paper. For more information, see Architectural Forms and SGML/XML Architectures on Robin Cover's XML Cover Pages.

    As to the actual syntax used by XML namespaces, the idea of declaring XML namespace prefixes in a processing instruction at the start of an XML document is occasionally raised. Processing instructions were used in an early draft of the XML namespaces specification and have the advantage of simplicity (see question 4.9). They were later replaced with the more flexible xmlns attributes. Although you might be tempted to strike out on your own and use processing instructions or some other strategy, this is probably a bad idea, although not necessarily for technical reasons. The real problem is that XML namespaces are already widely used and supported, so any XML documents you produce will be non-standard and non-portable.

    13.3) How controversial are XML namespaces?
    The need for XML namespaces and the basic idea that a two-part naming system (or something similar) is needed is not controversial. However, the design of XML namespaces -- that is, the way XML namespaces are declared and used in an XML document, as well the confusion discussed in "Namespace Myths Exploded" -- has, at times, been very controversial. (If you want to see just how controversial, go to the archives of the XML-DEV mailing list and search on the word "namespace".) Although XML namespaces still have some very vocal detractors, most people have accepted and are using them. Furthermore, most new XML tools and technologies use them, a state of affairs that is only likely to increase.

    14) SECTION 14: XML NAMESPACE RESOURCES
    14.1) What resources are available for learning about XML namespaces?
    Recommendations and other official stuff:

    Namespaces in XML (W3C Recommendation): http://www.w3.org/TR/REC-xml-names

    Namespaces in XML 1.1 (W3C Candidate Recommendation): http://www.w3.org/TR/xml-names11/

    Namespaces in XML (auf Deutsch): http://www.schumacher-netz.de/TR/1999/REC-xml-names-19990114-de.html

    Results of W3C XML Plenary Ballot on Relative URI References in Namespace Declarations: http://www.w3.org/2000/09/xppa

    Articles about XML namespaces:

    Namespace Myths Exploded, by Ronald Bourret: http://www.xml.com/pub/a/2000/03/08/namespaces/index.html

    XML Namespaces by Example, by Tim Bray: http://www.xml.com/pub/1999/01/namespaces.html

    XML Namespaces, by James Clark: http://www.jclark.com/xml/xmlns.htm

    Plan to use XML Namespaces, Parts 1 and 2, by David Marston: http://www-106.ibm.com/developerworks/xml/library/x-nmspace.html
    http://www-106.ibm.com/developerworks/library/x-nmspace2.html

    19 Short Questions about Namespaces (with Answers), by David Megginson: http://www.megginson.com/docs/namespaces/namespace-questions.html

    Namespace Nuances, by John E. Simpson: http://www.xml.com/pub/a/2001/07/05/namespaces.html

    Computers : Data Formats : Markup Languages : XML : Namespaces, by the Open Directory Project: http://dir.google.com/Top/Computers/Data_Formats/Markup_Languages/XML/Namespaces/
    A list of articles (including some of those above) about XML namespaces.

    More links are welcome.

    15) SECTION 15: COMMENTS, COMPLAINTS, AND SUGGESTIONS
    If you have comments, complaints, or suggestions for new questions, you can mail them to me (Ronald Bourret) at:

       rpbourret@rpbourret.com

    Please note that I travel frequently, so it might be two to three weeks before I reply.

    Thanks also to Adrian Boyko, Tim Bray, Derek Denny-Brown, Bob DuCharme, Sam Hunting, Madhav Lakkapragada, Andrew Layman, Gustaf Liljegren, Christopher Lott, Marc McDonald, Anders M鴏ler, Jim Palmer, Ganesan Radhakrishnan, Arjun Ray, Aron Roberts, and Richard Tobin for their input.

    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/2/25 11:29:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XML基础 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/21 4:00:41

    本主题贴数3,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    311.523ms