Re: Namespace Issue with InnerXML

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 stylesheet

<xsl:stylesheet
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:stylesheet>

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/
.



Relevant Pages

  • Re: Cannot find root element in XML file
    ... Well if you simply want to access the root element then ... will do whatever the name of the element is and whatever namespace that ... documents nodes fails. ... parsed correctly by .NET XML framework, ...
    (microsoft.public.dotnet.xml)
  • Re: Openxml() Edge Table - root element problem
    ... Based on them I did some reading on xml ... element and its descendants elements are in the namespace and for XPath ... prefix to the namespace and use that prefix. ... attribute in the root element, but looks like any name that fits the required ...
    (microsoft.public.sqlserver.xml)
  • Re: Openxml() Edge Table - root element problem
    ... parameter in sp_xml_preparedocument (instead of using one of the other root element attributes)? ... http://www.ups.com/XMLSchema/EBR/Billing/v1 is the namespace in the default namespace declaration of the root element of your posted XML sample. ... Or should I be searching for the applicable string in the original .xml doc for each new doc? ... or is the openxml() parse strictly internal? ...
    (microsoft.public.sqlserver.xml)
  • Re: Editing XML
    ... I wonder how text and child nodes are distinguished in the XML text. ... The first is the processing instruction, which you'll typically ignore in XSLT. ... THe next is the element node named "tag" and the last is the text node. ... There is always exactly one root element in a valid XML document. ...
    (comp.lang.pascal.delphi.misc)
  • newbie: writing to xml and namespaces
    ... I'm having some issues when programmatically editing an XML file. ... XmlTextWriter and add the default namespace declaration when writing the ... appears other than in the root element. ...
    (microsoft.public.dotnet.languages.csharp)