Reputation: 449
i'm trying to remove some elements from xml using xsl and also to change a namespace value declared in sub1 element. The problem is, when i change the namespace value, the old namespace declaration is inserted into a child element (sub2 in the example), how can i change the code to prevent it?
I made this example because i cannot show the real code
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 ID="someId" xmlns:pref="http://www.myMountain.org/blabla">
<pref:sub2>
Today is tuesday
<pref:sub3 att="someAttribute">
Some text
<pref:sub4>
<pref:emptyElement/>
</pref:sub4>
</pref:sub3>
</pref:sub2>
</pref:sub1>
</lamp:rootElement>
test.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oldPref="http://www.myMountain.org/blabla"
xmlns:pref="http://www.myHill.org/blabla"
exclude-result-prefixes="oldPref">
<!-- For deleting element "sub4" -->
<xsl:template match="oldPref:sub1/oldPref:sub2/oldPref:sub3/oldPref:sub4"/>
<!-- Now i'm replacing the value of "pref" namespace (by creating a new element) -->
<xsl:template match="oldPref:sub1">
<pref:sub1>
<!-- Now i'm copying the elements from old "sub1" to the new one -->
<xsl:apply-templates select="@*|node()"/>
</pref:sub1>
</xsl:template>
<!-- Now im copying the rest of the xml file -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
wrong result that i get result.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 xmlns:pref="http://www.myHill.org/blabla" ID="someId">
<pref:sub2 xmlns:pref="http://www.myMountain.org/blabla">
Today is tuesday
<pref:sub3 att="someAttribute">
Some text
</pref:sub3>
</pref:sub2>
</pref:sub1>
</lamp:rootElement>
desired result
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 xmlns:pref="http://www.myHill.org/blabla" ID="someId">
<pref:sub2>
Today is tuesday
<pref:sub3 att="someAttribute">
Some text
</pref:sub3>
</pref:sub2>
</pref:sub1>
</lamp:rootElement>
My saxon
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Project-Name: Saxon-HE
Created-By: 1.7.0_05-b06 (Oracle Corporation)
Main-Class: net.sf.saxon.Transform
This is the command i'm using
java net.sf.saxon.Transform -s:test.xml -xsl:test.xsl -o:result.xml
Upvotes: 0
Views: 81
Reputation: 101730
The problem here is that you are changing the namespace on pref:sub1
, but not on any of the other pref:
elements. You need something more general:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oldPref="http://www.myMountain.org/blabla"
xmlns:pref="http://www.myHill.org/blabla"
exclude-result-prefixes="oldPref">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="oldPref:*">
<xsl:element name="pref:{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="oldPref:sub4" />
</xsl:stylesheet>
When run on your sample input, the result is:
<?xml-stylesheet version="1.0" href="test.xsl"?>
<lamp:rootElement xmlns:lamp="adgfdgfhdsadfse">
<pref:sub1 ID="someId" xmlns:pref="http://www.myHill.org/blabla">
<pref:sub2>
Today is tuesday
<pref:sub3 att="someAttribute">
Some text
</pref:sub3>
</pref:sub2>
</pref:sub1>
</lamp:rootElement>
Upvotes: 1