Extracting and dumping elements using xmlstarlet

前端 未结 1 373
悲&欢浪女
悲&欢浪女 2020-12-05 11:34

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



        
相关标签:
1条回答
  • 2020-12-05 12:09

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

    0 讨论(0)
提交回复
热议问题