What is causing “junk after end of expression” error from xpath command?

Being far short of expert at XML, I've been going through basic tutorials. There is this one on xpath. Luckily, my Linux setup has an 'xpath' command which works fine, until I get to attributes.

xpath -e 'inventory/snack/chips@supplier'  lemonade2.xml 

gives the error

Parse of expression inventory/snack/chips@supplier failed - junk after end of expression: @supplier at /usr/share/perl5/XML/XPath/Parser.pm line 127.

My lemonade2.xml is copy-pasted straight from the site. xpath works fine without the '@supplier'.

Maybe there's some subtle missing character or something, or I've fat-fingered xpath into ruin somehow. Maybe the tutorial is very old and uses obsolete xpath syntax? Nah, that would break a lot of existing code, wouldn't it? Could it be interference with bash syntax? What am I doing wrong?

Answers


Accessing attributes is a path step in XPath, you're missing the slash in front of the attribute.

xpath -e 'inventory/snack/chips/@supplier'  lemonade2.xml 

Is <inventory/> the outermost element? Then you might also want to declare the root in front of it:

xpath -e '/inventory/snack/chips@supplier'  lemonade2.xml 

Need Your Help

Changing color of scroll bar in ListView in WinForms

c# .net winforms listview scrollbar

Is it possible to change the color of the scroll bar in a ListView control. It is a Windows application.