ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: XML :: XML ÃüÃû¿Õ¼ä

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

XML
XML DHTMLÐÐΪ
XML Ïà¹Ø¼¼Êõ
XML ±à¼­Æ÷
XML ÕªÒª
XML ʵÀý
XML×Ö·û±àÂë
xml ÎĵµÊ÷
IEºÍ»ðºü¶ÁÈ¡XML·..

XML ÃüÃû¿Õ¼ä


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2007-08-10 ä¯ÀÀ:5293 :: ::

XML Namespaces provide a method to avoid element name conflicts.
XML ÃüÃû¿Õ¼äÌṩÁ˱ÜÃâÔªËØÃû³Æ³åÍ»µÄ·½·¨¡£


Name Conflicts
Ãû³Æ³åÍ»

Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names.
ÒòΪXMLµÄÔªËØÃû³Æ²¢²»ÊÇÔ¤Ïȶ¨ÒåµÄ£¬Òò´Ë£¬Èç¹ûÁ½·Ý²»Í¬ÎĵµµÄÔªËØÃû³ÆÏàͬ£¬¾Í»á²úÉúÃû³Æ³åÍ»¡£

This XML document carries information in a table:
Õâ·ÝXMLÎĵµÐ¯´øÁ˱í¸ñÖеÄÐÅÏ¢£º

<table>
<tr>
<td>Apples</td>

<td>Bananas</td>
</tr>
</table>

This XML document carries information about a table (a piece of furniture):
Õâ·ÝXMLÎĵµÐ¯´øÁËÒ»ÕÅ×À×ÓµÄÐÅÏ¢£¨Ò»¼þ¼Ò¾ß£©£º

<table>
<name>African Coffee Table</name>

<width>80</width>
<length>120</length>
</table>

If these two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition.
Èç¹û°ÑÁ½·ÝXMLÎĵµÆ´½ÓÆðÀ´£¬¾Í»á³öÏÖÔªËØÃû³Æ³åÍ»£¬ÒòΪÁ½·ÝÎļþ¶¼°üº¬Ò»¸ö<table>ÔªËØ£¬µ«ÆäËùÖ¸¶¨µÄÄÚÈݺͶ¨ÒåÊDz»Í¬µÄ¡£


Solving Name Conflicts Using a Prefix
ÓÃǰ׺·¨½â¾öÃû³Æ³åÍ»¶ÜÎÊÌâ

This XML document carries information in a table:
Õâ·ÝXMLÎĵµÐ¯´øÁ˱í¸ñÖеÄÐÅÏ¢£º

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>

</h:tr>
</h:table>

This XML document carries information about a piece of furniture:
Õâ·ÝXMLÎĵµÖÐЯ´øÁËÒ»¼þ¼Ò¾ßµÄÐÅÏ¢£º

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>

<f:length>120</f:length>
</f:table>

Now there will be no name conflict because the two documents use a different name for their <table> element (<h:table> and <f:table>).
ÄÇô£¬ÏÖÔھͲ»»áÓÐÃû³Æ³åÍ»µÄÎÊÌâÁË£¬ÒòΪÁ½·ÖÎĵµµÄ<table>ÔªËØÊ¹ÓÃÁ˲»Í¬µÄÃû³Æ£¨<h:table> ºÍ<f:table>£©¡£

By using a prefix, we have created two different types of <table> elements.
ͨ¹ýʹÓÃǰ׺·¨£¬ÎÒÃÇ´´½¨ÁËÁ½ÖÖ²»Í¬ÀàÐ͵Ä<table>ÔªËØ¡£


Using Namespaces
ʹÓÃÃüÃû¿Õ¼ä

This XML document carries information in a table:
Õâ·ÝXMLÎĵµÐ¯´øÁ˱í¸ñÖеÄÐÅÏ¢£º

<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>

<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

This XML document carries information about a piece of furniture:
Õâ·ÝXMLÎĵµÖÐЯ´øÁËÒ»¼þ¼Ò¾ßµÄÐÅÏ¢£º

<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>

</f:table>

Instead of using only prefixes, we have added an xmlns attribute to the <table> tag to give the prefix a qualified name associated with a namespace.
ÔÚÕâÀÎÒÃDz¢Ã»ÓÐʹÓÃǰ׺£¬È¡¶ø´úÖ®µÄÊÇ£¬ÎÒÃǸø<table>±êÇ©Ìí¼ÓÁËÒ»¸öxmlnsÊôÐÔ£¬Ê¹Ç°×º°üº¬ÁËÒ»¸öÓëÃüÃû¿Õ¼äÏàÆ¥ÅäµÄºÏ·¨Ãû³Æ¡£


