Reputation: 1152
i'm trying to add to my xsd:schema
the following prefix xmlns:nr0="http://NamespaceTest.com/balisesXrm"
without changing nothing of my XSD document.
I tried this :
<xsl:template match="xsd:schema">
<xsl:element name="nr0:{local-name()}" namespace="http://NamespaceTest.com/balisesXrm">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
But it creates two problems :
1 - My schema becomes invalid as the name is changed into :<nr0:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SCCOAMCD="urn:SCCOA-schemaInfo" xmlns="urn:SBEGestionZonesAeriennesSYSCA-schema">
2 - All my elements I created in XML schema have been erased.
How can i keep my elements and just add the prefix to my root?
Upvotes: 1
Views: 554
Reputation: 70648
For your first problem, your code is currently creating an element, when you really want to create a namespace declaration.
What you can do, is simply create a new xsd:schema
element with the required namespace declaration, and also copy all existing ones too.
<xsl:template match="xsd:schema">
<xsd:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="@*|node()"/>
</xsd:schema>
</xsl:template>
Or, if you can use XSLT 2.0, you could use xsl:namespace
and do this...
<xsl:template match="xsd:schema">
<xsl:copy>
<xsl:namespace name="nr0" select="'http://NamespaceTest.com/balisesXrm'" />
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
(xsl:copy
copies the existing namespaces in this case)
For your second issue, you need to add the identity template to your stylesheet, if you haven't already
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xsd:schema">
<xsd:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="@*|node()"/>
</xsd:schema>
</xsl:template>
<!-- Alternative code for XSLT 2.0 -->
<xsl:template match="xsd:schema">
<xsl:copy>
<xsl:namespace name="nr0" select="'http://NamespaceTest.com/balisesXrm'" />
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2