I was adding a namespace in an element using xsl element namespace attribute. That adds empty namespace in child element in result.
Here is the XSL that adds namespace into element "Auto"
EDIT - short version of my xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/">
<xsl:element name="Root">
<xsl:element name="Auto" namespace="http://www.Root.com/XMLSchema/Auto">
<xsl:element name="Applicant">
<xsl:element name="ApplicantType">
<xsl:text>Applicant</xsl:text>
</xsl:element>
</xsl:element>
</xsl:element>
<xsl:element name="Life" namespace="http://www.Root.com/XMLSchema/Auto">
<xsl:element name="Applicant">
<xsl:element name="ApplicantType">
<xsl:text>Applicant</xsl:text>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
Here is the xml that transformed by XSL
<Root>
<Auto xmlns="http://www.Root.com/XMLSchema/Auto">
<Applicant xmlns="">
<ApplicantType>Applicant</ApplicantType>
</Applicant>
</Auto>
</Root>
If you see the Applicant element, transformation added xmlns="". How to remove this empty namespace?
We can help you better if you show the XSL that's producing the Applicant
and ApplicantType
elements. Also, you're confusing "namespace" and "namespace declaration" in your description of the problem... separating these two might help you grasp the solution
Your XSL code is apparently telling the processor to output the Applicant
element in no namespace. (And therefore your code is probably wrong... you want Applicant
to be in the same Auto
namespace as its parent.) Since Applicant
with no prefix would inherit the default namespace declaration from its parent, the output XML must "undeclare" the default namespace declaration, in order to put Applicant
in no namespace as you requested.
For example, if your XSL code says:
<xsl:element name="Auto" namespace="http://www.Root.com/XMLSchema/Auto">
<xsl:element name="Applicant">
then you are telling XSL to output the Applicant
element in no namespace, as described above. To fix this, you can repeat the namespace:
<xsl:element name="Auto" namespace="http://www.Root.com/XMLSchema/Auto">
<xsl:element name="Applicant" namespace="http://www.Root.com/XMLSchema/Auto">
or as @empo said, you can declare a namespace prefix and use it:
<xsl:stylesheet ... xmlns:auto="http://www.Root.com/XMLSchema/Auto">
...
<auto:Auto>
<auto:Applicant>
<auto:ApplicantType>
...
or use a default namespace declaration in the stylesheet (or template):
<xsl:stylesheet ... xmlns="http://www.Root.com/XMLSchema/Auto">
...
<xsl:element name="Auto">
<xsl:element name="Applicant">
...
Don't think in terms of namespace declarations in the surface syntax of the XML, think about element names as (uri, localname) pairs. If you create an Applicant element that's not in any namespace, but it's parent is in a namespace, then the serializer is going to add xmlns="" to ensure that your wishes are respected (i.e. having Applicant in a different namespace from its parent element). On the other hand, if you want Applicant to be in the namespace "http://www.Root.com/XMLSchema/Auto", then you need to ensure that it is created in this namespace. You haven't shown the code that created it, so we can't tell exactly what you did wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With