perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » focus » msxml » ask a question » past questions & answers Thursday, 11 October 2007
 
NEWS
MSXML 4.0 SP2 now available!

 
MSXML
Basics
DOM
SAX
XPath
XSLT
Schemas
SOM
HTTP Access
.NET
Data Islands
Ask a Question
   Past Q&As
C++ Samples
DLL/Version Info
Reference Guide
Books
KB Articles
   HOW TO
   SAMPLE
   INFO
   BUG/PRB
   FIX
   Misc.
MSXML Tips
   August 2002
   September 2002
MSXML Tools

Microsoft XML Core Services


Go back to list of previously asked questions and answers

Question: I have an XML document whose top element contains default namespace (xmlns="xxxx"). I would like to get rid of this namespace; and add a child element that contains the default namespace URI. I am using MSXML 4.0 with Visual Basic. I tried removeAttribute and objXMLDoc.namespace.remove, but they does not seem to work (first one does not do anything, second one gives error that object is read only). Please help.
Asked By: Guest
Viewed: 3439
Answer: Even though xmlns looks like attribute node to the top element, and even though you can access this using topElement.Attributes.getNamedItem("xmlns") or topElement.getAttributeNode("xmlns"), still removeAttribute and objXMLDoc.namespace.remove will not work - because of the fact that namespace is really part of the element name, which is read only. The only option we can think of is to re-create the document - to do this, you can use DOM or apply XSLT.

Here is an example that we wrote for you. It gets rid of the default namespace and adds a child element node. We use XSLT for this.

Source XML Document:
<?xml version="1.0" encoding="utf-8"?>
<empDetails xmlns="33BFA363-E5F3-43da-B895-302C108ADCAC">
    <emp id="1" firstName="John" lastName="smith"/>
    <emp id="2" firstName="Mark" lastName="Robinson"/>
    <emp id="3" firstName="Dave" lastName="Schur"/>
</empDetails>


XSLT Stylesheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:x="33BFA363-E5F3-43da-B895-302C108ADCAC"
   exclude-result-prefixes="x">

   <xsl:output method="xml" encoding="UTF-8"/>
   
   <xsl:template match="/">
      <empDetails>
         <Meta name="schema" content="33BFA363-E5F3-43da-B895-302C108ADCAC" />
         <xsl:apply-templates />
      </empDetails>
   </xsl:template>
   
   <xsl:template match="x:emp">
      <emp>
         <xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>
         <xsl:attribute name="firstName"><xsl:value-of select="@firstName" /></xsl:attribute>
         <xsl:attribute name="lastName"><xsl:value-of select="@lastName" /></xsl:attribute>
      </emp>
   </xsl:template>
     
</xsl:stylesheet>


Visual Basic code to apply the stylesheet:
    Dim objXML As New MSXML2.DOMDocument40
    Dim objXSL As New MSXML2.DOMDocument40
    
    objXML.async = False
    objXML.validateOnParse = False
    objXML.resolveExternals = False
    
    If objXML.Load("c:\emp.xml") Then
        
        objXSL.async = False
        objXSL.validateOnParse = False
        objXSL.resolveExternals = False
        
        If objXSL.Load("c:\emp.xsl") Then
            'MsgBox objXML.transformNode(objXSL)
            objXML.transformNodeToObject objXSL, objXML
            MsgBox objXML.xml
        Else
            With objXSL.parseError
                MsgBox "Error: " & .errorCode & _
                    ": " & .reason & vbNewLine & .srcText
            End With
        End If
    Else
        With objXML.parseError
            MsgBox "Error: " & .errorCode & _
                ": " & .reason & vbNewLine & .srcText
        End With
    End If


Go back to list of previously asked questions and answers
  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy