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

Use a ComboBox Value as part of an SQL Query? (MS Access 2010-2013)

sql ms-access

I have a Database that we use to create Bills of Materials from Tags in AutoCAD. Because of the nature of this, I need to create 3 separate queries. One for our "Steel", one for our

Lock a CSV file (only allow read-access). Possible?

java applet locking persistence file-locking

I'm programming a game applet that saves data to a CSV file and got a few methods that may read and write to same .csv file. And somehow I need to lock the file while each of my methods has a open ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.