|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Please supply a short example of your XML file.
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|