I don't know if this post is relevant to your situation garbile, but since learning that
UE supports ECMAScript for XML (E4X), I have been looking for problems where I could utilize E4X to solve the problem.
Since You, as Mofi also points out, do not include a before and after example, let's assume we have a xml like document like this:
- Code: Select all
<roottag>
<proc>
<step1>
<foo>dummy text
</foo>
</step1>
<step1>
<bar>
dummy text
</bar>
</step1>
</proc>
<proc>
<step1/>
<step1>
<acme>1</acme>
</step1>
</proc>
</roottag>
I then made this script which takes a completely different approach than Mofis script. But it will only work if your input is a valid XML file, while Mofis will work regardless as long as it finds <proc>'s and <step1>'s.
- Code: Select all
// Misc options for global XML object:
// http://developer.mozilla.org/en/docs/E4X_Tutorial:The_global_XML_object
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
XML.ignoreWhitespace = true;
XML.prettyPrinting = true;
XML.prettyIndent = 2;
// Do the rest of the processing in a function, so we can quit the script
renumberLabels();
function renumberLabels() {
// Retrieve whole document into XML List object.
UltraEdit.activeDocument.selectAll();
var xmlDoc = UltraEdit.activeDocument.selection;
// Try and create a XML object from the document contents:
try {
var xmlRoot = new XML( xmlDoc );
}
catch (xmlException) {
UltraEdit.messageBox("Document is not valid XML");
return; // quit the rest of the script
}
// Get XMLlist of <proc>'s
// http://developer.mozilla.org/en/docs/E4X_Tutorial:Accessing_XML_children
var xmlProcList = xmlRoot.proc;
// Iterate through <proc>'s
for (i in xmlProcList) {
// set renumber variable to 1 for each <proc>
var stepRenumber = 1;
// get <step1>'s in <proc> group
var xmlStep1List = xmlProcList[i].step1;
// Iterate over <step1>
for (j in xmlStep1List) {
// Assign label attribute for each <step1>
xmlStep1List[j].@label = (stepRenumber++);
}
}
// Write XML document back into editor while PrettyPrint formatting as well
UltraEdit.activeDocument.write( xmlRoot.toXMLString() );
}
The script will produce this output:
- Code: Select all
<roottag>
<proc>
<step1 label="1">
<foo>dummy text</foo>
</step1>
<step1 label="2">
<bar>dummy text</bar>
</step1>
</proc>
<proc>
<step1 label="1"/>
<step1 label="2">
<acme>1</acme>
</step1>
</proc>
</roottag>
You can run the script again and again even when label properties is already inserted. They will be renumbered if you reorganize some of them.
I learned something about E4X in the process and I hope other sees the potential in this to manipulate XML documents in UE.