w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD ÔªËØÌæ´ú
With XML Schemas, one element can substitute another element.
ÎÒÃÇ¿ÉÒÔͨ¹ýXML SchemaʵÏÖÒ»¸öÔªËØÌæ´úÁíÒ»ÔªËØ¡£
Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document.
¼ÙÉèÕâÀï°üº¬Á½¸ö·Ö±ðÀ´×ÔÓ¢¹úºÍŲÍþµÄʹÓÃÕߣ¬ÎÒÃÇÏ£ÍûʵÏÖÕâÑùµÄ¹¦ÄÜ£ºÁ½Î»Ê¹ÓÃÕßÄܹ»¸ù¾Ý×Ô¼ºµÄÓïÑÔÄÜÁ¦Ñ¡ÔñXMLÎĵµÖÐËùʹÓõÄŲÍþÔªËØÃû»òÓ¢ÎÄÔªËØÃû¡£
To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
ΪÁ˽â¾öÕâ¸öÎÊÌ⣬ÎÒÃÇÔÚXML schemaÀﶨÒåÁËÌæ´ú×é¡£Ê×ÏÈ£¬ÎÒÃÇÉùÃ÷ÁËÒ»¸ö±êÌâÔªËØ£¬È»ºó£¬ÎÒÃÇÉùÃ÷ÁËÓÃÓÚÌæ´ú±êÌâÔªËØµÄÆäËüÔªËØ£º
<xs:element name="name" type="xs:string"/> |
In the example above, the "name" element is the head element and the "navn" element is substitutable for "name".
ÔÚÉÏÊö°¸ÀýÖУ¬"name" ÔªËØÊDZêÌâÔªËØ£¬"navn" ÔªËØÊÇ "name" Ìæ´úÔªËØ¡£
Look at this fragment of an XML schema:
Çë¿´ÏÂÃæµÄXML schemaƬ¶Î£º
<xs:element name="name" type="xs:string"/> <xs:complexType name="custinfo"> <xs:element name="customer" type="custinfo"/> |
A valid XML document (according to the schema above) could look like this:
¸ù¾ÝÉÏÊöschemaµÄXMLÎĵµ£¬ÎÒÃǵõ½ÁË Ò»·ÝÓÐЧµÄXMLÎĵµ£¬ÈçÏÂËùʾ£º
<customer> |
or like this:
»òÕßÈçÏÂËùʾ£º
<kunde> |
To prevent other elements from substituting with a specified element, use the block attribute:
ΪÁË·ÀÖ¹ÆäËüÔªËØ±»ÒÑÖ¸¶¨µÄÔªËØÌæ´ú£¬¿ÉÒÔÓÃblock [ Ëø¶¨ ] ÊôÐÔ£º
<xs:element name="name" type="xs:string" block="substitution"/> |
Look at this fragment of an XML schema:
Çë¿´ÏÂÃæÕâ¶ÎXML schemaƬ¶Î£º
<xs:element name="name" type="xs:string" block="substitution"/> <xs:complexType name="custinfo"> <xs:element name="customer" type="custinfo" block="substitution"/> |
A valid XML document (according to the schema above) looks like this:
¸ù¾ÝÉÏÊöschemaµÄXMLÎĵµ£¬ÎÒÃǵõ½ÁË Ò»·ÝÓÐЧµÄXMLÎĵµ£¬ÈçÏÂËùʾ£º
<customer> |
BUT THIS IS NO LONGER VALID:
µ«ÊÇÏÂÃæÕâÑù¾Í²»ÔÙÓÐЧÁË£º
<kunde> |
The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element.
Ìæ´úÔªËØµÄÀàÐÍÓ¦¸ÃÓë±êÌâÔªËØµÄÀàÐÍÏàͬ£¬»òÊÇ´ÓÖÐÅÉÉú³öÀ´µÄ¡£Èç¹ûÌæ´úÔªËØµÄÀàÐͺͱêÌâÔªËØµÄÀàÐÍÏàͬ£¬Äã¾Í²»ÐèÒªÔÙÖ¸Ã÷Ìæ´úÔªËØµÄÀàÐÍÁË¡£
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!
×¢Òâ£ºÌæ´úÔªËØ×éÄÚµÄËùÓÐÔªËØ£¨±êÌâÔªËØºÍÌæ´úÔªËØ£©±ØÐëÉùÃ÷Ϊ“È«ÓòÔªËØ£¨global element£©”£¬·ñÔòËüÊDz»»á·¢Éú×÷Óõģ¡
Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements.
“È«ÓòÔªËØ”ÊÇ"schema"ÔªËØÏÂÃæµÄÖ±½Ó×ÓÔªËØ¡£“±¾µØÔªËØ”ÊÇǶÌ×ÔÚÆäËüÔªËØÄÚµÄÔªËØ¡£
ÆÀÂÛ (0)
All