Search code examples
xmlxmlstarlet

XML starlet, how to do the equivalent of a "join"?


I have an XML file that could be summarized like this

<xml>
    <lookfor key="boat"></lookfor>
    <content key="car" value="ford"></content>
    <content key="car" value="GM"></content>
    <content key="boat" value="catamaran"></content>        
    <content key="boat" value="freighter"></content>        
    <content key="plane" value="boeing"></content>
    <content key="plane" value="airbus"></content>
</xml>

And I'd like to find the xml starlet command that prints freighter and catamaran but based on the fact that their key matches the lookfor key (not based on the fact that I know beforehand that their key is boat).


Solution

  • xmlstarlet sel -T -t -v '/xml/content[@key = /xml/lookfor/@key]/@value' -n vehicles.xml