$ xmlstarlet sel -t -c "//str[@name="id" and .="http://localhost:8080/index.html"]/../str[@name="doc-norm"]"/value results.xml
My understanding is that xmlstarlet doesn’t fully support xpath expressions. Is there any other command-line tool that does BTW?
<doc>
<str name="id">http://localhost:8080/index.html</str>
<str name="doc-norm">6</str>
</doc>
Are you trying to return the
6from your example?I don’t have xmlstarlet to test this on, but try this XPath:
//*[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']This should return the value of a
strelement that has anameattribute with a value ofdoc-normwhen its parent element has a childstrelement that has anidattribute with a value ofhttp://localhost:8080/index.html. (I hope that makes sense!)I should also add that if you know what the level of the parent element is, try to avoid using the
//. Something like/doc[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']would be more efficient.UPDATE
I downloaded xmlstartlet to test and it works fine, however it returns the entire
strelement. If you want the text only, addtext()to the end of the XPath://*[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']/text()