Looking for a script to update xml tag attribute

Hi,
Can you please help me with a script to update xml attributes.

My xml structure looks like

<?xml version="1.0" encoding="UTF-8"?>
<env:envelope xmlns="abc" xmlns:env="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="220160"><env:transaction xmlns:ns2="urn:publicid:-:abc:MESSAGE:1.0" xmlns:ns3="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="646">
<ns3:header name="ORIGIN" value="T"/>
<ns3:app.message id="345">
<ns2:transmission>

I need to change the id of ns3:app.message to follow this sequence - 1,10,100,1000,1001,1002,1003,1004,1005, 1006, 1007, 1008, 1009, 101, 1010, 1011, 1012, 1013. Or to simplify it could be just sequence starting from 1000. It should restart from 1000 every time for a new env:transaction tag. Some bug in third party system means that these id needs to be in alphabetical sequence but still in numerical format. Like you enter numbers in a column in excel and treat them as alphabetical and sort them.

Please help. Or give some hints.

@sammyp75 , , welcome, we hope you find the forum useful and educational. NB: its not our role to write complete solutions but to help you find them , and typically there's more than one way to solve :smiley:

  • what have you tried/attempted - please share all - successful or otherwise
  • show example of EXPECTED OUTPUT, we might be able to guess, but you know what is actually expected, so this will help the team give pointers to assist.
  • what OS (name/version please) are you using

thks

I am planning to write a solution using java.

I am expecting an outcome like this

<?xml version="1.0" encoding="UTF-8"?> 
 <env:envelope xmlns="abc" xmlns:env="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="220160"><env:transaction xmlns:ns2="urn:publicid:-:abc:MESSAGE:1.0" xmlns:ns3="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="646"> 
 <ns3:header name="ORIGIN" value="T"/> 
 <ns3:app.message id="1000"> 
 .... 
 </ns3:app.message> 
 <ns3:app.message id="1001"> 
 .... 
 </ns3:app.message> 
 <ns3:app.message id="1002"> 
 .... 
 </ns3:app.message> 
 </env:transaction> 
 <env:transaction> 
 <ns3:app.message id="1000"> 
 .... 
 </ns3:app.message> 

 <ns3:app.message id="1001"> 
 .... 
 </ns3:app.message> 

Give me some hint may be. Or point to similar question.

please, provide COMPLETE examples not bits '....' doesn't mean anything

i've also edited your post and put it in markdown - you only need the backticks at the start and end not on every line

if writing this in java then an iterator over the parts being generate should be a trivial task.
I presume you are experienced coder .... if not please give an idea of your level of technical expertise.

You mean javascript, correct?

Desired xml

<?xml version="1.0" encoding="UTF-8"?>
<env:envelope
	xmlns="urn:publicid:-:abc:pqr:MESSAGE:1.0"
	xmlns:env="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="220160">
	<env:transaction
		xmlns:ns2="urn:publicid:-:abc:pqr:MESSAGE:1.0"
		xmlns:ns3="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="646">
		<ns3:header name="ORIGIN" value="T"/>
		<ns3:app.message id="1000">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
		<ns3:app.message id="1001">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
		<ns3:app.message id="1002">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
		<ns3:app.message id="1003">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
	</env:transaction>
	<env:transaction
		xmlns:ns2="urn:publicid:-:abc:pqr:MESSAGE:1.0"
		xmlns:ns3="urn:publicid:-:abc:GENERAL:ENVELOPE:1.0" id="647">
		<ns3:header name="ORIGIN" value="T"/>
		<ns3:app.message id="1000">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
		<ns3:app.message id="1001">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
		<ns3:app.message id="1002">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
		<ns3:app.message id="1003">
			<ns2:transmission>
				<ns2:record>
					<ns2:transaction.id>646</ns2:transaction.id>
				</ns2:record>
			</ns2:transmission>
		</ns3:app.message>
	</env:transaction>
</env:envelope>

Java wouldn't be my go to for this if it was an operation I was performing in isolation, I'd use a simple script to do something like the following:
step through each line resetting an id variable to 1000 if there's a new transaction, otherwise substituting the id if it's a new message and incrementing the id variable. However the complexity of the text in the message could break this approach.

Java's Xerces library is useful for this kind of processing, there's a good intro here , Have a go at using it to modify your data and show us how you're getting on if you've any questions.

almost sorted using java. thanks.

What is the point if you ignore staff questions and do not post any code?

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class JDomParser {

    private File file;

    public static void main(String args[]){
        JDomParser jDomParser = new JDomParser(new File("./old.xml"));
        jDomParser.updateSequences();

    }
    public JDomParser(File file) {
        this.file = file;
    }

    public void updateSequences() {
        try {
            Namespace namespace = Namespace.getNamespace("env", "urn:publicid:-:abc:GENERAL:ENVELOPE:1.0");

            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(this.getFile());
            Element tutorials = doc.getRootElement();
            List<Element> transactions = tutorials.getChildren("transaction", namespace);
            for (Element transaction : transactions){
                int i = 1000;
                List<Element>  appMessagesInTransaction  =  transaction.getChildren("app.message", namespace);
                for(Element appMessage : appMessagesInTransaction){
                    appMessage.getAttribute("id").setValue(i++ + "");
                }


            }
            XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());

            try(FileOutputStream fileOutputStream =
                        new FileOutputStream("./new.xml")){
                xmlOutputter.output(doc, fileOutputStream);
            }
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
    }


    public File getFile() {
        return file;
    }

}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.