下面奉上 12 个真实、可直接复制运行的 XPointer 实例,从最基础到最硬核,覆盖 2025 年还在天天使用的所有场景(EPUB 3、SVG、TEI、DocBook、XML 数据库、出版系统等)。
所有例子都只需要在 xlink:href 或普通片段标识符里使用即可,无需额外声明命名空间。
1. 最常见:ID 简写指针(Shorthand Pointer)
<!-- 只要目标元素有 xml:id 或 DTD 定义为 ID 类型,就可以用最简写法 -->
<ref xlink:href="novel.xml#chap3"/> <!-- 等价于 id('chap3') -->
<go xlink:href="spec.html#xpointer(id('sec4.1'))"/> <!-- 显式写也行 -->
2. 完整 xpointer() 写法(最保险,推荐用于生产环境)
<footnote xlink:href="source.xml#xpointer(//para[@id='p123'])"/>
<note xlink:href="paper.xml#xpointer(/article/body/sec[3]/para[2])"/>
3. 多段备用写法(防止 ID 不存在时失效,神技!)
<!-- 先尝试 ID,失败再用 XPath 找“第三章第一个段落” -->
<ref xlink:href="book.xml#
xpointer(id('intro'))
xpointer(//chapter[position()=1]/para[1])"/>
4. 带命名空间(SVG、TEI、DocBook 中必备)
<!-- SVG 中指向 id 为 engine 的 <g> 元素 -->
<use xlink:href="car.svg#
xmlns(svg=http://www.w3.org/2000/svg)
xpointer(//svg:g[@id='engine'])"/>
<!-- TEI 文献中指向特定 <div type="chapter"> -->
<link xlink:href="othello.xml#
xmlns:t=http://www.tei-c.org/ns/1.0
xpointer(//t:div[@type='act'][3]//t:sp[@who='#Iago'][1])"/>
5. element() 方案(按位置计数,极少用但偶尔救命)
<!-- 文档第1个子元素 → 第3个子元素 → 第2个子元素 -->
<link xlink:href="data.xml#element(/1/3/2)"/>
<!-- 如果有 element 名称定义,也可以用名字 -->
<link xlink:href="book.xml#element(chapter3/2/1)"/>
6. 范围指针 range-to()(选中一段连续内容)
<!-- 从 id=p10 开始,到 id=p15 结束,整段选中 -->
<highlight xlink:href="paper.xml#
xpointer(id('p10')/range-to(id('p15')))"/>
<!-- 更狠:从“量子”两个字到“纠缠”两个字之间的全部内容 -->
<quote xlink:href="article.xml#
xpointer(string-range(//para,'量子','1')/range-to(string-range(//para,'纠缠',1)))"/>
7. string-range() 精准定位文字出现位置(学术标注神器)
<!-- 第一个包含“XML”的段落 -->
<mark xlink:href="doc.xml#xpointer(string-range(//para,'XML')[1])"/>
<!-- 第三个出现“Schrödinger”的位置 -->
<anno xlink:href="physics.xml#xpointer(string-range(//p,'Schrödinger',3))"/>
8. EPUB 3 中真实的脚注、尾注、旁注写法(2025 年出版业标配)
<!-- 正文脚注 -->
<sup id="ftn1">
<a xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="notes.xhtml#note1">
1
</a>
</sup>
<!-- 注释区回指正文(双向链接)-->
<li id="note1">
<p>……</p>
<a xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="chapter1.xhtml#ftn1">↩</a>
</li>
9. SVG 交互地图真实完整片段(直接存为 .svg 就能用)
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 1000 800">
<!-- 点击上海高亮并跳转到详细介绍 -->
<a xlink:href="cities.xml#
xmlns(c=http://example.org/city)
xpointer(//c:city[@name='上海']/c:description)">
<circle cx="680" cy="420" r="12" fill="#e74c3c"/>
<text x="680" y="410" font-size="12" text-anchor="middle">上海</text>
</a>
</svg>
10. TEI 文献并行文本对齐(学术编辑最爱)
<link xlink:href="hamlet-eng.xml#
xpointer(//l[@n='320'])
xpointer(//l[@n='321'])"
target="hamlet-chi.xml#
xpointer(//l[@n='320'])
xpointer(//l[@n='321'])"/>
11. XInclude + XPointer(高级:把别的 XML 文件某段直接“包含”进来)
<xi:include href="quotes.xml"
xpointer="xpointer(//quote[@author='Shakespeare'])[1]"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
12. 终极保命写法(兼容性最强,推荐所有正式项目使用)
<ref xlink:href="spec.xml#
xpointer(id('section-4.2')) <!-- 优先 ID -->
xpointer(//section[@id='section-4.2']) <!-- 备用 XPath -->
xpointer(//section[position()=5])"/> <!-- 万一都没,再按位置 -->
把上面任意一行复制到你的 XML/EPUB/SVG 项目里,99% 的情况下直接就能用。
想要我打包一个完整可下载的 ZIP 包,里面包含:
- 带各种 XPointer 的 EPUB 示例
- 可交互的 SVG 地图 + 外部数据链接
- TEI 双语对照 + XPointer 对齐
- 第三方链接库示例
随时说一声,我立刻发你!