Xmlns: XML namespace

Source: Internet
Author: User
Tags xml parser xsl
The following is reproduced from:
Http://hi.baidu.com/tianhesj/blog/item/0dd9718d32be4011b21bbacc.html

Translate namespaces into namespaces. What is the role of a namespace? When we use another person's or multiple DTD files in an XML document, the conflict arises: Because the identifiers in XML are created by ourselves, in different DTD files, the identifiers may have the same name but different meanings, which may cause data confusion.
For example, in a document <Table> wood table </table>, <Table> indicates a table,
In another document <Table> namelist </table>, <Table> indicates a table. If I need to process these two documents at the same time, a name conflict will occur.
To solve this problem, we introduced the namespaces concept. Namespaces identifies the same names by adding a URL.
Namespaces must also be declared at the beginning of the XML document. The declared syntax is as follows:
<Document xmlns: yourname = 'url'>
Yourname is the namespaces name defined by you, and URL is the URL of the namespace.
Assuming that the above "Table <Table>" document comes from the http://www.zhuozi.com, we can declare
<Document xmlns: zhuozi = 'HTTP: // www.zhuozi.com '>;
Then use the defined namespace in the following identifiers:
<Zhuozi: Table> wood table </table>
In this way, the two <Table> are distinguished. Note: setting a URL does not mean that the ID is to be read from that URL. It is just a different sign.

Name Conflict
Because the elements used in the XML document are not fixed, two different XML documents may use the same name to describe different types of elements. In this case, naming conflicts often occur. See the following two examples.

This XML document contains fruit information in the table element:

  1. <Table>
  2. <Tr>
  3. <TD> apples </TD>
  4. <TD> bananas </TD>
  5. </Tr>
  6. </Table>

This XML document contains the table information (furniture, which cannot be eaten) in the table element ):

  1. <Table>
  2. <Name> African coffee table </Name>
  3. <Width> 80 </width>
  4. <Long> 120 </length>
  5. </Table>

If the preceding two XML document fragments happen to be used together, a name conflict may occur. Because both fragments contain <Table> elements, the definitions of these two table elements are different from those of the contained content.

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

Use a prefix to solve naming conflicts
The following XML document contains information in the table element:

  1. <H: Table>
  2. <H: tr>
  3. <H: TD> apples
  4. <H: TD> bananas
  5. </H: tr>
  6. </H: Table>

The following XML document carries the furniture table information:

  1. <F: Table>
  2. <F: Name> African coffee table </F: Name>
  3. <F: width> 80 </F: width>
  4. <F: length> 120 </F: length>
  5. </F: Table>

Now there is no element naming conflict problem, because the two documents use different prefixes for their respective table elements. The table elements in the two documents are respectively (

By using the prefix, we create two different table elements.

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

Use namespace
The following XML document contains information in the table element:

  1. <H: Table xmlns: H = "http://www.w3.org/TR/html4/">
  2. <H: tr>
  3. <H: TD> apples
  4. <H: TD> bananas
  5. </H: tr>
  6. </H: Table>

The following XML document carries the furniture table information:

  1. <F: Table xmlns: F = "http://www.w3schools.com/furniture">
  2. <F: Name> African coffee table </F: Name>
  3. <F: width> 80 </F: width>
  4. <F: length> 120 </F: length>
  5. </F: Table>

In the preceding two examples, besides the prefix, both table elements use the xmlns attribute to associate the elements with different namespaces.

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

Namespace attributes
The namespace attributes are generally placed at the start of an element. The syntax is as follows:

Xmlns: namespace-Prefix = "namespace"

In the preceding example, The namespace defines an Internet address:

Xmlns: F = "http://www.w3schools.com/furniture"

The W3C naming convention declares that the namespace itself is a unified resource identifier, Uniform Resource Identifier (URI ).

When a namespace is used at the beginning of an element, all the child elements of the element are associated with the same namespace through a prefix.

Note: The network address used to identify the namespace is not called by the XML parser. The XML parser does not need to find information from this network address, the network address only serves to give the namespace a unique name, so the network address can also be virtual, however, many companies often regard this network address as a real web page, which contains more detailed information about the current namespace.
You can access the http://www.w3.org/TR/html4.

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

Uniform Resource Identifier
A uniform resource identifier (URI) is a string that identifies network resources. The most common URI should be the Uniform Resource Locator uniform resource locator (URL ). The URL is used to identify the address of the network host. On the other hand, another uncommon URI is the Universal Resource Name (URN ). In our example, URLs is generally used.

Since the previous example uses the URL address to identify the namespace, we can be sure that the namespace is unique.

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

Default namespace
Define a default XML namespace so that we do not need to use the prefix in the start tag of the child element. Its syntax is as follows:

<Element xmlns = "namespace">

The following XML document contains fruit information in the table element:

  1. <Table xmlns = "http://www.w3.org/TR/html4/">
  2. <Tr>
  3. <TD> apples </TD>
  4. <TD> bananas </TD>
  5. </Tr>
  6. </Table>

The following XML document contains the furniture table information:

  1. <Table xmlns = "http://www.w3schools.com/furniture">
  2. <Name> African coffee table </Name>
  3. <Width> 80 </width>
  4. <Long> 120 </length>
  5. </Table>

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

Use namespace
When the file starts to use XSL, it will find that the namespace is used so frequently. The XSL style sheet is mainly used to convert an XML document into a format similar to an HTML file.

If you look at the XSL document under idea, you will find that many tags are HTML tags. The markup is not an HTML markup, but an XSL with a prefix, which is identified by the namespace "http://www.w3.org/TR/xsl:

  1. <? XML version = "1.0" encoding = "ISO-8859-1"?>
  2. <XSL: stylesheet xmlns: XSL = "http://www.w3.org/TR/xsl">
  3. <XSL: template match = "/">
  4. <HTML>
  5. <Body>
  6. <Table border = "2" bgcolor = "yellow">
  7. <Tr>
  8. <TH> title </Th>
  9. <TH> artist </Th>
  10. </Tr>
  11. <XSL: For-each select = "catalog/CD">
  12. <Tr>
  13. <TD> <XSL: value-of select = "title"/> </TD>
  14. <TD> <XSL: value-of select = "artist"/> </TD>
  15. </Tr>
  16. </XSL: For-each>
  17. </Table>
  18. </Body>
  19. </Html>
  20. </XSL: Template>
  21. </XSL: stylesheet>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.