日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

[原]linux 命令行下的 XML 處理利器 xmlstarlet 學習

 浸心閣 2015-09-02

長久以來,在shell 下處理xml一直是樓主頭疼的問題,用 regex 匹配不是不可以,但對于復雜的需求就有點捉襟見肘了,例如這樣一個問題 :查找某個元素值 item 的值等于value的節(jié)點,并把這個節(jié)點的另外一個元素值 item2 的值替換為另外一個值 value2。

對于這種需求,用 regex (sed) 處理起來很是痛苦,即便你用了 sed 的 pattern space ,也是很麻煩,光日后維護都成問題了。

昨天找了一下,有幾個候選的

  • xmllint
  • xml-coreutils
  • xmlstarlet

xmllint 太老了,而且文檔比較少,就一個 manual ,不太敢用。下面先說 xml-coreutils

這個東西 manual 和文檔倒還算齊全。是模擬 coreutils 包的思路,具體可以看這里有 xml-ls、xml-cat 等命令,看起來還不錯,不過用起來就是另外一回事了,最大的問題就是 Xpath 的支持不到位 ,很多功能都沒有 :不支持屬性、不支持條件、不支持函數(shù),而且對于大文件貌似處理有問題,經(jīng)常報錯。所以最后放棄了,也不建議大家使用。例如下面的 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 那種風格,分成多個小工具的方式,而是一個大的程序,通過不同的子命令來實現(xiàn)不同的功能。這個還只是小區(qū)別,主要是 xmlstarlet 對 Xpath 規(guī)范的支持相當?shù)轿?至少我認為我日常需要的大部分的功能都滿足,沒有想到的也提供了),這個才是 xmlstarlet 跟 xml-coreutils 的最大區(qū)別。

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:///.
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).

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多