Re: Namespace Issue with InnerXML
- From: Martin Honnen <mahotrash@xxxxxxxx>
- Date: Fri, 18 Jan 2008 14:28:54 +0100
Antoine Fromentin wrote:
Thanks,
I know understand why I would need to transform the XML using XSLT.
I'm sure it's probably not a complex XSLT that can remove the root element of an XML file but as I never used XMLT....
My real XML file is more complex , I'd like to create a generic XSLT which only removes the root element.
But your original change is more than only removing the root element, you want to change the namespace of the descendant nodes. When you have e.g.
<root xmlns="http://example.com/"><child><grandchild/></child></root>
then the default namespace declaration on the root element applies to the descendant elements as well thus simply remving the root element will leave the elements child and grandchild in the default namespace meaning a serialization would look like
<child xmlns="http://example.com/"><grandchild/></child>
while your orginal request was to strip the namespace from the descendants.
Furthermore it is generally not possible to remove the root element and get a well-formed document as the root element can have several child elements and that way the result could have several new root elements which is not allowed for an XML document.
Here is an XSLT style***
<xsl:style***
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:bt="http://pjc.BizTalk"
exclude-result-prefixes="bt">
<xsl:template match="/bt:*">
<xsl:apply-templates select="*[1]"/>
</xsl:template>
<xsl:template match="bt:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:style***>
that applied to your original XML document
<root xmlns="http://pjc.BizTalk">
<ns0:LaunchStoreUpload xmlns:ns0="http://pjc.schemas">
<Source>
<FTPParams>A</FTPParams>
</Source>
</ns0:LaunchStoreUpload>
</root>
gives the result
<ns0:LaunchStoreUpload xmlns:ns0="http://pjc.schemas">
<Source>
<FTPParams>A</FTPParams>
</Source>
</ns0:LaunchStoreUpload>
The first template makes sure that only the first child element of the input root element is processed, the second template strips the namespace of all elements in the namespace http://pjc.BizTalk and the third template makes sure that elements in other namespaces are copied through unchanged.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
.
- Follow-Ups:
- Re: Namespace Issue with InnerXML
- From: Antoine Fromentin
- Re: Namespace Issue with InnerXML
- References:
- Namespace Issue with InnerXML
- From: Antoine Fromentin
- Re: Namespace Issue with InnerXML
- From: Martin Honnen
- Re: Namespace Issue with InnerXML
- From: Antoine Fromentin
- Namespace Issue with InnerXML
- Prev by Date: Re: Editting an XML file
- Next by Date: Re: "The 'maxoccurs' attribute is not supported in this context"
- Previous by thread: Re: Namespace Issue with InnerXML
- Next by thread: Re: Namespace Issue with InnerXML
- Index(es):