w3pop.com :: ÍøÂçѧԺ :: XLink/XPointer :: XPointer ʵÀý
Let's try to learn some basic XPointer syntax by looking at an example.
½²ÁËÕâô¶à£¬»¹ÊÇÈÃÎÒÃÇͨ¹ýһЩʵÀýÀ´Ñ§Ï°Ò»Ð©»ù±¾µÄXPointerÓï·¨°É¡£
In this example, we will show you how to use XPointer in conjunction with XLink to point to a specific part of another document.
ÔÚÕâ¸öʵÀýÖУ¬ÎÒÃÇ»á¸øÄãչʾÈçºÎͨ¹ýXPointerÓëXLinkÀ´Ö¸ÏòÆäËüÎĵµÖеÄÒ»¶Î¾ßÌ岿·Ö¡£
We will start by looking at the target XML document (the document we are going to link to).
ÎÒÃÇ»áͨ¹ý²éÔÄÄ¿±êXMLÎļþ£¨ÎÒÃÇ×¼±¸Á´½ÓµÄÎĵµ£©¿ªÊ¼ÎÒÃǵÄѧϰ¡£
The target XML document is called "dogbreeds.xml" and it lists a few different dog breeds:
Ä¿±êXMLÎĵµÃûΪ“dogbreeds.xml”£¬ËüÁоÙÁËһЩ¹·µÄÖÖÀࣺ
<?xml version="1.0" encoding="ISO-8859-1"?> <dogbreeds> <dog breed="Rottweiler" id="Rottweiler"> <dog breed="FCRetriever" id="FCRetriever"> </dogbreeds> |
View the "dogbreeds.xml" file in your browser
ÔÚÄãµÄä¯ÀÀÆ÷ÖвéÔÄ“dogbreeds.xml”µÄÎļþ
Note that the XML document above uses id attributes on each element we may want to link to!
×¢ÒâÉÏÃæµÄXMLÎĵµ£¬ËüÔÚÿ¸öÎÒÃÇÏ£ÍûÁ´½ÓµÄÔªËØÉÏʹÓÃÁËIDÊôÐÔ£¡
Instead of linking to the entire document (as with XLink), XPointer allows you to link to specific parts of the document. To link to a specific part of a page, add a number sign (#) and an XPointer expression after the URL in the xlink:href attributes.
ÓëXLinkÁ´½Óµ½ÍêÕûµÄÎĵµ²»Í¬µÄÊÇ£¬XPointerÔÊÐíÄãÁ´½Óµ½ÎĵµÖеľßÌåÖ¸¶¨µÄ²¿·Ö¡£Èç¹ûÄãÏ£ÍûÁ´½Óµ½Ò³ÃæµÄij¸ö¾«È·²¿·Ö£¬¾ÍÐèÒªÔÚ xlink:href ÊôÐÔµÄURIÖÐÌí¼ÓÒ»¸ö“#”ºÍ“XPointer”±í´ïʽ¡£
The expression: #xpointer(id("Rottweiler")) refers to the element in the target document, with the id value of "Rottweiler".
±í´ïʽ£º#xpointer(id("Rottweiler")) ͨ¹ý "Rottweiler" µÄIDÖµÒýÓÃÁËÄ¿±êÎĵµÖÐÔªËØ¡£
So the xlink:href attribute would look like this: xlink:href=http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))
Òò´Ë£¬xlink:href µÄÊôÐÔÈçÏ£ºxlink:href=http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))
However, XPointer allows a shorthand form when linking to an element with an id. You can use the value of the id directly, like this: xlink:href=http://dog.com/dogbreeds.xml#Rottweiler
µ±È»£¬µ±Á´½Óµ½Ò»¸ö°üº¬IDµÄÔªËØÊ±£¬XPointerÔÊÐíʹÓÃËõд¸ñʽ¡£Äã¿ÉÒÔÖ±½ÓʹÓÃIDÖµ£¬È磺xlink:href=http://dog.com/dogbreeds.xml#Rottweiler
The following XML document refers to information of the dog breed for each of my dogs :-), all through XLink and XPointer references:
ÏÂÊöXMLÎĵµÍ¨¹ýXLinkºÍXPointer²ÎÊýÒýÓÃÁËÎÒµÄÿֻ¹·µÄÖÖÀࣺ
<?xml version="1.0" encoding="ISO-8859-1"?> <mydogs xmlns:xlink="http://www.w3.org/1999/xlink"> <mydog xlink:type="simple" <mydog xlink:type="simple" </mydogs> |
ÆÀÂÛ (0)
All