1

we have a requirement to insert XML element in multiple locations

i have to insert one more element

<wtc-import>
      <name>WTCImportedService-0-rap01</name>
      <resource-name>XXXX01F</resource-name>
      <local-access-point>lap01</local-access-point>
      <remote-access-point-list>rap01</remote-access-point-list>
      <remote-name>XXXX01F</remote-name>
    </wtc-import>

in the ---INSERT WTC IMPORT SERVICE---- mentioned location in the below file

basically before the end of each < /wtc-server> tag

  <wtc-server>
    <name>WTC-server-117-02</name>
    <target>cwea_117_02</target>
    <wtc-local-tux-dom>
      <name>lap01</name>
      <access-point>lap01</access-point>
      <access-point-id>wl.prodcwe117.02</access-point-id>
      <block-time>600</block-time>
      <nw-addr>//localhost:7125</nw-addr>
    </wtc-local-tux-dom>
    <wtc-remote-tux-dom>
      <name>rap01</name>
      <access-point>rap01</access-point>
      <access-point-id>tx.hello101</access-point-id>
      <local-access-point>lap01</local-access-point>
      <nw-addr>//hostanme:71105</nw-addr>
      <federation-url></federation-url>
      <federation-name></federation-name>
    </wtc-remote-tux-dom>
    <wtc-import>
      <name>WTCImportedService-0-rap01</name>
      <resource-name>XXXX01F</resource-name>
      <local-access-point>lap01</local-access-point>
      <remote-access-point-list>rap01</remote-access-point-list>
      <remote-name>XXXX01F</remote-name>
    </wtc-import>

-------INSERT NEW WTC IMPORT HERE---------

  </wtc-server>

  <wtc-server>
    <name>WTC-server-117-03</name>
    <target>cwea_117_03</target>
    <wtc-local-tux-dom>
      <name>lap01</name>
      <access-point>lap01</access-point>
      <access-point-id>wl.prodcwe117.02</access-point-id>
      <block-time>600</block-time>
      <nw-addr>//localhost:7125</nw-addr>
    </wtc-local-tux-dom>
    <wtc-remote-tux-dom>
      <name>rap01</name>
      <access-point>rap01</access-point>
      <access-point-id>tx.hello101</access-point-id>
      <local-access-point>lap01</local-access-point>
      <nw-addr>//hostanme:71105</nw-addr>
      <federation-url></federation-url>
      <federation-name></federation-name>
    </wtc-remote-tux-dom>
    <wtc-import>
      <name>WTCImportedService-0-rap01</name>
      <resource-name>XXXX01F</resource-name>
      <local-access-point>lap01</local-access-point>
      <remote-access-point-list>rap01</remote-access-point-list>
      <remote-name>XXXX01F</remote-name>
    </wtc-import>

-------INSERT NEW WTC IMPORT HERE---------

 </wtc-server>

Please help

4
  • uhh, shell/Python? That's like saying "I have a Phillips-head screw, and want to know how to embed it in a wooden plank using either a rusty fishhook or a screwdriver". You can do this in shell, but doing it well (read: in a way that works with all possible forms your XML input can take and guarantees valid XML output) requires either building an XSLT template for the translation and invoking xsltproc, or using tools like XMLStarlet that effectively put a pretty frontend on the job of building a XSLT template. Much better to implement it in Python. Commented Jan 26, 2019 at 0:40
  • BTW, "snippet" functionality in StackOverflow is basically an equivalent to JSFiddle; it's only for code that can be run in a browser. Use the {} button (or four-space indents, or triple-quoted segments) for all other cases where you want to put literal code in a question. Commented Jan 26, 2019 at 0:43
  • Sure @CharlesDuffy Thanks for the inputs Commented Jan 26, 2019 at 0:46
  • BTW, it helps to make sure your code is complete enough to be testable. That means including a root element, and any xmlns definitions that might be at the top of the file. Commented Jan 26, 2019 at 0:53

1 Answer 1

2

Doing this in shell with XMLStarlet would look like:

xmlstarlet ed \
  --subnode '//wtc-server' -t elem -n wtc-import \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n name -v WTCImportedService-0-rap01 \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n resource-name -v XXXX01F \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n local-access-point -v lap01 \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n remote-access-point-list -v rap01 \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n remote-name -v XXXX01F \
  <in.xml >out.xml

(code tested without any xmlns definitions, since the original version of the question didn't contain any).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.