XML root element

Hi All

Can someone please help me with this awk to search an element in a XML file with a particular value and then change the root element.

Thanks & Regards
Karan

Can you give a provide the file content.

Hi All,

I have a XML file which i want to search an element with two set of values in the PCDATA.
The element is CustomNoteTypeName and can have two values i.e. DBHA or DAKO.
If these two values are found then change the root element of the XML file to something other what is at present if not found let the XML files process the way are to be.

Kindly let me know if you need more information from my end.

Here is a XSL stylesheet which does what you want do. Just change the root element "root" to whatever you need it to be.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"/>

<xsl:template match="/XmlInterchange/Payload/Consols/Consol/Notes/Note/CustomNoteTypeName">
   <xsl:if test=". = 'DBHA' or . = 'DAKO'" >
      <xsl:element name="root">
        <xsl:copy-of select="/*/node()" />
      </xsl:element>
   </xsl:if>
</xsl:template>

<xsl:template match="/">
  <xsl:apply-templates select="/XmlInterchange/Payload/Consols/Consol/Notes/Note/CustomNoteTypeName"/>
</xsl:template>

</xsl:stylesheet>

I am a newbie to Unix can you tell me how to use it in a awk script with the extension .awk???

Many thanks

Karan

Here is how to do it using grep and sed. Given that your elements are not on separate lines, sed is a better tool to use than awk.

grep -E "DBHA|DAKO" yourfile.xml > /dev/null
if (( $? == 0 ))
   sed -e 's|<XmlInterchange>|<root>|' -e 's|</XmlInterchange>|</root>|' yourfile.xml
fi

Hi Murphy,

I believe this will work if the XML file has elements after new lines what if it is one line XML file then grep wont work.

I am assuming i am not sure if i am right above.

Here is what i wrote ...changing the original request..the XML element and value has changed...

{
+2 if ( grep -F "<NumberType>BHT</NumberType>" )
+3 {
+4 gsub(/XmlInterchange/,"XmlInterchange_new",$0);print $0;
+5 }
+6 else
+7 print $0;
+8 }
+9

Can you confirm if this is fine.

And what if the XML file is one line file.

Regards
Karan

Can you please help me with an awk as it will check the folder and if any file comes then it will check for the relevant value and change the root element...

I hope you got it. I need a generalised one not on particular XML files

Thanks

Karan

Why don't you use some XML command line tool like xmlstarlet, some XML related Perl module or XMLgawk?

I am not sure whether these would work in my AIX machine.

Thats why i am looking for a generalised solution using awk.

Can you please help me with that.

Thanks

Karan