The XML Namespace (xmlns) Attribute
XMLÃû³Æ¿Õ¼äµÄ£¨xmlns£©ÊôÐÔ

The XML namespace attribute is placed in the start tag of an element and has the following syntax:
½«XMLÃüÃû¿Õ¼äÊôÐÔ·ÅÖÃÔÚÒ»¸öÔªËØµÄÆðʼ±êÇ©ÖУ¬Óï·¨ÈçÏ£º

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
ÔÚÔªËØµÄÆðʼ±êÇ©Öж¨ÒåÒ»¸öÃüÃû¿Õ¼äʱ£¬ËùÓаüº¬Ïàͬǰ׺µÄ×ÓÔªËØ¶¼ÓëÏàͬµÄÃû³Æ¿Õ¼äÏàÆ¥Å䣻

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.
ÖµµÃ×¢ÒâµÄÊÇ£º½âÎöÆ÷²¢²»»áʹÓÃÕâЩÓÃÓÚ¶¨ÒåÃüÃû¿Õ¼äµÄµØÖ·À´ËÑË÷ÐÅÏ¢¡£ÕâÑù×öµÄÄ¿µÄ½öÊÇΪÃüÃû¿Õ¼äÌṩһ¸ö¶ÀÁ¢µÄÃû³Æ¡£È»¶ø£¬ÔÚ´ó¶àÊýÇé¿öÏ£¬¹«Ë¾»á½«ÃüÃû¿Õ¼ä×÷Ϊһ¸öָʾÆ÷£¬Ö¸Ïò°üº¬ÃüÃû¿Õ¼ä¾ßÌåÐÅÏ¢µÄÕæÊµÍøÒ³¡£
×Ô¼º¿´¿´°É£º http://www.w3.org/TR/html4/.


Uniform Resource Identifier (URI)
ͳһ×ÊԴʶ±ðÆ÷£¨URI£©

A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN). In our examples we will only use URLs.
URIÊÇʶ±ðÍøÂç×ÊÔ´µÄÒ»×é×Ö·û´®¡£×îÆÕͨµÄURIÊÇͳһ×ÊÔ´¶¨Î»Æ÷£¨URL£©£¬ËüÓÃÓÚ¶Ô»¥ÁªÍøµÄÓòÃûµØÖ·½øÐж¨Î»£»ÁíÒ»ÖÖ²»Ì«ÆÕ±éµÄURIÊÇͨÓÃ×ÊÔ´Ãû³Æ (URN)¡£ÔÚÎÒÃǵݸÀýÖУ¬ÎÒÃÇֻʹÓÃURL¡£


Default Namespaces
ĬÈϵÄÃüÃû¿Õ¼ä

Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:
ÎªÔªËØ¶¨ÒåÒ»¸öĬÈϵÄÃüÃû¿Õ¼ä£¬ÎÒÃǾͲ»ÐèҪΪÿ¸ö×ÓÔªËØ¶¨Òåǰ׺¡£Óï·¨ÈçÏ£º

xmlns="namespaceURI"

This XML document carries information in a table:
Õâ·ÝXMLÎĵµÐ¯´øÁ˱í¸ñÖеÄÐÅÏ¢£º

<table xmlns="http://www.w3.org/TR/html4/">

<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML document carries information about a piece of furniture:
Õâ·ÝXMLÎĵµÖÐЯ´øÁËÒ»¼þ¼Ò¾ßµÄÐÅÏ¢£º

<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>

<length>120</length>
</table>

 


Namespaces in Real Use
ÃüÃû¿Õ¼äµÄʵ¼ÊÓ¦ÓÃ

When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to transform XML documents into other formats, like HTML.
µ±Ä㿪ʼʹÓÃXSLʱ£¬Äã»áºÜ¿ìµØ·¢ÏÖÃüÃû¿Õ¼äÔÚÏÖʵÖеÄʹÓ÷½·¨¡£XSLʽÑù±íÓÃÓÚ½«XMLÎĵµ×ª»»³ÉÆäËü¸ñʽ£¬È磺HTML¸ñʽ¡£

If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified by the namespace "http://www.w3.org/1999/XSL/Transform":
Èç¹ûÄã×Ðϸ¿´¿´ÏÂÃæµÄÀý×Ó£¬Äã»á·¢ÏÖ£¬´ó²¿·Ö±êÇ©¶¼ÊÇHTMLÐ͵ıêÇ©¡£·ÇHTMLµÄ±êÇ©°üº¬ÁË Ç°×ºÃû“xsl” £¬Ëü¿ÉÒÔ±»ÃüÃû¿Õ¼ä"http://www.w3.org/1999/XSL/Transform"Ëùʶ±ð£º

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">

<tr>
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>

<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>

</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ÆÀÂÛ (0) All