0

Question was changed, which I believe is different from the existing one that is considered to be similar.

I have a property file specify the mapping between old namespace and new namespace. For example: http://old-namespace1 = http://new-namespace1. The mapping is 1:1. There are about two hundred entries. I can build a map for old namespace and new namespace with this mapping property file.

For each xml file that needs such changes, I want to replace all occurrences of old namespace uri in the mapping file with the corresponding new namespace uri.

The following is a sample of xml file before the change and after the change.

xml file before:

<?xml version="1.0" encoding="UTF-8" ?>
<rootElem xmlns="http://old-namespace1"   xmlns:ns2="http://old-namespace2" xmlns:ns3="http://old-namespace3">
    <childElem xmlns="http://old-namespace4">
      ...
    </childElem>
    ...
</rootElem>

xml file after:

<?xml version="1.0" encoding="UTF-8" ?>
<rootElem xmlns="http://new-namespace1"   xmlns:ns2="http://new-namespace2" xmlns:ns3="http://new-namespace3">
    <childElem xmlns="http://new-namespace4">
      ...
    </childElem>
    ...
</rootElem>

I'd like to do it using xslt and call the transform from java program: 1. for each xmlns:*, get its value 2. find if the value has mapping, if so, get the mapped new value, 3. output the new value.

My question is for #2 above: I can list the maps in xsl. But if the map already exists in another file, is there a way in xsl to read the file and get the map?

user3014901
  • 367
  • 2
  • 4
  • 15
  • What about a replace with regex ? I mean for example using `find` and `sed` you can do this in one command or even in your IDE or some editor. – Duffydake Mar 13 '15 at 15:13
  • Thank you. But this utility needs to be invoked by java program. – user3014901 Mar 13 '15 at 16:04
  • 1
    If your requirement is only to change URL you can create a script and call it with java or do all of this directly in java, I think it's the more simple and fastest way to do it. – Duffydake Mar 13 '15 at 16:15
  • My question (with a view to using XSLT) would be: do you have any namespace declarations that aren't actually used in the XML document to place nodes in that namespace? And if yes, do you need to pass them on (after remapping)? If not (and that would be the reasonable answer), then this could be handled by XSLT - not as a problem of "replacing namespaces", but as a problem of moving nodes from namespace X to namespace Y. Here, it is not necessary to know what prefix is bound to namespace X in the source document. – michael.hor257k Mar 14 '15 at 05:52
  • Actually I don't care if the namespace declaration is being used. As long as the namespace uri value appears in the map, it needs to be replaced with the new value. That is why I think it is an overkill to use dom? – user3014901 Mar 17 '15 at 19:16
  • I think so too. The namespace URL should only appear once in the document. You don't need DOM or XSLT to fix that. – user207421 Mar 17 '15 at 22:26

0 Answers0