[关闭]
@haokuixi 2015-05-17T18:57:01.000000Z 字数 806 阅读 9365

使用 XPath 选取有特定文本值的节点

xpath


要解决的问题:
从 xml 文件中选取有某个特定文本值的节点,比如说我要处理的是 plist 文件,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>AppIDName</key>
  6. <string>chineseidublin</string>
  7. <key>ApplicationIdentifierPrefix</key>
  8. <array>
  9. <string>CS8M2QZ3L3</string>
  10. </array>
  11. <key>TimeToLive</key>
  12. <integer>364</integer>
  13. <key>Version</key>
  14. <integer>1</integer>
  15. </dict>
  16. </plist>

key=AppIDName,我要提取它所对应的 string value,即 chineseidublin,那么
1. 定位 key 节点 N
2. 取节点 N 后面的第一个节点

所使用的 XPath 表达式:

//key[text()='AppIDName']/following::*[1]

PS:
上面的表达式对于 XPath 中的各个知识点都有涉及:


参考:
1. XPath 教程 | W3School
2. XML Path Language (XPath) 2.0 (Second Edition)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注