Bash XML Parsing using Perl XPath | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Bash XML Parsing using Perl XPath

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-05-2011
jfmorales jfmorales is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 19 July 2011, 5:27 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Question Bash XML Parsing using Perl XPath

I have a bash script that needs to read input from an XML file, which includes varying numbers of a certain type of child node. I want to be able to iterate through all the child nodes of a given parent. I installed the Perl XML-XPath package from
search.cpan.org. Once it's installed, from bash, we can do queries like

xpath -e "//ConfigurationData/DataItem/ClassInstances/ClassInstance[1]" input.xml

This query returns the first ClassInstance node in this path. However, I don't know how to query how many nodes there of this type, or how to step through them one at a time.

Googling around, I found references to a number count() function and an fn.count() function, but couldn't get either to work inside an xpath command called from bash.

Any suggestions? Thanks!
Sponsored Links
    #2  
Old 01-05-2011
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 22 July 2014, 12:11 AM EDT
Location: /dev/ph
Posts: 4,770
Thanks: 59
Thanked 422 Times in 391 Posts
Please supply a short example of your XML file.
Sponsored Links
    #3  
Old 01-05-2011
jfmorales jfmorales is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 19 July 2011, 5:27 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
The XML file is still being designed, so we have some flexibility there. I expect it to include collections with a varying number of members such as

Code:
<breads>
  <bread>
      <type>sourdough</type>
  </bread>
  <bread>
      <type>pumpernickel</type>
  </bread>
</breads>

Thanks.

Last edited by radoulov; 01-05-2011 at 04:05 PM.. Reason: Code tags!
    #4  
Old 01-05-2011
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 22 July 2014, 12:11 AM EDT
Location: /dev/ph
Posts: 4,770
Thanks: 59
Thanked 422 Times in 391 Posts
My version of Perl xpath does not have a -e option and the command line syntax appears to be different. However you should be able to get the idea.

To retrieve the count of the bread nodes:

Code:
$ xpath input.xml "count(//bread)"  2>/dev/null
2
$

To retrieve the contents of the first type node:

Code:
$ xpath input.xml "//bread[position() = 1]/type/text()" 2>/dev/null
sourdough
$

To retrieve the contents of the second type node:

Code:
$ xpath input.xml "//bread[position() = 2]/type/text()" 2>/dev/null
pumpernickel
$

The Following User Says Thank You to fpmurphy For This Useful Post:
jfmorales (01-06-2011)
Sponsored Links
    #5  
Old 01-06-2011
jfmorales jfmorales is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 19 July 2011, 5:27 PM EDT
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Bug Solved

Thanks, fpmurphy! Your example worked perfectly, with only a slight adjustment for the differing syntax of xpath on my system:

xpath -e "count(//bread)" input.xml 2>/dev/null

The 2>/dev/null that you included at the end is particularly nice as it suppresses some unnecessary text from the reply.

Joseph
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help parsing command line arguments in bash Breakology Shell Programming and Scripting 3 07-14-2009 06:00 PM
Perl parsing compared to Ksh parsing popeye Shell Programming and Scripting 1 08-06-2008 10:46 PM
Parsing Directory Names for Use as Bash Variables aefskysa Shell Programming and Scripting 6 07-26-2008 10:39 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
Conversion of bash parsing script to perl? cstovall Shell Programming and Scripting 2 10-13-2004 10:33 PM



All times are GMT -4. The time now is 12:12 AM.