Script for showing only selected nodes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for showing only selected nodes
# 1  
Old 11-17-2009
Script for showing only selected nodes

Dear all,

I am bit confused lately, I have a xmlfile here:

file: book.xml

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

I want, if I type query="CHILDREN", I want my script shows output like this :

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>

I tried find solution using XML DOM (W3Schools Online Web Tutorials), but it seems cannot shows fully (a block) path and attribut related to my query.

It can be solved using shell script?

Your answer really appreciated,.
Thanks
Denny
# 2  
Old 11-17-2009
try:

Code:
awk 'BEGIN {print "<?xml version="1.0" encoding="ISO-8859-1"?>"; print "<bookstore>";  } /CHILDREN/,/\/book/ { print; } END { print "</bookstore>"; } ' < inp_file >out_file

# 3  
Old 11-17-2009
Quote:
Originally Posted by dennis.jacob
try:

Code:
awk 'BEGIN {print "<?xml version="1.0" encoding="ISO-8859-1"?>"; print "<bookstore>";  } /CHILDREN/,/\/book/ { print; } END { print "</bookstore>"; } ' < inp_file >out_file


Hi Dennis, thank you for replying.Smilie
I think your script will useful if we know how deep the tree of the xml file.
is it possible to parse more complex xml file with unknown deep of path tree?
example:
Code:
<bookstore>
 <path1>
  <path11>
    <.....>
      .
      .
      .
         <book category="COOKING">
           <title lang="en">Everyday Italian</title>
           <author>Giada De Laurentiis</author>
           <year>2005</year>
           <price>30.00</price>
         </book>
      .
      .
      .
    </.....>
  </path11>
 </path1>
</bookstore>

Thank you Dennis,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Interacitve script for Healthcheckup of nodes

Hi This is my first post to this site.:D I want to develop a script with following steps: 1.Script1,2,3 placed at particular path on local server 2.ask for an IP and will directly pick up the correct script matching to the IP (IP could be defined in same script or a new script could be called).... (10 Replies)
Discussion started by: veronica
10 Replies

2. Shell Programming and Scripting

Tabulate nodes and subnodes of XML file script

Hello to all, I'd like to tabulate the content of the XML file below, I've been trying with AWK putting the Top node (<KIND>) as RS and so far I'm able to print the first and second field of each record, but I don't know how to continue with the following nodes that have more inner nodes. The... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

3. Shell Programming and Scripting

How to pass enter key or selected character in bash script?

hi, i've bash script thats working... but now i need to add a line....that prompts for user input....like yes and 1 as complete install.... so here's how it looks... $ cd 9200 (cd into directory) $./install (hv to type ./install to run install then ask for) ----do you want to... (4 Replies)
Discussion started by: kernel11
4 Replies

4. Shell Programming and Scripting

awk script for getting the selected records from a file.

Hello, I have attached one file named file.txt . I have to create a file using the awk script with the records in which 38th position is P and not V . ex it should have 00501 HOLTSVILLE NYP00501 and it should not include 00501 I R S SERVICE CENTER ... (3 Replies)
Discussion started by: sonam273
3 Replies

5. Shell Programming and Scripting

Script to find and email selected files

I am trying to come up with a script that will search for selected files and then email them to me. For example, say I have a directory that has the following files: AA_doug.txt AA_andy.txt BB_john.txt APPLE_mike.txt GLOBE_ed.txt GLOBE_tony.txt TOTAL_carl.txt what is the best way to... (2 Replies)
Discussion started by: coach5779
2 Replies

6. Shell Programming and Scripting

Run script on HACMP nodes?

Hello All, Anybody knows how can I run script on the AIX HACMP offline node, without logon a offline node? I would like to run a script on the online node and at same time or after the online node on the offline node. Any IDEA? :confused: (3 Replies)
Discussion started by: kalaso
3 Replies

7. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

8. Shell Programming and Scripting

Sum value from selected lines script (awk,perl)

Hello. I face this (2 side) problem. Some lines with this structure. ........... 12345678 4 12345989 13 12346356 205 12346644 74 12346819 22 ......... The first field (timestamp) is growing (or at least equal). 1)Sum the second fields if the first_field/500 are... (8 Replies)
Discussion started by: paolfili
8 Replies

9. Shell Programming and Scripting

Running a command on multiple selected files in nautilus script

I am trying to make a script to convert drg files to wav and so far i have this #!/bin/bash drg2sbg "$*" -o "$*".sbg sbagen -Wo "/home/nick/Desktop/I-Doser Wave Files/"$*"" "$*".sbg rm "$*".sbg cd "/home/nick/Desktop/I-Doser Wave Files" rename 's/\.drg$/\.wav/' *.drg exit the drg2sbg and... (2 Replies)
Discussion started by: Nickbowlingdude
2 Replies

10. Shell Programming and Scripting

Shell script to map XML nodes

Hi folks, I'm a bit of a novice at this but here goes. I want to read in nodes from an XML file, and map the path to each. eg the file is structured <node><nodename>.</nodename> <node><nodename>topnode</nodename> <node><nodename>subnode1</nodename></node> ... (1 Reply)
Discussion started by: mark14
1 Replies
Login or Register to Ask a Question