Strip xml element nodes based on conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strip xml element nodes based on conditions
# 1  
Old 03-29-2012
Strip xml element nodes based on conditions

Hi Gurus,

I have a xml in which I have to remove unused element nodes. Here are the requirements

1. Remove StoreAddresses & Contacts complex element directly
2. Remove StoreItemCharacteristic element based on CharacteristicName="Organic"

Here is the sample xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Stores>
    <Store StoreId="123" CreateDate="2010-01-31T20:46:22">
        <StoreItems>
            <StoreItem StoreItemId="776607f4" ItemName="Milk" InStockDate="2010-01-31T00:00:00" ExpDate="2012-01-19T14:49:46.317">
                <StoreItemCharacteristics>
                    <StoreItemCharacteristic CharacteristicId="d65f1cec" CharacteristicName="Dairy" CreateDate="2010-01-31T00:00:00" ModifyDate="2010-01-31T00:00:00">
                        <Values>
                            <Value ValueId="949a4f81-6128-4d78-afa7-531c0dd76343" ValueName="Dawn" CreateDate="2010-01-31T00:00:00" ModifyDate="2010-01-31T00:00:00"/>
                        </Values>
                    </StoreItemCharacteristic>
                    <StoreItemCharacteristic CharacteristicId="2e09ca76" CharacteristicName="Organic" CreateDate="2010-01-31T00:00:00" ModifyDate="2010-01-31T00:00:00">
                        <Values>
                            <Value ValueId="29520b49" ValueName="Horizon" CreateDate="2010-01-31T00:00:00" ModifyDate="2010-01-31T00:00:00"/>
                        </Values>
                    </StoreItemCharacteristic>
					<StoreItemCharacteristic CharacteristicId="2e09ca76" CharacteristicName="Blender" CreateDate="2010-01-31T00:00:00" ModifyDate="2010-01-31T00:00:00">
                        <Values>
                            <Value ValueId="29520b49" ValueName="Cuisinart" CreateDate="2010-01-31T00:00:00" ModifyDate="2010-01-31T00:00:00"/>
                        </Values>
                    </StoreItemCharacteristic>
                </StoreItemCharacteristics>
            </StoreItem>
        </StoreItems>
        <StoreAddresses>
            <StoreAddress AddressTypeCode="1" AddressTypeName="Commericial" AddressOne="Dallas" StateName="Texas" CreateDate="2012-01-19T00:00:00" ModifyDate="2012-01-19T00:00:00"/>
        </StoreAddresses>
        <Contacts>
            <Contact FirstName="" LastName="JOE" MiddleName="" CellPhone="          " HomePhone="" WorkPhone="" BestTimeToCall="Anytime" Comment="" />
        </Contacts>
    </Store>
</Stores>

For (1), I was able to remove using sed command

Code:
sed -i /<Contacts>/,/<\/Contacts>/d Stores.xml
sed -i /<StoreAddresses>/,/<\/StoreAddresses>/d Stores.xml

above commands are deleting entire node from start tag to end tag As Desired

For (2), I am not getting how to delete from start tag (<StoreItemCharacteristic ... </StoreItemCharacteristic>) to end tag based on condition CharacteristicName="Organic". Please help me how can I achieve this


I am stuck very badly ....Help me please please

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 03-29-2012 at 08:17 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strip file based on pattern

Hi, I am stripping the below file based on a pattern: **Starts**02-MAY-2017 03:48:13 **Ends**02-MAY-2017 03:48:13 +---------------------------------------------------------------------------+ Start of log messages ... (2 Replies)
Discussion started by: Prasannag87
2 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

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

4. 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

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

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 (9 Replies)
Discussion started by: karansachdeva
9 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