1

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"
Musik847
  • 21
  • 5

2 Answers2

2

This xmllint command line,

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

will select

EXTRACT_THIS_PLEASE

as requested.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 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 EXTRACT_THIS_PLEASE 0x22222222 NOTHING – Musik847 Jul 04 '22 at 11:53
  • @Musik847 per your follow-up question, you may wish to have a look into XSLT transform to produce the output you describe. – j_b Jul 05 '22 at 02:41
1

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

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

Output:

EXTRACT_THIS_PLEASE
j_b
  • 1,975
  • 3
  • 8
  • 14