w3pop.com :: ÍøÂçѧԺ :: XForms :: XForms XPath
XForms uses XPath to address data. This is called binding."
XFormsÓÃXPath¶ÔÊý¾Ý½øÐÐѰַ£¬Õâ³ÆÖ®Îª°ó¶¨£¨binging£©¡£
XForms uses two sections to define data: The XForms model and the XForms user interface.
XForms ʹÓÃÁ½¸ö²¿·ÖÀ´¶¨ÒåÊý¾Ý£ºXForms Ä£ÐÍºÍ XForms Óû§½çÃæ¡£
The XForms model is an XML template (instance) for the data, and the XForms user interface is a description of the input and display of the data.
XForms Ä£ÐÍÊÇÕë¶ÔÊý¾ÝµÄÒ»¸ö XML Ä£°å£¨ÊµÀý£©£¬¶ø XForms Óû§½çÃæÊǶÔÊý¾ÝÊäÈëºÍÏÔʾµÄÃèÊö¡£
XForms uses XPath to define the connection between the two sections. This is called binding.
XForms ʹÓà XPath À´¶¨ÒåÕâ¸öÁ½¸ö²¿·ÖµÄÁ¬½Ó¡£Õâ±»³ÆÎª°ó¶¨£¨binding£©¡£
XPath is a W3C standard syntax for defining parts of XML documents.
XPath ÓÃÓÚ¶¨Òå XML ÎĵµµÄ¸÷²¿·ÖµÄ W3C ±ê×¼Óï·¨¡£
XPath uses path expressions to identify nodes in an XML document. These path expressions look much like the expressions you see when you work with a computer file system.
XPath ʹÓ÷¾¶±í´ïʽÀ´±êʾ XML ÎĵµÖеĽڵ㡣ÕâЩ±í´ïʽÓëÄúÔÚ¼ÆËã»úϵͳÖп´µ½µÄÎļþ·¾¶±í´ïʽ·Ç³£ÀàËÆ¡£
This XPath expression:
XPath±í´ïʽ£º
/person/fname |
addresses the fname node in the XML document:
´«ËÍXMLÎĵµÖеÄfname½Úµã£º
<person> |
Learn more about XPath in our XPath tutorial
ÔÚÎÒÃǵÄXPath½Ì³ÌÀïÁ˽â¸ü¶àÄÚÈÝ¡£
With an XForms model instance like this:
XForms Ä£ÐÍÊÇÕâÑùµÄ£º
<instance> |
The XForms user interface can bind <input> elements using the ref attribute:
XForms Óû§½çÃæ¿ÉʹÓà ref ÊôÐÔÀ´À¦°ó <input> ÔªËØ£º
<input ref="name/fname"> <input ref="name/lname"> |
The ref="name/fname" attribute in the example above is an XPath expression pointing to the <fname> element in the instance model. This binds the input field to the <fname> element in the XML document (instance) that is collecting data from the form.
ÉÏÃæÀý×ÓÖÐµÄ ref="name/fname" ÊôÐÔÊÇÒ»¸öÖ¸ÏòʵÀýÄ£ÐÍÖÐ <fname> ÔªËØµÄ XPath ±í´ïʽ¡£Ëü¿É½«ÊäÈëÓòÀ¦°óµ½Õý´Ó±íµ¥ÊÕ¼¯Êý¾ÝµÄ XML Îĵµ£¨ÊµÀý£©ÖÐµÄ <fname> ÔªËØ¡£
The XForms user interface could also use a reference like this:
XForms Óû§½çÃæÒ²¿ÉÒÔʹÓÃÒýÓãº
<input ref="/person/name/fname"> <input ref="/person/name/lname"> |
In the example above, the slash (/) at the beginning of the XPath expression indicates the root of the XML document.
ÔÚÉÏÃæµÄÀý×ÓÖУ¬XPath ±í´ïʽÆðʼ²¿Î»µÄÕýб¸Ü (/) ָʾ XML ÎĵµµÄ¸ù¡£
With an XForms model instance like this:
XForms Ä£ÐÍÊÇÕâÑùµÄ£º
<model> |
The XForms user interface can bind <input> elements using the bind attribute:
XForms Óû§½çÃæ¿ÉʹÓà bind ÊôÐÔÀ´À¦°ó <input> ÔªËØ£º
<input bind="firstname"> <input bind="lastname"> |
Why two different ways to bind input control elements to instance data?
Ϊʲô»áÓÐÁ½ÖÖ½«ÊäÈë¿Ø¼þÔªËØÀ¦°óµ½ÊµÀýÊý¾ÝµÄ·½·¨ÄØ£¿
Well, when you start using XForms in complex applications, you will find binding using bind to be a more flexible way to deal with multiple forms and multiple instance models.
ÔõÃ´ËµÄØ£¬µ±ÄúÔÚ¸´ÔÓµÄÓ¦ÓóÌÐòÖпªÊ¼Ê¹Óà XForms ʱ£¬Äú»á·¢ÏÖ£¬Ê¹Óà bind ½øÐаó¶¨ÊÇÒ»ÖÖ¸üÁé»îµÄ´¦Àí¶àÖØ±íµ¥ºÍ¶àÖØÊµÀýÄ£Ð͵ķ½·¨¡£
ÆÀÂÛ (0)
All