打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[原]linux 命令行下的 XML 处理利器 xmlstarlet 学习
长久以来,在shell 下处理xml一直是楼主头疼的问题,用 regex 匹配不是不可以,但对于复杂的需求就有点捉襟见肘了,例如这样一个问题 :查找某个元素值 item 的值等于value的节点,并把这个节点的另外一个元素值 item2 的值替换为另外一个值 value2。
对于这种需求,用 regex (sed) 处理起来很是痛苦,即便你用了 sed 的 pattern space ,也是很麻烦,光日后维护都成问题了。
昨天找了一下,有几个候选的
xmllint
xml-coreutils
xmlstarlet
xmllint 太老了,而且文档比较少,就一个 manual ,不太敢用。下面先说 xml-coreutils
这个东西 manual 和文档倒还算齐全。是模拟 coreutils 包的思路,具体可以看这里。有 xml-ls、xml-cat 等命令,看起来还不错,不过用起来就是另外一回事了,最大的问题就是 Xpath 的支持不到位 ,很多功能都没有 :不支持属性、不支持条件、不支持函数,而且对于大文件貌似处理有问题,经常报错。所以最后放弃了,也不建议大家使用。例如下面的 xml ,xml-coreutils 不支持 “/bookstore/book@category ” 这种 Xpath,也就是无法识别出 category 这个属性。。。
books.xml
Shell
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
下面正式介绍 xmlstarlet 这个东东。xmlstarlet (官方站点点这里) 跟 xml-coreutils 不同,它并不是按照 coreutils 那种风格,分成多个小工具的方式,而是一个大的程序,通过不同的子命令来实现不同的功能。这个还只是小区别,主要是 xmlstarlet 对 Xpath 规范的支持相当到位(至少我认为我日常需要的大部分的功能都满足,没有想到的也提供了),这个才是 xmlstarlet 跟 xml-coreutils 的最大区别。
XMLStarlet is a set of command line utilities (tools) which can be used to transform, query, validate, and edit XML documents and files using simple set of shell commands in similar way it is done for plain text files using UNIX grep, sed, awk, diff, patch, join, etc commands.
This set of command line utilities can be used by those who deal with many XML documents on UNIX shell command prompt as well as for automated XML processing with shell scripts.
The toolkit’s feature set includes options to:
Check or validate XML files (simple well-formedness check, DTD, XSD, RelaxNG)
Calculate values of XPath expressions on XML files (such as running sums, etc)
Search XML files for matches to given XPath expressions
Apply XSLT stylesheets to XML documents (including EXSLT support, and passing parameters to stylesheets)
Query XML documents (ex. query for value of some elements of attributes, sorting, etc)
Modify or edit XML documents (ex. delete some elements)
Format or “beautify” XML documents (as changing indentation, etc)
Fetch XML documents using http:// or ftp:// URLs
Browse tree structure of XML documents (in similar way to ‘ls’ command for directories)
Include one XML document into another using XInclude
XML c14n canonicalization
Escape/unescape special XML characters in input text
Print directory as XML document
Convert XML into PYX format (based on ESIS – ISO 8879), and vice versa
XMLStarlet command line utility is written in C and uses libxml2 and libxslt from http://xmlsoft.org/.
Implementation of extensive choice of options for XMLStarlet utility was only possible because of rich feature set of libxml2 and libxslt (many thanks to the developers of those libraries for great work).
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
install xmlstarlet 1.6.1
sed 使用变量替换和执行外部命令
shell脚本regex正则表达式 和 字符串替换
Dom4j的使用(全而好的文章) - xhy0422 - JavaEye技术网站
关于Shell脚本效率优化的一些个人想法三江小渡 | 三江小渡
linux系统安全shell脚本第二版
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服