XML, XLink e XQuery

O XLink é usado para criar hiperlinks em documentos XML

XPath

  • O XLink é usado para criar hiperlinks em documentos XML
  • Qualquer elemento pode atuar como link em documentos XML
  • Através do XLink, é possível definir links fora do arquivo vinculado
  • O XLink é um padrão recomendado pelo W3C

Suporte do navegador para XLink

Os navegadores não suportam o XLink em documentos XML.

No entanto, todos os navegadores principais suportam XLink no SVG.

Sintaxe do XLink

Em HTML, o elemento <a> define o hiperlink. No entanto, isso não é como ele funciona no XML.

Em documentos XML, você pode usar qualquer nome de elemento que desejar - portanto, o navegador não pode prever quais elementos de link serão chamados no documento XML.

Abaixo está um exemplo simples de como usar o XLink para criar links em documentos 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.codew3c.com">Visite CodeW3C.com</homepage>
  <homepage xlink:type="simple" xlink:href="http://www.w3.org">Visite W3C</homepage>
</homepages>

Se precisarmos acessar as funcionalidades do XLink, devemos declarar o espaço de nomes XLink. O espaço de nomes XLink é: "http://www.w3.org/1999/xlink".

Os atributos xlink:type e xlink:href no elemento <homepage> vêm do espaço de nomes XLink.

xlink:type="simple" cria um link "semelhante ao HTML" (isto é, "clique aqui para ir a outro lugar").

A propriedade xlink:href especifica o URL a ser vinculado.

XLink Example

The following XML document contains XLink features:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Yasha Talks About Eating">
  <description
  xlink:type="simple"
  xlink:href="/images/cswd.jpg"
  xlink:show="new">
  The book "Yasha Talks About Eating" is a guidebook for Chaozhou and Shantou cuisine, providing the necessary guidance and help for food lovers and those in search of flavors;
  This book can also be said to be a humanistic reader of Chaozhou and Shantou cuisine. It introduces Chaozhou and Shantou cuisine rather than Chaozhou and Shantou food, fully mining and displaying the hidden local culture, dietary customs, and historical details behind the food.
  </description>
</book>
<book title="The Magnificent Fox Dad">
  <description
  xlink:type="simple"
  xlink:href="/images/mrfox.jpg"
  xlink:show="new">
  Boggs, Bons, and Bean are the most despicable and mean-spirited people you can meet. They hate Mr. Fox and decide to eliminate his entire family.
  Thus, these three people, with guns, cunningly waited outside Mr. Fox's cave... But Mr. Fox had a clever plan to deal with them!
  Thus, a contest of wisdom and strength began...
  </description>
</book>
</bookstore>

Example Explanation:

  1. The XLink namespace is declared at the top of the document (xmlns:xlink="http://www.w3.org/1999/xlink")
  2. xlink:type="simple" creates a simple "similar to HTML" link
  3. The xlink:href attribute specifies the URL to which the link should point (in this case, an image)
  4. xlink:show="new" indicates that the link should open in a new window

XLink - Go Further

In the above example, we demonstrated a simple XLink.

XLink becomes more interesting when we use remote locations as resources instead of accessing them as standalone pages.

If we set the xlink:show attribute value to "embed", the linked resource should be handled inline within the page. For example, if you think it might be another XML document, you can build the hierarchy of the XML document.

You can also use the xlink:actuate attribute to specify when the resource should appear.

XLink Attribute Reference

Attribute Value Description xlink:actuate onLoad onRequest other none Define when to read and display the linked resources: onLoad - The resources should be loaded and displayed when the document is loaded onRequest - Não lê ou exibe o recurso antes de clicar no link xlink:href URL define o URL a ser linkado. xlink:show embed new replace other none define onde abrir o link. O padrão é "replace". xlink:type simple extended locator arc resource title none define o tipo de link. none, title, resource, arc, locator, extended, simple

XPointer

XPath
  • XPointer permite que os links apontem para partes específicas de documentos XML
  • XPointer usa expressões XPath para navegar em documentos XML
  • XPointer é um padrão recomendado pelo W3C

Suporte do navegador XPointer

Nenhum navegador suporta XPointer. Mas XPointer também é usado em outros idiomas XML.

Exemplo de XPointer

Neste exemplo, combinaremos o uso de XPointer e XLink, apontando para uma parte específica de outro documento.

Primeiro, vamos ver este documento de destino XML (o documento ao qual estamos linkando):

<?xml version="1.0" encoding="UTF-8"?>
<dogbreeds>
<dog breed="Rottweiler" id="Rottweiler">
  <picture url="https://dog.com/rottweiler.gif" />
  <history>Os antepassados do Rottweiler eram provavelmente romanos
  cães de pastoreio.....</history>
  <temperament>Confiantes, enérgicos, alertas e impressionantes, o Rottweiler
  é uma escolha popular por sua capacidade de proteger....</temperament>
</dog>
<dog breed="FCRetriever" id="FCRetriever">
  <picture url="https://dog.com/fcretriever.gif" />
  <history>Um dos primeiros usos de cães de resgate foi para
  ajudar os pescadores a pescar peixes da água....</history>
  <temperament>O retriever de pelagem plana é doce, exuberante,
  cão animado que adora brincar e apanhar....</temperament>
</dog>
</dogbreeds>

Observe que o documento XML usa a propriedade id em cada elemento!

Portanto, o XPointer permite que você links para partes específicas do documento, em vez de links para o documento inteiro (como XLink).

Para links para partes específicas da página, adicione um sinal de número (#) e uma expressão XPointer após a URL na propriedade xlink:href, conforme mostrado a seguir:

xlink:href="https://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"

A expressão faz referência ao elemento com o valor de id "Rottweiler" no documento alvo.

xlink:href="https://dog.com/dogbreeds.xml #xpointer(id('Rottweiler'))"。

O XPointer também permite usar métodos abreviados para links para elementos com id. Você pode usar diretamente o valor do id, conforme mostrado a seguir:

xlink:href="https://dog.com/dogbreeds.xml#Rottweiler

O seguinte documento XML contém links para mais informações sobre cada raça de cão:

<?xml version="1.0" encoding="UTF-8"?>
<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
<mydog>
  <description>
  Anton é o meu cão favorito. Ele ganhou muitos.....
  </description>
  <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#Rottweiler">
  Fato sobre o Rottweiler
  </fact>
</mydog>
<mydog>
  <description>
  Pluto é o cão mais doce do mundo......
  </description>
  <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#FCRetriever">
  Fact sobre o Retriever liso de pêlo
  </fact>
</mydog>
</mydogs>