Reputation: 9
my xml input is:
<?xml version="1.0" encoding="windows-1255"?>
<ns0:PatMedData xmlns="http://Maccabi.Hospitals.Integration" xmlns:ns0="http://Maccabi.Hospitals.Integration">
<MessageId>AS80000000000456</MessageId>
</ns0:PatMedData>
I cant change the xml scheme, the problem is that the root element has xmlns="http://Maccabi.Hospitals.Integration" and xmlns:ns0="http://Maccabi.Hospitals.Integration" this causes weird problems
i tried this xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name() = 'MessageId']">
<xsl:element name="brlName"/>
<xsl:text>BBB</xsl:text>
</xsl:template>
</xsl:stylesheet>
the reuslt is:
<brlName xmlns="">BBB</brlName>
how do i get rid of the xmlns=""???
thanks...
Upvotes: 0
Views: 45
Reputation: 4739
Change:
<xsl:element name="brlName"/>
<xsl:text>BBB</xsl:text>
To:
<xsl:element name="brlName" namespace="http://Maccabi.Hospitals.Integration">
<xsl:text>BBB</xsl:text>
</xsl:element>
But if you are going to add some element you better declare the namespace
Upvotes: 0
Reputation: 70598
The result you are actually getting is this... (Although I suspect you did mean to wrap BBB in the xsl:element tag)
<ns0:PatMedData xmlns="http://Maccabi.Hospitals.Integration"
xmlns:ns0="http://Maccabi.Hospitals.Integration">
<brlName xmlns=""/>BBB
</ns0:PatMedData>
The confusion lies in the fact the root element has two namespace declarations. Both have the same URI, but one is declared as the default namespace, and the other bound to prefix ns0. When you are outputting brlName you are outputting it with no namespace, but because the root element has a default namespace declared, it is output as <brlName xmlns=""/>
to show it has no namespace, otherwise it would be part of the existing namespace declared.
What you haven't said is whether the new brlName is meant to be in the same namespace, or whether is should not be in any namespace.
If you want brlName to be part of the same namespace as the root element, change your xsl:element to this
<xsl:element name="brlName" namespace="{namespace-uri()}" />
This will output the following:
<ns0:PatMedData xmlns="http://Maccabi.Hospitals.Integration"
xmlns:ns0="http://Maccabi.Hospitals.Integration">
<brlName/>BBB
</ns0:PatMedData>
But, if you wanted brlName not to be in any namespace, but really didn't like the xlmns=''
being present you could write a template to remove the default namespace from the root. To do this, instead of changing the xsl:element, try adding this extra template to the XSLT
<xsl:template match="/*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
This should yield the following:
<ns0:PatMedData xmlns:ns0="http://Maccabi.Hospitals.Integration">
<brlName/>BBB
</ns0:PatMedData>
It is important to re-iterate the two solutions are not the same. The first solution leaves brlName in the same namespace as the root PatMedData element. In the second, brlName is not in a namespace at all.
Upvotes: 1