XML root element


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML root element
# 1  
Old 10-18-2008
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
# 2  
Old 10-18-2008
Can you give a provide the file content.
# 3  
Old 10-18-2008
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.
# 4  
Old 10-18-2008
Here is a XSL stylesheet which does what you want do. Just change the root element "root" to whatever you need it to be.
Code:
<?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>

# 5  
Old 10-18-2008
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
# 6  
Old 10-18-2008
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.
Code:
grep -E "DBHA|DAKO" yourfile.xml > /dev/null
if (( $? == 0 ))
   sed -e 's|<XmlInterchange>|<root>|' -e 's|</XmlInterchange>|</root>|' yourfile.xml
fi

# 7  
Old 10-18-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert a CSV within xml element tag using Python?

Hi Team, I have a CSV file which I have to read through and needs to insert the content within an XML file using Python ONLY ( as most of the code base we have in python only). I managed to find the first part, missing how to insert to XML under "specific" tags. cat input.csv... (0 Replies)
Discussion started by: panyam
0 Replies

2. UNIX for Dummies Questions & Answers

Extract Element from XML file

<?xml version = '1.0' encoding =... (8 Replies)
Discussion started by: Siva SQL
8 Replies

3. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. UNIX for Advanced & Expert Users

Perl XML::DOM: How to test if element exists?

Hi, I'm trying to write a script for some xml file handling, but I'm not getting too far with it. I've got the following xml content <?xml version="1.0" encoding="UTF-8"?> <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <Operation name="OPER1"> <Action name="ACTION1">... (2 Replies)
Discussion started by: Juha
2 Replies

5. Shell Programming and Scripting

Need to find root element name of XML file

Given this XML: <?xml version="1.0"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> ... (2 Replies)
Discussion started by: ricksj
2 Replies

6. Shell Programming and Scripting

Extracting the Root Element from the XML File

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 (6 Replies)
Discussion started by: sree_chari
6 Replies

7. UNIX Desktop Questions & Answers

read XML xml element with REGEXP

Hi, I would need to read an xml element from an xml file to a local variable. Please could you help me with a shell script to get so? Considering that I have a file called file.xml like below: <header> <description>This is the description</description> <content>This is the... (2 Replies)
Discussion started by: oscarmon
2 Replies

8. Shell Programming and Scripting

Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example: 1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message> 2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>... (1 Reply)
Discussion started by: sharpi03
1 Replies

9. Shell Programming and Scripting

Finding a XML element and moving the file

Hi All, I am looking for a awk/shell which can find an element named REFERENCE in a XML file and check whether it is empty or not. If there is no value in the REFERENCE element then correspondingly move the file to some other folder. The Unix server is AIX version 4. Any inputs... (9 Replies)
Discussion started by: karansachdeva
9 Replies

10. Shell Programming and Scripting

reading and searching xml element text in script

well i have this xml file here: this file is called filereader.xml <?xml version="1.0" encoding="UTF-8"?> <file> <file1> <filecopy>/new/test/thefile.txt</filecopy> <filecopy>/new/test/thefile2.ppt</filecopy> </file1> </file> i need to write the script that search for the Bold text... (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question