Posted on 05-22-2018 11:11 AM
I'm trying to parse some XML output (a smart group) from the JSS API using xpath
. The output is pretty standard stuff, a list of computers and any outstanding software updates on them. Let's save the entire file in a file ~/Desktop/whywontthiswork.xml
.
In it are sections like this for each computer:
<Available_SWUs>
<Available_SWU>
<Available_Update>Update 1</Available_Update>
</Available_SWU>
<Available_SWU>
<Available_Update>Update 2</Available_Update>
</Available_SWU>
<Available_SWU>
<Available_Update>Update 3</Available_Update>
</Available_SWU>
<Available_SWU>
<Available_Update>Update 4</Available_Update>
</Available_SWU>
</Available_SWUs>
If I go at it with xpath
to get at all the <Available_Update>
elements I might use:
xpath ~/Desktop/whywontthiswork.xml '/advanced_computer_search/computers/computer/Available_SWUs/Available_SWU/Available_Update'
or even:
xpath ~/Desktop/whywontthiswork.xml '//Available_Update'
if I'm feeling lazy. Both of these will give me:
<Available_Update>Update 1</Available_Update>-- NODE --
<Available_Update>Update 2</Available_Update>-- NODE --
<Available_Update>Update 3</Available_Update>-- NODE --
<Available_Update>Update 4</Available_Update>-- NODE --
That's nice, but not terribly pretty.
Now comes the annoying bit. I BELIEVE xpath
should remove the tags for me if I append ()
(a bit of sed
trickery to gets rid of the -- NODE --
bits).
What I've been seeing is a line similar to (see here):
xpath ~/Desktop/whywontthiswork.xml '/advanced_computer_search/computers/computer/Available_SWUs/Available_SWU/Available_Update()' 2>&1 | sed 's/-- NODE --//g'
which, by all accounts, SHOULD return:
Update 1
Update 2
Update 3
Update 4
but instead taunts me with:
Parse of expression /advanced_computer_search/computers/computer/Available_SWUs/Available_SWU/Available_Update() failed - junk after end of expression: ( at /System/Library/Perl/Extras/5.18/XML/XPath/Parser.pm line 127.
which is annoying.
Posted on 05-22-2018 07:13 PM
I think you're just missing the "/text" between /Available_Update and () - try:
xpath ~/Desktop/whywontthiswork.xml '/advanced_computer_search/computers/computer/Available_SWUs/Available_SWU/Available_Update/text()' 2>&1 | sed 's/-- NODE --//g'
Posted on 05-22-2018 09:32 PM
Well, the ophthalmologist did say I needed reading glasses...
I missed that. Reading the same path for half an hour, my brain must have decided to filter it out.
Thank you.
~W