I need with xmlstarlet to filter the content of a XML and export elements based on subelement criteria
Using this kind of xml:
<products>
<product>
<product_code>1-70-032-0023</product_code>
<availability>criteria_i_need</availability>
<quantity>393</quantity>
</product>
<product>
<product_code>1-70-032-0024</product_code>
<availability>criteria_i_need</availability>
<quantity>19</quantity>
</product>
<product>
<product_code>1-70-032-0046</product_code>
<availability>criteria_i_do_NOT_need</availability>
<quantity>351</quantity>
</product>
</products>
using the command:
xmlstarlet ed -d '/product/availability[. = "criteria_i_need"]' input-file.xml > filtered-file.xml
but the output file contains ALL elements and not only the 2 firsts that match the criteria.
It's not entirely clear what you expect filtered-file.xml
to contain, but since you used ed -d
as your argument, I assume you want to delete the one <product>
that does not meet your criteria and save the rest.
So I would change your command to
xmlstarlet ed -d '//product[not(.//availability[. = "criteria_i_need"])]' input-file.xml > filtered-file.xml
and see if it works.
Alternatively you could delete on a specfiic criteria, e.g.:
xmlstarlet ed -d 'products/product[availability = "criteria_i_do_NOT_need"]'
or with contains()
:
xmlstarlet ed -d 'products/product[contains(availability, "_NOT_")]'