Home > Net >  Extract XML with xmllint --xpath
Extract XML with xmllint --xpath

Time:07-05

I am having trouble extracting the "EXTRACT_THIS_PLEASE" from a similar XML file using xmllint --xpath. I understand sed and awk should not be used from some Googling. I also see that other XML parsers are usually recommended, but this is the only one I seem to have on my RHEL system. I have tried various things and understand that the issue has to do with white spaces.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <model-response-list xmlns="http://www.website.com/thing/link/linktothing/linklink" total-models="1" throttle="1" error="EndOfResults">
        <model-responses>
            <model mh="0x12345678">
                <attribute id="0x12345">EXTRACT_THIS_PLEASE</attribute>
            </model>
        </model-responses>
    </model-response-list>

EDIT: kjhughes and j_b, you guys are both wizards. Thank you so much. Could I also also extract 0x12345678 from "". I am looking to do this 5000 times and ultimately have a list of devices in rows or columns like this:

"0x12345678
EXTRACT_THIS_PLEASE
0x99999999
EXTRACT_THIS_PLEASE
0x11111111
NOTHING
0x33333333
EXTRACT_THIS_PLEASE
0x22222222
NOTHING"

CodePudding user response:

This xmllint command line,

xmllint --xpath "//*[@id='0x12345']/text()" file.xml

will select

EXTRACT_THIS_PLEASE

as requested.

See also

CodePudding user response:

Another option to extract the contents of the <attribute> elemenet:

xmllint --xpath "//*[name()='attribute']/text()" x.xml

Output:

EXTRACT_THIS_PLEASE
  • Related