Any help to extract the root element from an XML file will be appreciated.
Example: test.xml
<?xml version="1.0" encoding="utf-8" ?>
<TestXMLMessage>
<TestRec>
<ID>1000</ID>
</TestRec>
</TestXMLMessage>
Wanted to extract the TestXMLMessage.
Regards,
Chari
Something like this?
awk -F"[<>]" 'NR==2{print $2}' file
or maybe this what you mean:
awk '/<TestXMLMessage>/{f=1};f;/<\/TestXMLMessage>/{f=0}' file
1 Like
Could this help you?
awk '{if(/^<TestXMLMessage>/){print;flg=1;next} if(flg==1) {print;next} if (/^<\/TestXMLMessage>/){print;flg=0}}' test.xml
This can be done a bit shorter:
awk '/<TestXMLMessage>/,/<\/TestXMLMessage>/' file
1 Like
Here is an XSL stylesheet which prints the root element of the document
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="name(/*)"/>
</xsl:template>
</xsl:stylesheet>
Using xsltproc to transform the input docuemnt:
$ xsltproc example.xsl example.xml
TestXMLMessage
$
Hi All,
Thanks for your quick response.
This helped what I wanted to achieve.
awk -F"[<>]" 'NR==2{print $2}' file
Best Regards,
Chari
Hi All,
I am trying to change the XML filename in a folder to the rootelement in the XML file.
Now I have something like this.
for i in *.xml; do echo $i;awk -F"[<>]" 'NR==2{print $2}' $i; done
Output of above command:
TestMsg2010-10-19_20_20_54.xml
R1Msg TimeStamp="2010-10-19T08:49:08.000000Z"
TestMsg2010-10-19_20_20_58.xml
R2Msg TimeStamp="2010-10-19T08:49:08.000000Z"
Renamed File Output:(This is what I want to achieve)
R1Msg2010-10-19_20_20_54.xml
R2Msg2010-10-19_20_20_58.xml
R3Msg2010-10-19_20_21_00.xml
Is there a way that I can tweak the above command to achieve the same? Please help me as I am new to this side of programming.
Thanks in advance.