XLink 和 XPointer 语法

下面是 XLinkXPointer 最常用、最实用的完整语法汇总,直接可抄可用的“速查表”形式(2025 年仍然有效的写法)。

1. XLink 语法速查表

链接类型必填属性常用可选属性典型写法示例
简单链接
(等价于 HTML
xlink:type=”simple”
xlink:href=”URI”
xlink:role, xlink:title
xlink:show
xlink:actuate
“`xml
扩展链接xlink:type=”extended”<extendedlink xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended">
定位器
(指向外部资源)
xlink:type=”locator”
xlink:href=”URI”
xlink:label=”名字”
xlink:role, xlink:titlexml<br><person xlink:type="locator"<br> xlink:href="people.xml#id123"<br> xlink:label="john"<br> xlink:title="约翰"/>
资源
(本地内容)
xlink:type=”resource” 或 “resource”
xlink:label=”名字”
xlink:role, xlink:titlexml<br><photo xlink:type="resource" xlink:label="pic1"><br> <desc>婚纱照</desc><br></photo>

(从 A 到 B 的遍历)
xlink:type=”arc”
xlink:from=”label”
xlink:to=”label”
xlink:arcrole
xlink:show
xlink:actuate
xlink:title
xml<br><goes xlink:type="arc"<br> xlink:from="john"<br> xlink:to="mary"<br> xlink:arcrole="http://example.org/rel#spouse"<br> xlink:show="replace"<br> xlink:actuate="onRequest"/>
标题元素xlink:type=”title”xml<br><title xlink:type="title">家人关系图</title>

xlink:show 的值
new | replace | embed | other | none

xlink:actuate 的值
onRequest(用户点击) | onLoad(文档加载时自动) | other | none

2. XPointer 语法速查表(写在 URI 的 # 后面)

类型语法格式例子
简写指针(Shorthand)#id值#chap3 → 等价于 id(‘chap3’)
xpointer() 方案#xpointer(XPath表达式)#xpointer(//section[@id='intro'])
多段备用(推荐写法)#xpointer(主表达式) xpointer(备用表达式)#xpointer(id('chap3')) xpointer(//chapter[position()=3])
element() 方案#element(路径或ID)#element(/1/3/2)
#element(chap3)(如果有 element 名为 chap3)
xpath1()(旧写法)#xpath1(/book/chapter[2])很少用
带命名空间#xpointer(//svg:rect) xmlns(svg=http://www.w3.org/2000/svg)SVG 中常用
范围(range)#xpointer(起点/range-to(终点))#xpointer(id('p1')/range-to(id('p5'))
指向字符串出现位置#xpointer(string-range(//para,”XML”)[1])指向第一个包含“XML”的段落
完整真实世界常用写法文件名 + 多段备用 + 命名空间svgmap.svg#xmlns(svg=http://www.w3.org/2000/svg)<br>xpointer(//svg:g[@id='beijing'])<br>xpointer(id('beijing'))

3. 真实项目中最常见的 5 种组合写法(直接复制)

<!-- 1. 最常见的简单链接 + ID -->
<ref xlink:type="simple" xlink:href="spec.xml#intro"/>

<!-- 2. 简单链接 + 完整 XPointer(兼容性最好) -->
<go xlink:type="simple"
    xlink:href="paper.xml#xpointer(//section[@id='conclusion'])"/>

<!-- 3. 带命名空间的 SVG 链接(2025 年仍天天用) -->
<use xlink:href="icons.svg#xpointer(//svg:symbol[@id='home'])
                   xmlns(svg=http://www.w3.org/2000/svg)"/>

<!-- 4. EPUB 3 中典型的脚注写法 -->
<note xlink:type="simple"
      xlink:href="content.xhtml#xpointer(id('note1'))"
      xlink:role="http://www.w3.org/1999/xhtml/role#note"/>

<!-- 5. 第三方扩展链接(放在独立文件 links.xml 中) -->
<relationships xmlns:xlink="http://www.w3.org/1999/xlink"
               xlink:type="extended">
  <loc xlink:type="locator" xlink:href="a.xml#id1" xlink:label="A"/>
  <loc xlink:type="locator" xlink:href="b.xml#id2" xlink:label="B"/>
  <arc xlink:type="arc" xlink:from="A" xlink:to="B"
       xlink:arcrole="http://example.org/rel#references"/>
</relationships>

记住这两句话就够用了:

  • 所有 XLink 必须声明命名空间 xmlns:xlink="http://www.w3.org/1999/xlink"
  • XPointer 永远写在 URI 的 # 后面,最保险的写法是:#xpointer(你的XPath) 或多段备用

需要我给你一个特定场景(比如 SVG 交互地图、EPUB 双向注脚、TEI 文献引用)的完整可运行模板,随时说!

文章已创建 2783

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部