Question

Commandline xpath query

Asked by: mreuring

I'm writing an AppleScript action for iTunes that needs to query some information from an XML file, which isn't available natively :(. Currently I'm using 'xmllib' from http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html to run xpath queries in applescript. But I wan't to write this script without any 3rd-party dependencies, if at all possible and for this part of the process that I'm automating, XMLLib is the only 3rd party dependency.

So, now I was wondering if I could use any of the built-in (pre-compiled) tools that will run from commandline to query an xml-file using xpath. Considdering an xpath that returns a string, '//Series[1]/id[1]/text()', this would be quite acceptable to me and would negate the need for installing the above-mentioned scripting plugin. For instance I was wondering if Perl or PHP could be abused to do this for me, I've seen similar tricks somewhen/where but I have hardly any experience with PHP and 0 with Perl, nor is Bash-scripting among my strengths :)

Requirements would be for this commandline tool to be available on OSX.4 running on a PowerPC, since my media-server is an old G4 that doesn't run any later version :)

FYI, the AppleScript action, when 'finished', will be opensourced.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-07-21 at 10:55:05ID24588312
Tags

perl

,

bash

,

OSX

,

xpath

,

applescript

Topics

Mac OS X

,

Perl Programming Language

,

Bourne-Again Shell (bash)

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. monitor plugins not loading on G4 733
    I have a G4 733 with a 15 inch flat panel display. I keep getting a message saying Apple Monitor Plugins did not load completely, error 6911 at start up. Monitor works fine other than the USB ports on the back of display do not work. I only use this computer for recording aud...
  2. Saving an XML file from XPath (perl)
    Hi Is it possible to save a modifed XMl file in Perl's XPath? I have a script that looks for blank id attributes in an xml file and populates them with a unique ID... Am I on the right track? How do I save the XML? many thanks in advance! Lucas
  3. What is DOM and XPATH
    What is the difference between DOM and XPATH? Are the similar? Is XPATH part of DOM? If I have an http request that returns me an XML file, how do I go about parsing the infomration being returned? Is that done through DOM or XPATH. Can I use a perl script for that?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Adam314Posted on 2009-07-21 at 11:02:32ID: 24907477

If you post a sample of the XML file, and what you'd like from it, we may be able to get you something.

 

by: mreuringPosted on 2009-07-21 at 11:14:50ID: 24907611

Alright, I've posted a partial of one of the xml-files I'm working with.

From this file I'm pulling:
"//Episode[DVD_episodenumber=" & (episode of myEpisodeInfo) & "][SeasonNumber=" & (season of myEpisodeInfo) & "]"
And from the resulting node I pull:
"/Overview/text()"
"/EpisodeName/text()"
"/FirstAired/text()"
Then for genre I jump back to the root for this:
"//Series[1]/Genre[1]/text()"

I was thinking of grabbing all the info in individual queries, instead of grabbing the node first. This would be less efficient, but I cannot conceive of how to return multiple results from a shell-command back to AppleScript... Anyways, I have a reasonable grasp of xpath, it's getting the result without 3rd-party patching that eludes me :)

<?xml version="1.0" encoding="UTF-8" ?>
<Data><Series>
  <id>75682</id>
  <Actors>|Emily Deschanel|David Boreanaz|T.J. Thyne|John Francis Daley|Michaela Conlin|Tamara Taylor|Eric Millegan|Jonathan Adams|</Actors>
  <Genre>|Drama|</Genre>
  <IMDB_ID>tt0460627</IMDB_ID>
  <Language>en</Language>
  <Network>FOX</Network>
  <NetworkID></NetworkID>
  <Overview>Lorem Ipsum</Overview>
  <Rating>8.6</Rating>
  <SeriesID>33332</SeriesID>
  <fanart>fanart/original/75682-2.jpg</fanart>
</Series>
<Episode>
  <id>298561</id>
  <Combined_episodenumber>2</Combined_episodenumber>
  <Combined_season>1</Combined_season>
  <DVD_chapter></DVD_chapter>
  <DVD_discid></DVD_discid>
  <DVD_episodenumber></DVD_episodenumber>
  <DVD_season></DVD_season>
  <Director>Allan Kroeker</Director>
  <EpImgFlag></EpImgFlag>
  <EpisodeName>The Man In The SUV</EpisodeName>
  <EpisodeNumber>2</EpisodeNumber>
  <FirstAired>2005-09-20</FirstAired>
  <GuestStars>|Jose Zuniga|Nicholas Massouh|Ane Dudek|Bahar Soomekh|Federico Dordei|Said Faraj|Dave Roberson|Tracy Howe|</GuestStars>
  <IMDB_ID></IMDB_ID>
  <Language>en</Language>
  <Overview>Lorum Ipsum</Overview>
  <ProductionCode>1AKY02</ProductionCode>
  <Rating>7.5</Rating>
  <SeasonNumber>1</SeasonNumber>
  <Writer>Stephen Nathan</Writer>
  <absolute_number></absolute_number>
  <filename>episodes/75682/298561.jpg</filename>
  <lastupdated>1231598855</lastupdated>
  <seasonid>9191</seasonid>
  <seriesid>75682</seriesid>
</Episode>
</Data>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:

Select allOpen in new window

 

by: mreuringPosted on 2009-07-21 at 11:16:49ID: 24907637

I just noticed that I posted the less common query for getting the 'episode' node, most of the time I'd be searching based on EpisodeNumber instead:
"//Episode[EpisodeNumber=" & (episode of myEpisodeInfo) & "][SeasonNumber=" & (season of myEpisodeInfo) & "]"

 

by: Adam314Posted on 2009-08-22 at 17:50:57ID: 25160911

I'm not familiar with the xpath, or how it relates to the XML file.  I was planning on looking into this so I could help some more, but haven't had time.

If you summarize what you want, I can help you get it.

 

by: mreuringPosted on 2009-08-23 at 02:28:15ID: 25161950

Alright, I'll try to start with a quick summary of what xpath does:
xpath can be used to search for, and retreive, information from an xml-file in  a way which is quite similar to a 'path' in any shell (DOS, Unix, etc). With it you can return entire nodes/elements or even go as detailed as a single attribute. Also, by using advanced xpath constructions, you can select based on attributes.

In my particular case I have an xml-document which is filled with episodes for a particular (tv)series and I am querying it for one particular episode. After retreiving the episode I need query the resulting element for information regarding its' title, description and possibly some other info when I want to expand the script.
Also I'm retreiving some information from the 'Series' node regarding the Genre of the series, which in iTunes needs to be stored for every single 'track'.

The reason I was suggesting perl is because I know it's possible to write commandline 'tools' with it, it's installed by default since OSX.4 and it has an xml/xpath api installed. Problem is, I've never written anything in perl so I don't know how to do it myself, while it sounds like it could be a rather easy thing to write (in java it wouldn't take me very long to write it and I've been considdering to do so). All that the tool needs to do is recieve the path to an xml file and an xpath query, process it and then return the result as a string.

 

by: mreuringPosted on 2009-08-23 at 02:34:35ID: 25161966

Adam314 still seems interested in bringing this issue to a conclusion, since this part of my project is not in any time-pressure it has not been resolved through other means and I would like to await Adam's reply.

 

by: mreuringPosted on 2009-08-24 at 04:29:44ID: 25167269

Ok, so I had another short inspirational session of googling and came across 'http://uszla.me.uk/space/blog/2007/09/21', using xmllint to run an xpath query against some xml file. I adapted it a little to more closely match my needs and have something that works. It ain't pretty, if you ask my opinion, but it works. Also, xmllint comes installed on an osx.4 box, at least one that has been regularly updated.

Since this still ain't the pretty solution I was hoping for I'm still holding out for a custom script using perl or ruby or whatever can be used to make some elegant script out of... But in case it doesn't happen, I have something that works well enough.

-- *
-- xpath query using xmllint shell mode. This method throws the xpath query at xmllint using an echo
-- statement. Since this tool lives on tiger as well as leopard it is a relatively 'safe' method of
-- running an xpath query from applescript. Currently this method only works on queries that select
-- a single node!
-- The result of the shellCommand is trimmed and set to an empty string if the result started with the
-- query (which indicates it failed or multiple nodes were part of the selection).
-- For the idea of creating this method I would refer to: http://uszla.me.uk/space/blog/2007/09/21
--
-- @parameter query A String containing the xpath query you want to use
-- @parameter source A String containing the xml, or a POSIX path to the xml-file you want to query
-- @parameter fromFile A class set to 'file' if the source is a file, otherwise a file is create and source is stored in that file.
-- */
on xpath for query against source from fromFile
	set shellPipe to "|"
	set shellEcho to "echo \"cd " & query & NL & "cat\""
	set shellCommand to "/usr/bin/xmllint --noent --shell "
	set shellCleanup to "sed 's/^[/a-zA-Z][^<]*>//g'"
	set shellCaptureError to " 2>&1" -- For capturing the errorout
	
	-- If source is not from a file, create a temp file...	
	if fromFile is file then
		set filePath to quoted form of POSIX path of source
	else
		set theFilePath to TEMP_FILE
		set filePath to quoted form of POSIX path of theFilePath
		try
			do shell script "/bin/rm " & filePath
		on error
			--Ignore it, no need to worry...
		end try
		set theFileReference to open for access theFilePath with write permission
		write source to theFileReference
		close access theFileReference
	end if
	
	set shellScript to (shellEcho & shellPipe & shellCommand & filePath & shellCaptureError & shellPipe & shellCleanup)
	set shellResult to (do shell script shellScript)
	
	--If the result starts with the query, something went wrong...
	if (shellResult contains query) then
		-- display dialog (shellScript & NL & "'" & trim(true, shellResult) & "'")
		set shellResult to ""
	else
		set shellResult to trim(true, shellResult)
	end if
	
	return shellResult
end xpath
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:

Select allOpen in new window

 

by: Adam314Posted on 2009-08-27 at 09:00:37ID: 25199741

Here is a perl script that will display the requested parameters.  If you'd prefer the output in a different format, let me know.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
 
my $xml = XMLin('file1.xml');
 
foreach (qw(EpisodeNumber SeasonNumber Overview EpisodeName FirstAired)) {
	print "$_=$xml->{Episode}->{$_}\n";
}
print "Series Genre=$xml->{Series}->{Genre}\n";
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: mreuringPosted on 2009-08-30 at 02:30:55ID: 25216918

I'll test it out and let you know, I'll probably be able to tweak the output, I just didn't know anything about building a perl script. Like I said, I'll get back to you!

 

by: Adam314Posted on 2009-08-31 at 10:02:45ID: 25224396

No problem.  The XML::Simple module does all of the reading of the XML file.  To see the structure of the xml once it has been read,you can use:
    print Dumper($xml);

If you have any more questions, let me know.

 

by: mreuringPosted on 2009-09-09 at 01:36:56ID: 31606037

Since this perl script is by far more tweakable and simple than what I have I'm gonna go with it and will probably adapt it to more closely match my needs as I go along :) Thanx!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...