1

I have a requirement where I need to read a XML file and replace the tag that contains /, " and save it as a new file.

The original tag format:

<CollateralReportDocument xmlns="http://schemas.foo.com/sII/CollateralReportDocument/1.0">

The new file should have the tag as:

<CollateralReportDocument>

I have tried using

Sed -i 's/<CollateralReportDocument xmlns="http://schemas.foo.com/sII/CollateralReportDocument/1.0">
/ <CollateralReportDocument>/g' filename.xml 

...but it is not working. Can you help me with this?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Aaron
  • 662
  • 8
  • 20
  • 1
    Fixing your code that's reading the file to be namespace-aware would very much be the better practice. XML namespaces are there for a reason -- they're how/why a single document can have different types of (meta)data, and the people designing those schemas don't need to coordinate on a single set of names before someone can combine those schemas in a single document. – Charles Duffy Mar 16 '18 at 20:48
  • Even if you really do want to translate everything in the CollateralReportDocument/1.0 namespace to the default namespace, better to do that with an XSLT stylesheet, so it'll know that if you have a `xmlns:crd="http://schemas.foo.com/sII/CollateralReportDocument/1.0"` somewhere else in your document (or in a future version of same), `crd:bah` needs to be changed to `bah` in all parts of the document under the point in the tree where the definition took place. – Charles Duffy Mar 16 '18 at 20:51

1 Answers1

0

You use / as separator, but you also have some / inside your string to substitute.

You should use # as separator instead.

Linux:

sed -i 's#<CollateralReportDocument xmlns="http://schemas.foo.com/sII/CollateralReportDocument/1.0">#<CollateralReportDocument>#g' filename.xml

macOS:

sed -i '' 's#<CollateralReportDocument xmlns="http://schemas.foo.com/sII/CollateralReportDocument/1.0">#<CollateralReportDocument>#g' filename.xml
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • command works without any error but the xml file still has the old tag the requirement is for Spark where I can not read this file at the root tag because the end tag is – Aaron Mar 16 '18 at 20:57
  • @user2315840 Strange... It works for me on macOS and Manjaro. What OS/distribution are you working on? – Ronan Boiteau Mar 16 '18 at 21:04