0

i thought this would be simple but now i spent about 4 hours on this problem. All i want to do is change the default namespace of this XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<scenarios xmlns="http://my.url/xmlns/scenarios/v1.0.0">
    <scenarios>
        <scenario id="1" name="00_reset" active="true">
            <events>
                <sensorevent id="1" name="resetButtonEvent">
                    <sensors>
                        <sensor deviceid="46"/>
                    </sensors>
                </sensorevent>
            </events>
        </scenario>
    </scenarios>
    <systemstates>
        <systemstate id="1" default="true" name="00_visitor_reset" display="true" publish="true" type="BOOLEAN"/>
    </systemstates>
</scenarios>

"v1.0.0" should be changed to "v1.1.0". But all i could come up with is this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:old="http://my.url/xmlns/scenarios/v1.0.0" 
xmlns:new="http://my.url/xmlns/scenarios/v1.1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="old:*">
        <xsl:element name="{local-name()}" namespace="http://my.url/xmlns/scenarios/v1.1.0">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

which produces:

<?xml version="1.0" encoding="UTF-8"?><ns0:scenarios xmlns:ns0="http://my.url/xmlns/scenarios/v1.1.0">
    <ns1:scenarios xmlns:ns1="http://my.url/xmlns/scenarios/v1.1.0">
        <ns2:scenario xmlns:ns2="http://my.url/xmlns/scenarios/v1.1.0" id="1" name="00_reset" active="true">
            <ns3:events xmlns:ns3="http://my.url/xmlns/scenarios/v1.1.0">
                <ns4:sensorevent xmlns:ns4="http://my.url/xmlns/scenarios/v1.1.0" id="1" name="resetButtonEvent">
                    <ns5:sensors xmlns:ns5="http://my.url/xmlns/scenarios/v1.1.0">
                        <ns6:sensor xmlns:ns6="http://my.url/xmlns/scenarios/v1.1.0" deviceid="46"/>
                    </ns5:sensors>
                </ns4:sensorevent>
            </ns3:events>
        </ns2:scenario>
    </ns1:scenarios>
    <ns7:systemstates xmlns:ns7="http://my.url/xmlns/scenarios/v1.1.0">
        <ns8:systemstate xmlns:ns8="http://my.url/xmlns/scenarios/v1.1.0" id="1" default="true" name="00_visitor_reset" display="true" publish="true" type="BOOLEAN"/>
    </ns7:systemstates>
</ns0:scenarios>

which is not really what i want, because the only thing that should change is the namespace declaration in the root element. How can i change the default namespace from http://my.url/xmlns/scenarios/v1.0.0 to http://my.url/xmlns/scenarios/v1.1.0 using XSLT1.0 without adding prefixes?

3 Answers 3

1

Try

<xsl:stylesheet version="1.0" xmlns:old="http://my.url/xmlns/scenarios/v1.0.0" 
xmlns="http://my.url/xmlns/scenarios/v1.1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="old:*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

If that does not help then try a different XSLT 1.0 processor.

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

2 Comments

That works in XMLSpy (which isn't XSLT1.0) but not with the Java processor. I would rather not change processor, but if there is no other way i have to do it.
I don't think there is anything like the Java processor, there are various XSLT 1.0 processors available for Java, like Saxon 6.5 for instance, or like Xalan from Apache.org. I don't think it is an XSLT 1.0 versus 2.0 problem although the XSLT 2.0 spec is probably more specific as to which serialization result is the right one. Try with Saxon 6.5 to compare, I think it will give you the result you want. Although in the Java world I think moving to XSLT 2.0 with Saxon 9 is generally a good and easy step anyway. There is nothing you can change in the XSLT code itself.
0

XSLT 1.0 allows the XSLT processor to choose any namespace prefix it likes, so this output is conformant, but very unfriendly. In XSLT 2.0 the processor is required to choose the prefix you ask for unless there is a conflict, so this stylesheet should produce the output you want. So I think your choices are to run the code through an XSLT 2.0 processor, or through a more user-friendly XSLT 1.0 processor.

Comments

0

I've now switched to Saxon as XSLT processor and it shows the desired output. I wanted to avoid additional dependencies but it seems that there is no other way. Thanks Martin Honnen and Michael Kay!

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.