xml - xmlstarlet 根据属性选择值
问题描述
使用以下 file.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config>
<index type="I8">
<book>2</book>
</index>
</config>
我无法选择书
xmlstarlet sel --template --match /config/index[@type="I8"] -c . file.xml
我无法选择书
xmlstarlet sel --template --match /config/index[@type='I8'] -c . file.xml
我可以选择书
xmlstarlet sel --template --match "/config/index[@type='I8']" -c . file.xml
我可以选择书
xmlstarlet sel --template --match '/config/index[@type="I8"]' -c . file.xml
此外,如果 xml 中的 type 为 type="8",我可以选择它:
xmlstarlet sel --template --match /config/index[@type="8"] -c . file.xml
为什么?
xmlstarlet 1.6.1
compiled against libxml2 2.9.4, linked with 20904
compiled against libxslt 1.1.29, linked with 10129
解决方案
这个有效(在 Windows 和 Ubuntu 上测试):
xmlstarlet sel -t -i /config/index/@type=\"I8\" -m //book -c . -b file.xml
它与引用有关,您可以在查看下一条语句的输出时了解原因:
xmlstarlet sel -C --template --match /config/index[@type="I8"] -c . file.xml
它的输出:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="/config/index[@type=I8]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
缺少引号I8
。
推荐阅读
- java - 如何修复 Android Studio 中的 ''module java.base does not "opens java.io" to unnamed module '' 错误?
- r - 将类“perm.htest”对象从(多重排列 t 测试)转换为数据框
- node.js - 是否可以使用 Twilio whatsapp API 发送广播消息
- node.js - 我的 Discord 机器人正在持续发送消息,即使我将它编程为只发送一次
- lumen - 为什么我的邮递员永远挂在 GuzzleHttp\Client 请求中?
- c - 如何在 MISRAC-2004-complaint C 中实现 modbus RTU?
- algorithm - 从集合中挑选项目的算法
- sed - 如何使用 sed 仅替换文件中的特定空格?
- c - 为什么执行此程序时显示“(null)”?
- node.js - 2个节点机器人合二为一?