0

I want to replace the namespace in an xml file using sed (I've given up trying to do it from ant, where it is required). The main issue is that that there is a repeating list of elements - not all of which need replacing.

Xml file:

<xml-fragment>
  <imp:exportedItemInfo instanceId="WikiDS_v1_PS" typeId="ProxyService" xmlns:imp="http://www.bea.com/wli/config/importexport">
    <imp:properties>
      <imp:property name="dataclass" value="com.bea.wli.sb.resources.config.impl.XmlEntryDocumentImpl"/>
      <imp:property name="isencrypted" value="false"/>
    </imp:properties>
  </imp:exportedItemInfo>
  <imp:exportedItemInfo instanceId="NonShared/wikitest/FileUploadManagementDS_v1_BS_WSDL" typeId="WSDL" xmlns:imp="http://www.bea.com/wli/config/importexport">
    <imp:properties>
      <imp:property name="dataclass" value="com.bea.wli.sb.resources.config.impl.WsdlEntryDocumentImpl"/>
      <imp:property name="isencrypted" value="false"/>
    </imp:properties>
  </imp:exportedItemInfo>
  <imp:exportedItemInfo instanceId="NonShared/wikitest/WikiTestDS_v1_BS" typeId="BusinessService" xmlns:imp="http://www.bea.com/wli/config/importexport">
    <imp:properties>
      <imp:property name="dataclass" value="com.bea.wli.sb.resources.config.impl.XmlEntryDocumentImpl"/>
      <imp:property name="isencrypted" value="false"/>
    </imp:properties>
  </imp:exportedItemInfo>
</xml-fragment>

So in this case I want to replace the namespace in the block that has a typeId of ProxyService but none of the others.

0

You can use sed to replace only the lines matching the particular pattern.

sed -e '/typeId="ProxyService"/s/xmlns:imp="http:\/\/www.bea.com\/wli\/config\/importexport"/xmlns:imp="http:\/\/www.bea.com\/wli\/config\/importexport2"/' new.xml

This will replace your namespace only in the lines matching "typeId="ProxyService" . I've just added a 2 to your namespace, but you can replace that with any string of your choice.

  • Oops my edit didn't work - I did add that the namespace I wish to change is the one for the attribute 'dataclass' in the child 'property' element. I thought this may be possible using the g switch somehow - i.e., match from ProxyService until the closing imp:exportedItemInfo tag – user2609641 May 15 '15 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.