Splitting the XML file and renaming the files

Hello Gurus,

I have a requirement to split the xml file into different xml files.
Can you please help me with that?
Here is my Source XML file

 <jms-system-resource>
    <name>PS6SOAJMSModule</name>
    <target>soa_server1</target>
    <sub-deployment>
      <name>PS6SOASubDeployment</name>
      <target>PS6SOAJMSServer</target>
    </sub-deployment>
    <descriptor-file-name>jms/soajmsmodule-ps6-jms.xml</descriptor-file-name>
  </jms-system-resource>
  <jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

I would like to have above file splitted into two different files

File1 Should be named as JMSSSYSTEMRESOURSE1.XML and it needs to have the xml data like below

 <jms-system-resource>
    <name>PS6SOAJMSModule</name>
    <target>soa_server1</target>
    <sub-deployment>
      <name>PS6SOASubDeployment</name>
      <target>PS6SOAJMSServer</target>
    </sub-deployment>
    <descriptor-file-name>jms/soajmsmodule-ps6-jms.xml</descriptor-file-name>
  </jms-system-resource>

Second file should be named as JMSSSYSTEMRESOURSE2.XML and it needs to have the xml data like below

<jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

Can you please help me with it?

One might think that the help you got with your other three threads would give you everything you need to solve this problem.

What operating system are you using?

What shell are you using?

What have you tried to solve this problem on your own?

Hello Don,

I have tried below one code to extract all the

<jms-system-resource>

Code which i have tried

awk '
/<jms-system-resource>/{
  flag=1
  val=""
}
/<\/jms-system-resource>/{
  print val RS $0;
  val=flag="";
  next
}
flag && NF{
  val=val?val ORS $0:$0
}
'Inupt.xml

but the output is not promising for next transaction. Hence i have to generate separate files for each

<jms-system-resource>

tag.
And here are operating system details.

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Thanks,

---------- Post updated 03-28-18 at 11:25 AM ---------- Previous update was 03-27-18 at 02:53 PM ----------

I have also tried below

and i think i am successfull using below

awk '/<jms-system-resource>/{c++}{print > "JMSSYSTEMRESOURCE_" c ".xml"}
' Test_data.xml

Thanks For your Help:b:

1 Like

Congratulations!

Note, however, that the standards don't specify the precedence between string concatenation and output redirection. To be portable to all versions of awk , add parentheses around the concatenation as in:

awk '/<jms-system-resource>/{c++}{print > ("JMSSYSTEMRESOURCE_" c ".xml")}
' Test_data.xml

With the code you are using, you get the following diagnostic error (and no output files) when it is run using a BSD based version of awk (such as that found on macOS):

awk: syntax error at source line 1
 context is
	/<jms-system-resource>/{c++}{print > "JMSSYSTEMRESOURCE_" >>>  c <<<  ".xml"}
awk: illegal statement at source line 1