simpatico
simpatico

Reputation: 11107

How could I simply this xpath so that xmlstartlet works with it?

$ 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>

Upvotes: 0

Views: 565

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

Are you trying to return the 6 from 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 str element that has a name attribute with a value of doc-norm when its parent element has a child str element that has an id attribute with a value of http://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 str element. If you want the text only, add text() to the end of the XPath:

//*[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']/text()

Upvotes: 2

Related Questions