Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Extracting and dumping elements using xmlstarlet

I am looking for a way to extract and print an element from my xml using xmlstarlet; for example if my xml is

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

I would like to print out book element with price = 29.99 as:

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

I understand the xpath query to select such an element (/bookstore/book[price=29.99]) but I am not able to print/dump it on stdout. If I use the '-v' option and use -v (.) I don't get the output as I want (with all the tags in it) I just get the text values. There ought to be a way to simply dump selected element as it is, and that's what I am looking for.

Thanks in anticipation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the "-c" (copy) option, should achieve what you're after:

xmlstarlet sel -t -c "/bookstore/book[price=29.99]" books.xml

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

You can watch the XSLT generated internally in xmlstarlet by adding the global "-C" switch after "sel". This shows how the copy option results in an xslt copy-of construct:

...
<xsl:template name="t1">
  <xsl:copy-of select="/bookstore/book[price=29.99]"/>
</xsl:template>
...

This results in namespace nodes, child nodes, and attributes nodes being included, cf. the XSLT spec (see w3schools summary).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...