XML 、XLink、XPointer
XLinkは、XMLドキュメントにハイパーリンクを作成するために使用されます。
|
XLinkブラウザのサポート
XMLドキュメントでのXLinkのブラウザサポートはありません。
ただし、すべての主要なブラウザはSVGでXLinksをサポートしています。
XLink構文
HTMLでは、<a>要素はハイパーリンクを定義します。ただし、これはXMLでの動作とは異なります。XMLドキュメントでは、任意の要素名を使用できます。したがって、ブラウザがXMLドキュメントで呼び出されるリンク要素を予測することはできません。
以下は、XLinkを使用してXMLドキュメントにリンクを作成する方法の簡単な例です。
<?xml version="1.0" encoding="UTF-8"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
<homepage xlink:type="simple"
xlink:href="https://www.w3schools.com">Visit W3Schools</homepage>
<homepage xlink:type="simple"
xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>
XLink機能にアクセスするには、XLink名前空間を宣言する必要があります。XLink名前空間は「http://www.w3.org/1999/xlink」です。
<homepage>要素のxlink:type属性とxlink:href属性は、XLink名前空間から取得されます。
xlink:type = "simple"は、単純な「HTMLのような」リンクを作成します(「ここをクリックしてそこに移動する」という意味です)。
xlink:href属性は、リンク先のURLを指定します。
XLinkの例
次のXMLドキュメントにはXLink機能が含まれています。
<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Harry Potter">
<description
xlink:type="simple"
xlink:href="/images/HPotter.gif"
xlink:show="new">
As his fifth year at Hogwarts School of Witchcraft and
Wizardry approaches, 15-year-old Harry Potter is.......
</description>
</book>
<book title="XQuery Kick Start">
<description
xlink:type="simple"
xlink:href="/images/XQuery.gif"
xlink:show="new">
XQuery Kick Start delivers a concise introduction
to the XQuery standard.......
</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属性リファレンス
Attribute | Value | Description |
---|---|---|
xlink:actuate | onLoad onRequest other none |
Defines when the linked resource is read and shown:
|
xlink:href | URL | Specifies the URL to link to |
xlink:show | embed new replace other none |
Specifies where to open the link. Default is "replace" |
xlink:type | simple extended locator arc resource title none |
Specifies the type of link |
XPointer
|
XPointerブラウザのサポート
XPointerのブラウザサポートはありません。ただし、XPointerは他のXML言語で使用されます。
XPointerの例
この例では、XPointerをXLinkと組み合わせて使用して、別のドキュメントの特定の部分をポイントします。
まず、ターゲットXMLドキュメント(リンク先のドキュメント)を確認します。
<?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>
上記のXMLドキュメントでは、各要素にid属性が使用されていることに注意してください。
そのため、(XLinkのように)ドキュメント全体にリンクする代わりに、XPointerを使用するとドキュメントの特定の部分にリンクできます。ページの特定の部分にリンクするには、次のように、xlink:href属性のURLの後に番号記号(#)とXPointer式を追加します。xlink:href = "https://dog.com/dogbreeds.xml #xpointer(id( 'Rottweiler')) "。この式は、ID値が「Rottweiler」のターゲットドキュメント内の要素を参照します。
XPointerでは、IDを持つ要素にリンクするための簡略化されたメソッドも使用できます。次のように、idの値を直接使用できます:xlink:href = "https://dog.com/dogbreeds.xml#Rottweiler"
次のXMLドキュメントには、各犬の犬種の詳細情報へのリンクが含まれています。
<?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">
Fact about flat-coated Retriever
</fact>
</mydog>
</mydogs>