xml - 使用 XPath 获取 XML 部分
问题描述
我有以下 xml(TEI) 结构:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../schema/tei_color.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="../schema/tei_color.rng" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
</teiHeader>
<text xml:id="Z_1822-02-20_k" xml:lang="ger">
<body>
<div>
<div rendition="simple:half-broken">
<pb n="46r" facs="#Z_1822-02-20_k_A_0001"/>
<cb rendition="simple:column-left"/>
</div>
<div>
<cb rendition="simple:column-right"/>
<p>
<handShift scribeRef="#Z_1822-02-20_k_scrb_1_t"/>Auf
<pb n="46v" facs="#Z_1822-02-20_k_A_0002"/>
<hi rendition="simple:italic">Heliostaten</hi> von dem Herrn Geheimen<lb/>Post-Rath
</p>
</div>
</div>
</body>
</text>
</TEI>
使用 XPath 如何获取 pb 元素,包括其下的 xml 内容。
解决方案
根据您使用 xpath 的语言,您可以使用 2 种不同的 XPath,如下所示:
//pb
和
//pb/following-sibling::*
或者,如果您可以在变量中重用 //pb,只需使用通过 XPath 扩展的变量:
./following-sibling::*
或使用联合运算符合二为一|
:
//pb | //pb/following-sibling::*
如果其下的 xml 内容可能只是纯文本,则应使用:
//pb | //pb/following-sibling::node()
将node()
选择所有节点,包括text()
-nodes 和element()
-nodes。有关 node() 的更多信息,请参阅此答案。
推荐阅读
- r - 设置种子时,示例函数在控制台和针织文档中给出不同的结果
- javascript - 图像导入问题以及在部署后使用 React Router 和 GitHub Pages 应用 CSS 的顺序
- apache - 为什么 .htaccess 忽略“RewriteCond %{HTTPS} !on [NC]”?
- python - 根据其他数组上的元素条件从 Numpy 数组元素中进行选择
- java - 多个动作侦听器的单独类
- scipy - 使用 scipy.interpolate.Rbf 对 3D 常规网格数据进行插值
- excel - 打开没有标准名称的文件
- javascript - 如何将索引作为参数添加到 useEffect 挂钩?
- c++ - C++ 语言标准是否允许空宏?
- vue.js - 位于中间件中的等待请求在呈现页面之前完成