w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD <anyAttribute> ÔªËØ
XSD <anyAttribute> ÔªËØ
::
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!
<anyAttribute> ÔªËØÄܹ»Ê¹ÎÒÃÇÔÚXMLÎĵµÖÐÌí¼Óδ±»schemaÖ¸¶¨¹ýµÄÊôÐÔ£¡
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema.
<anyAttribute> ÔªËØÄܹ»Ê¹ÎÒÃÇÔÚXMLÎĵµÖÐÌí¼Óδ±»schemaÖ¸¶¨¹ýµÄÊôÐÔ¡£
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <anyAttribute> element we can add any number of attributes to the "person" element:
ÏÂÃæÁоÙÁËÃûΪ "family.xsd" µÄ XML schema Ƭ¶Î¡£ËüչʾÁË"person"ÔªËØµÄÉùÃ÷¡£Í¨¹ýʹÓÃ<anyAttribute>ÔªËØ£¬ÎÒÃÇ¿ÉÒÔ¸ø"person"ÔªËØÌí¼ÓÈÎÒâÊýÁ¿µÄÊôÐÔ¡£
<xs:element name="person"> |
Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute.
ÏÖÔÚ£¬ÎÒÃÇÏ£ÍûÔÚ"person"ÔªËØÖÐÌí¼Ó"gender"ÊôÐÔ¡£ËäÈ»ÕâÆªschemaÎļþµÄ×÷Õß²¢Î´ÉùÃ÷"gender"ÊôÐÔ£¬µ«ÊÇ£¬ÎÒÃÇÒ»Ñù¿ÉÒÔ×öµ½¡£
Look at this schema file, called "attribute.xsd":
Çë¿´ÏÂÃæÕâ·ÝÃûΪ"attribute.xsd"µÄschemaÎļþ£º
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:attribute name="gender"> </xs:schema> |
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "attribute.xsd":
ÏÂÃæµÄÕâ·ÝÃûΪ "Myfamily.xml" µÄ XMLÎļþ£¬Ê¹ÓÃÁËÀ´×Ô "family.xsd" ºÍ "attribute.xsd" Á½Æª²»Í¬µÄschema×é¼þ£º
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns="http://www.microsoft.com" <person gender="female"> <person gender="male"> </persons> |
The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element.
ÉÏÊöXMLÎļþÊÇÓÐЧµÄ¡£ÒòΪÃûΪ"family.xsd" µÄ schema ÎļþÔÊÐíÎÒÃÇÔÚ"person"ÔªËØÀïÌí¼ÓÊôÐÔ¡£
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.
<any> ºÍ <anyAttribute> ÔªËØÊÇÓÃÓÚÀ©Õ¹ÎĵµµÄ£¡ËüÃÇÔÊÐíÎĵµÖаüº¬Î´ÔÚÖ÷Ìå XML schema ÖÐÉùÃ÷µÄÆäËüÐÂÔªËØ¡£
ÆÀÂÛ (0)
All