XLink 实例

下面给出 8 个真实、可直接复制运行的 XLink 实例,覆盖从最基础到高级的全部常用场景(2025 年仍在 SVG、EPUB、TEI、DocBook、XML 数据库中天天使用)。

1. 最基础:简单链接(和 HTML 完全等价)

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:xlink="http://www.w3.org/1999/xlink">
  <title>XML 入门</title>
  <chapter id="ch1">第一章</chapter>
  
  <link xlink:type="simple"
        xlink:href="https://www.w3.org/TR/xlink/"
        xlink:show="new"
        xlink:actuate="onRequest"
        xlink:title="W3C XLink 官方规范">
    查看 XLink 规范(新窗口打开)
  </link>
</book>

2. 指向同一文档内部(最常见内部锚点)

<toc xmlns:xlink="http://www.w3.org/1999/xlink">
  <item xlink:type="simple" xlink:href="#ch1">第一章 简介</item>
  <item xlink:type="simple" xlink:href="#ch2">第二章 XLink 语法</item>
</toc>

<chapter id="ch2">第二章内容...</chapter>

3. 完整 XPointer 指向其他 XML 文件的精确位置

<footnote xmlns:xlink="http://www.w3.org/1999/xlink"
          xlink:type="simple"
          xlink:href="source.xml#xpointer(//para[@id='p87'])"
          xlink:title="原文出处">
  [87]
</footnote>

4. SVG 中最常见的 XLink(2025 年每天都在用)

文件:map.svg

<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink" width="800" height="600">

  <!-- 重用外部图标库 -->
  <defs>
    <g id="home-icon">
      <use xlink:href="icons.svg#house" x="0" y="0"/>
    </g>
  </defs>

  <!-- 点击北京跳到详细页 -->
  <circle cx="400" cy="200" r="15" fill="red"/>
  <text x="400" y="190" text-anchor="middle">北京</text>
  
  <a xlink:href="beijing.xml#xpointer(//section[@id='history'])"
     xlink:show="replace"
     xlink:actuate="onRequest">
    <circle cx="400" cy="200" r="20" fill="transparent"/>
  </a>
</svg>

5. EPUB 3 中真实的脚注与双向链接(出版业标准写法)

content.xhtml 片段:

<p>量子力学在 1925 年由海森堡提出<note id="n1"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xlink:type="simple"
     xlink:href="notes.xhtml#note1">1</note></p>

<!-- 回链:从注释回到正文 -->
<div id="note1" class="note">
  <p>矩阵力学的诞生标志……</p>
  <a xmlns:xlink="http://www.w3.org/1999/xlink"
     xlink:type="simple"
     xlink:href="content.xhtml#n1"
     xlink:title="返回正文">↩ 返回</a>
</div>

6. 第三方扩展链接(最强大功能:链接可以放在独立文件)

links.xml(可以放在服务器任意位置)

<?xml version="1.0"?>
<relationships xmlns:xlink="http://www.w3.org/1999/xlink"
               xlink:type="extended">
  
  <!-- 参与者 -->
  <author xlink:type="locator" 
          xlink:href="people.xml#xpointer(id('shakespeare'))" 
          xlink:label="shakespeare"/>
  
  <work xlink:type="locator" 
        xlink:href="romeo-and-juliet.xml" 
        xlink:label="play"/>
  
  <character xlink:type="locator" 
             xlink:href="romeo-and-juliet.xml#xpointer(//speaker[text()='Romeo'])" 
             xlink:label="romeo"/>

  <!-- 关系弧 -->
  <created xlink:type="arc"
           xlink:from="shakespeare"
           xlink:to="play"
           xlink:arcrole="http://example.org/rel#author-of"
           xlink:title="莎士比亚创作了《罗密欧与朱丽叶》"/>

  <appears-in xlink:type="arc"
              xlink:from="romeo"
              xlink:to="play"
              xlink:arcrole="http://example.org/rel#character-in"/>
</relationships>

7. 嵌入图片或多媒体(show=“embed”)

<gallery xmlns:xlink="http://www.w3.org/1999/xlink">
  <item xlink:type="simple"
        xlink:href="cat.jpg"
        xlink:show="embed"
        xlink:actuate="onLoad"
        xlink:title="小猫"/>
        
  <item xlink:type="simple"
        xlink:href="music.mp3"
        xlink:show="embed"
        xlink:actuate="onLoad"/>
</gallery>

8. 带语义的链接(使用 role 和 arcrole)

<citation xmlns:xlink="http://www.w3.org/1999/xlink"
          xlink:type="simple"
          xlink:href="https://doi.org/10.1000/182"
          xlink:role="http://purl.org/spar/cito/cites"
          xlink:arcrole="http://purl.org/spar/cito/citesAsEvidence"
          xlink:title="引用文献 [1]">
  [1]
</citation>

这些实例全部可以在以下环境中直接运行

  • 任何支持 SVG 的浏览器(实例 4)
  • Calibre、Sigil、Apple Books、Readium(实例 5)
  • Oxygen XML Editor、XSpect、XML 数据库(实例 6)
  • 任何支持 XInclude/XPointer 的 XML 工具链

需要我再给你一个「完整可下载的 ZIP 示例包」(包含 SVG 地图 + EPUB 脚注 + 第三方链接集),随时说,我直接打包发你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值