1

thanks for helping. I'm not a programmer, but i understand basic principles. i need to do this in a bunch of xml files. i'm certain xpath pl or xtask or some combination using regex, could pull this off, but i'm lost. does anyone have any ideas? thanks!

Here is the scope:

Copy the from the “scc_title” element to the “scc_comments” element. The scc_comments element is usually empty. if it isn't i still need it to append it to the current content.

<property name="scc_title" type="s">NEED TO COPY THIS TEXT</property>

<property name="scc_comments" type="s">AND PASTE IT HERE</property>
chaos
  • 27,506
  • 12
  • 74
  • 77
user252328
  • 11
  • 2

2 Answers2

2

An alternative way with python and ElementTree:

from __future__ import print_function
import sys 
import xml.etree.ElementTree as ET

def main():
  if len(sys.argv) < 3:
    print("usage:", sys.argv[0], "input", "output")
    sys.exit(1)
  tree = ET.parse(sys.argv[1])
  root = tree.getroot();
  src = root.find(".//*[@name='scc_title']")
  dst = root.find(".//*[@name='scc_comments']")
  if src is not None and dst is not None:
    dst.text += src.text
    tree.write(sys.argv[2])
  else:
    if src is None:
      print("Failed to find 'scc_title' attribute", file=sys.stderr)
    if dst is None:
      print("Failed to find 'scc_comments' attribute", file=sys.stderr)
    sys.exit(1)

if __name__ == "__main__":
  main()
Jason Conti
  • 2,371
1

Pythonic non xml way assuming that scc_title comes before scc_comments AND each tag has its own line AND that all of the XML files are in the same directory I didn't test this, but this is the basic idea. Also, I am not sure if there is a quick GUI way, and I am not a programmer either so there is probably a better way to do this with the xml modules:

#put this in the directory with the xml files
import re
import os

#for file_name in current directory "."
for file_name in os.listdir("."):

    if ".xml" in file_name:
        outfile = open("edited_"+file_name,"w+")

        with open(file_name,'r') as f:
            for line in f:

                if "scc_title" in line:
                    #split the string by two delimeters "<" and ">" and get the 3rd element starts at 0
                    scc_title_value = re.split('<|>',line)[2]

                if "scc_comments" in line:
                    scc_comments_value = re.split('<|>',line)[2]
                    #replace scc_comments_value with scc_title_value
                    line = line.replace(scc_comments_value,scc_title_value)

                outfile.write(line)
jmunsch
  • 2,213
  • 1
  • 22
  • 29