以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 XML基础 』 (http://bbs.xml.org.cn/list.asp?boardid=1) ---- [转载]XML Namespaces FAQ (http://bbs.xml.org.cn/dispbbs.asp?boardid=1&rootid=&id=5454) |
-- 作者:admin -- 发布时间:2/25/2004 11:21:00 AM -- [转载]XML Namespaces FAQ http://www.rpbourret.com/xml/NamespacesFAQ.htm XML Namespaces FAQ Copyright 2000-2004 by Ronald Bourret Table of Contents SECTION 1: EXECUTIVE SUMMARY SECTION 2: TRADITIONAL NAMESPACES SECTION 3: XML NAMESPACES PART II: DECLARING AND USING XML NAMESPACES SECTION 4: DECLARING XML NAMESPACES IN AN XML DOCUMENT SECTION 5: USING XML NAMESPACES IN AN XML DOCUMENT SECTION 6: SCOPE OF XML NAMESPACE DECLARATIONS SECTION 7: XML NAMESPACES AND DTDs SECTION 8: USING DOCUMENTS THAT USE XML NAMESPACES SECTION 9: PROCESSING XML NAMESPACES IN XML APPLICATIONS PART III: NAMES, PREFIXES, AND URIs SECTION 10: NAMES SECTION 11: XML NAMESPACE PREFIXES SECTION 12: XML NAMESPACE NAMES (URIs) PART IV: MISCELLANEOUS SECTION 13: POLITICS AND REVOLUTIONS SECTION 14: XML NAMESPACE RESOURCES SECTION 15: COMMENTS, COMPLAINTS, AND SUGGESTIONS 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 and B are in the http://www.foo.org/ namespace, which is associated with the foo prefix. --> <!-- This is equivalent to the previous example but uses a default namespace instead of the foo prefix. --> 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 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? For example, in the following XML document, there is no conflict between the three different uses of the name Value. <AuctionItem> 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? (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 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" ?> <?xml version="1.0" ?> 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? 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> 3.3) Does the XML namespaces recommendation define anything except a two-part naming system for element types and attributes? This is a very important point and a source of much confusion, so we will repeat it: THE XML NAMESPACES RECOMMENDATION DOES NOT 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? <foo:A xmlns:foo="http://www.foo.org/"> 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? 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/"> 3.6) Do XML namespaces apply to entity names, notation names, or processing instruction targets? 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? (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? 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" <foo:A xmlns:foo="http://www.foo.org/"> 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? 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编辑过]
|
-- 作者:admin -- 发布时间:2/25/2004 11:29:00 AM -- 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 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 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? 4.3) Can I use an attribute default in a DTD to declare an XML namespace? 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" ?> <?xml version="1.0" ?> 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" ?> 4.5) How do I override an XML namespace declaration that uses a prefix? <foo:A xmlns:foo="http://www.foo.org/"> 4.6) How do I override a default XML namespace declaration? <A xmlns="http://www.foo.org/"> 4.7) How do I undeclare an XML namespace prefix? <foo:A xmlns:foo="http://www.foo.org/"> 4.8) How do I undeclare the default XML namespace? <A xmlns="http://www.foo.org/"> 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/"> 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? 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 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. --> <!-- xslt refers to the http://www.w3.org/1999/XSL/Transform namespace. --> 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. --> 5.3) How do I use the default XML namespace to refer to attribute names in an XML namespace? 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. --> 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? <!-- A, B, C, and G are in the http://www.foo.org/ namespace. --> <A xmlns="http://www.foo.org/"> 6) SECTION 6: SCOPE OF XML NAMESPACE DECLARATIONS 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/"> 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/"> <A xmlns="http://www.foo.org/"> <foo:A xmlns:foo="http://www.foo.org/"> 6.3) If an element or attribute is in the scope of an XML namespace declaration, is its name in that namespace? 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/"> <!-- B is in the http://www.foo.org/ namespace; C is not in any XML namespace. --> <!-- A and C are in the http://www.foo.org/ namespace. B is in the http://www.bar.org/ namespace. --> <?xml version="1.0" ?> <?xml version="1.0" ?> <A xmlns:foo="http://www.foo.org/" 6.7) How can I declare XML namespaces so that all elements and attributes are in their scope? <Department 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 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? 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? For example, the following is legal: <!ELEMENT foo:A (foo:B)> 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? 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)> 7.5) Can the attribute list of an element type contain attributes whose names come from other XML namespaces? For example, the following is legal: <!ATTLIST foo:A 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" ?> <?xml version="1.0" ?> <?xml version="1.0" ?> <?xml version="1.0" ?> Declare xmlns attributes in the DTD. <?xml version="1.0" ?> <?xml version="1.0" ?> Declare all xmlns attributes in the DTD. 7.7) How can I validate an XML document that uses XML namespaces? 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? 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.2) How can I check that a document conforms to the XML namespaces recommendation? 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? 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? 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? 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> <Server> <Departments> 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 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 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? 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)> <!ELEMENT foo:A (#PCDATA)> 9) SECTION 9: PROCESSING XML NAMESPACES IN XML APPLICATIONS 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? URI^local-name http://www.tu-darmstadt.de/ito/sales^SalesOrder public void startElement(String elementName, AttributeList attrs) public void startElement(String elementName, AttributeList attrs) public void startElement(String elementName, AttributeList attrs) if (getURI(elementName).equals("http://www.tu-darmstadt.de/ito/sales")) 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? 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) public void startElement(String namespaceURI, 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? // Check the local name. if (elementNode.getNodeName().equals("SalesOrder")) // Check the XML namespace name (URI). if ((NSElement)elementNode).getNamespace().equals("http://www.tu-darmstadt.de/ito/sales")) // Check the local name. if ((NSElement)elementNode.getLocalName().equals("SalesOrder")) 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? // Check the local name. if (elementNode.getNodeName().equals("SalesOrder")) // Check the XML namespace name (URI). if (elementNode.getNamespaceURI().equals("http://www.tu-darmstadt.de/ito/sales")) // Check the local name. if (elementNode.getLocalName().equals("SalesOrder")) 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? 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? 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?
|
-- 作者:admin -- 发布时间:2/25/2004 11:29:00 AM -- 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 serv:Address 10.2) What characters are allowed in a qualified name? 10.3) Where can qualified names appear? <!DOCTYPE foo:A [ 10.4) Can qualified names be used in attribute values? <foo:A xmlns:foo="http://www.foo.org/"> <xsd:attribute name="foo" type="xsd:string" /> 10.5) How are qualified names mapped to names in XML namespaces? <?xml version="1.0" ?> <?xml version="1.0" ?> <?xml version="1.0" ?> 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? <serv:Address xmlns:serv="http://www.tu-darmstadt.de/ito/servers">123.45.67.8</serv:Address> 10.12) How are universal names represented? 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 {http://www.tu-darmstadt.de/ito/servers}Address 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 <serv:Addresses xmlns:serv="http://www.tu-darmstadt.de/ito/addresses"> 11.3) Are prefixes significant? For example, the following are equivalent: <serv:Addresses xmlns:serv="http://www.tu-darmstadt.de/ito/addresses"> <foo:Addresses xmlns:foo="http://www.tu-darmstadt.de/ito/addresses"> <xsl:stylesheet version="1.0" <aaa:stylesheet version="1.0" <html:stylesheet version="1.0" 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? 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> <A> 11.5) Can I use more than one prefix for the same XML namespace? For example, in the following, both B and C are in the http://www.foo.org/ namespace: <A xmlns:foo="http://www.foo.org/" 11.6) How are prefixes declared? 11.7) Can I undeclare a prefix -- that is, dissociate a prefix from an XML namespace? 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? <?xml version="1.0" ?> 11.9) What happens if there is no prefix on an element type name? <A> For more information, see myth #4 in "Namespace Myths Exploded". 12) SECTION 12: XML NAMESPACE NAMES (URIs) 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? 12.3) What does the URI used as an XML namespace name point to? URIs USED AS XML NAMESPACE NAMES ARE JUST 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" xmlns:foo="urn:uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 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? 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? 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"> 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 XML namespaces provide a two-part naming system for element types and attributes. Declare all XML namespaces on the root element. 13.2) Are there any alternatives to XML namespaces? 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? 14) SECTION 14: XML NAMESPACE RESOURCES 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 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/ More links are welcome. 15) SECTION 15: COMMENTS, COMPLAINTS, AND SUGGESTIONS 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. |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
296.875ms |