XML, XLink en XQuery
- Vorige pagina XML XQuery
- Volgende pagina XML Validator
XLink wordt gebruikt om hyperlinks in XML-documenten te maken
XPath
- XLink wordt gebruikt om hyperlinks in XML-documenten te maken
- Elk element in een XML-document kan dienen als een koppeling
- Met XLink kunnen koppelingen buiten het gelinkte bestand worden gedefinieerd
- XLink is een aanbevolen standaard van de W3C
XLink-browserondersteuning
Browsers ondersteunen XLink niet in XML-documenten.
Maar, alle主流 browsers ondersteunen het XLink in SVG.
XLink-syntaxis
In HTML definieert het <a>-element een hyperlink. Echter, dit is niet hoe het in XML werkt.
In een XML-document kun je elke elementnaam gebruiken die je wilt - dus kan de browser niet voorspellen welke koppelingelementen in het XML-document worden aangeroepen.
Hier is een eenvoudige voorbeeld van hoe je XLink kunt gebruiken om koppelingen in een XML-document te maken:
<?xml version="1.0" encoding="UTF-8"?> <homepages xmlns:xlink="http://www.w3.org/1999/xlink"> <homepage xlink:type="simple" xlink:href="https://www.codew3c.com">Bezoek CodeW3C.com</homepage> <homepage xlink:type="simple" xlink:href="http://www.w3.org">Bezoek W3C</homepage> </homepages>
Als we de functies van XLink moeten gebruiken, moeten we de XLink-naamruimte declareren. De XLink-naamruimte is: 'http://www.w3.org/1999/xlink'.
De xlink:type en xlink:href-eigenschappen in het <homepage>-element komen van deze XLink-naamruimte.
xlink:type="simple" maakt een eenvoudige 'soort HTML'-koppeling (wat betekent 'klik hier om ergens anders heen te gaan').
De xlink:href-eigenschap specificeert de URL die moet worden gelinkt.
Voorbeeld van XLink
De volgende XML-document bevat XLink-functies:
<?xml version="1.0" encoding="UTF-8"?> <bookstore xmlns:xlink="http://www.w3.org/1999/xlink"> <book title="Yashu Tanchi"> <description xlink:type="simple" xlink:href="/images/cswd.jpg" xlink:show="new"> Het boek 'Yashu Tanchi' is de gids voor de Shaojiang-keuken; het biedt de benodigde gids en hulp aan liefhebbers van eten en zoekers naar smaken; Dit boek kan ook worden beschouwd als een culturele leesgids voor de Shaojiang-keuken; het beschrijft de Shaojiang-keuken en niet de Shaojiang-eten, en onthult volledig de lokale cultuur, eetgewoonten en historische details achter de voedsel. </description> </book> <book title="De onmisbare vossenvader"> <description xlink:type="simple" xlink:href="/images/mrfox.jpg" xlink:show="new"> Bogies, Bons en Bains zijn de laaklijst en zuinigste mensen die je kunt tegenkomen; ze haten de heer van de vossen en hebben besloten dat hij en zijn familie moeten worden uitgeroeid. En zo stonden deze drie mannen met hun wapens schuil voor de hol van de heer van de vossen… Maar de heer van de vossen had wel een slim plan om hen te verslaan! En zo begon een strijd om wijsheid en kracht... </description> </book> </bookstore>
例子解释:
- XLink 命名空间在文档顶部声明 (xmlns:xlink="http://www.w3.org/1999/xlink")
- xlink:type="simple" 创建一个简单的“类似 HTML”的链接
- xlink:href 属性指定要链接到的 URL(在本例中为图像)
- xlink:show="new" 指示链接应在新窗口中打开
XLink - 走得更远
在上面的例子中,我们演示了简单的 XLink。
如果我们将远程的位置作为资源而不是独立页面访问时,XLink 将变得更加有趣。
如果我们将 xlink:show 属性值设置为 "embed",则被链接的资源应在页面内内联处理。例如,如果您认为这可能是另一个 XML 文档,则可以构建 XML 文档的层次结构。
您还可以使用 xlink:actuate 属性指定资源应出现的时间。
XLink 属性参考
属性 值 描述 xlink:actuate onLoad onRequest other none 定义何时读取并显示被链接的资源: onLoad - 资源应该在文档加载时加载并显示 onRequest - The resource will not be read or displayed before clicking the link xlink:href URL specifies the URL to which the link points. xlink:show embed new replace other none specifies where to open the link. The default is "replace". xlink:type simple extended locator arc resource title none specifies the type of link.XPointer
XPath- XPointer allows links to point to specific parts of an XML document
- XPointer uses XPath expressions to navigate in XML documents
- XPointer is a W3C recommended standard
XPointer browser support
No browsers support XPointer. But XPointer is also used in other XML languages.
XPointer example
In this example, we will combine the use of XPointer and XLink to point to a specific part of another document.
we first take a look at this target XML document (the document we link to):
<?xml version="1.0" encoding="UTF-8"?> <dogbreeds> <dog breed="Rottweiler" id="Rottweiler"> <picture url="https://dog.com/rottweiler.gif" /> <history>The Rottweiler's ancestors were probably Roman drover dogs.....</history> <temperament>Confident, bold, alert and imposing, the Rottweiler is a popular choice for its ability to protect....</temperament> </dog> <dog breed="FCRetriever" id="FCRetriever"> <picture url="https://dog.com/fcretriever.gif" /> <history>One of the earliest uses of retrieving dogs was to help fishermen retrieve fish from the water....</history> <temperament>The flat-coated retriever is a sweet, exuberant, lively dog that loves to play and retrieve....</temperament> </dog> </dogbreeds>
Please note that the XML document uses the id attribute on each element!
Therefore, XPointer allows you to link to a specific part of the document, rather than linking to the entire document (like XLink).
To link to a specific part of a page, add a hash (#) and an XPointer expression to the URL in the xlink:href attribute, as shown below:
xlink:href="https://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"This expression refers to the element with the id value "Rottweiler" in the target document.
xlink:href="https://dog.com/dogbreeds.xml #xpointer(id('Rottweiler'))".XPointer also allows for abbreviated methods to link to elements with an id. You can directly use the value of the id, as shown below:
xlink:href="https://dog.com/dogbreeds.xml#RottweilerBelow is the XML document that contains links to more information about each dog's breed:
<?xml version="1.0" encoding="UTF-8"?> <mydogs xmlns:xlink="http://www.w3.org/1999/xlink"> <mydog> <description> Anton is my favorite dog. He has won a lot of..... </description> <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#Rottweiler"> Fact about Rottweiler </fact> </mydog> <mydog> <description> Pluto is the sweetest dog on earth...... </description> <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#FCRetriever"> Feit over de Flat-Coated Retriever </fact> </mydog> </mydogs>
- Vorige pagina XML XQuery
- Volgende pagina XML Validator