Since I could not find an explained example elsewhere, I share my findings as a Q&A.
Consider this list of pupils (pupils.xml): 
<pupils>
    <pupil>
        <firstName>Adam</firstName>
        <lastName>Amith</lastName>
        <birthDate>2000-01-01</birthDate>
    </pupil>
    <pupil>
        <firstName>Berta</firstName>
        <lastName>Bmith</lastName>
        <birthDate>2000-01-02</birthDate>
    </pupil>
    <pupil>
        <firstName>Caesar</firstName>
        <lastName>Cmith</lastName>
        <birthDate>2000-01-03</birthDate>
    </pupil>
    <pupil>
        <firstName>Doris</firstName>
        <lastName>Dmith</lastName>
        <birthDate>2000-01-04</birthDate>
    </pupil>
</pupils>
How can I select two subattributes of each pupil to get a list like this:
Adam Amith
Berta Bmith
Caesar Cmith
Doris Dmith
                The command to get this is
xmlstarlet select -T -t -m "//pupil" -v "firstName" -o " " -v "lastName" -n pupils.xml
and brings:
Adam Amith
Berta Bmith
Caesar Cmith
Doris Dmith
-T switches the output to text-t -m "//pupil" -v "firstName" -o " " -v "lastName" -n defines a template
-m "//pupil" matches every pupil node-v "firstName" prints the value of the firstName subnode (subnode of currently matched node)-o " " prints a blank-v "lastName" (see above)-n prints a newlinepupils.xml provides the input fileThe documentation provides even more advanced parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With