473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

remove nodes that belong to a namespace

Hi all,
Following is a portion of an XML document. I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following
example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?

<ns0:Proposal>
<ns0:Company>
<w:p>
<w:r>
<w:t>test company</w:t>
</w:r>
</w:p>
</ns0:Company>
</ns0:Proposal>

I tried following xpath just to select all nodes that belong to ns0
namespace. But it did not work.
XmlNodeList oNodeList = oXMLWordDoc.SelectNodes("//ns0:*",oNSMgr);

I also tried this with no avail.

XmlNodeList oNodeList = oXMLWordDoc.SelectNodes("ns0:*",oNSMgr);

Thanks.
Nov 12 '05 #1
6 3485
"Nikhil Patel" <ni********@aol.com> wrote in message news:Oj*************@TK2MSFTNGP10.phx.gbl...
I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following
example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?


Simple matter of walking the node tree, throwing out nodes with
the namespace URI corresponding to the ns0 prefix, while cloning
and "bubbling" up their children.

Here's a little recursive routine that should accomplish that:

- - - ElementNsFilter.cs (partial)
// . . .
public void RemoveNS( XmlNode node, string nsURI)
{
// Descend first.
if ( node.HasChildNodes )
RemoveNS( node.FirstChild, nsURI);

// Then move laterally.
if ( node.NextSibling != null )
RemoveNS( node.NextSibling, nsURI);

// Then ignore text, PI, comment, CDATA nodes.
if ( node.NodeType == XmlNodeType.Element )

// Your filter criterion goes here.
if ( node.NamespaceURI == nsURI )
{

// Must copy the child nodes to bubble up.
XmlNodeList xnl = node.ChildNodes;
int i = -1, iMax = xnl.Count;
ArrayList copies = new ArrayList( xnl.Count);
while ( ++i < iMax )
copies.Add( xnl[ i].Clone());

// Insert child nodes in document order as they appear
// beneath this node, prior to it in it's parent's node coll.
i = -1;
while ( ++i < iMax )
node.ParentNode.InsertBefore( (XmlNode)( copies[ i]), node);

// Remove this node (the copied children now exist in
// the parent's node coll).
node.ParentNode.RemoveChild( node);
}
}
// . . .
- - -

The reason a copy of child nodes is necessary, is that the more straight-
forward loop:

foreach( XmlNode xn in node.ChildNodes)
node.ParentNode.InsertBefore( xn, node);

Actually incurs unintended side effects in the ChildNodes collection. The
insertion of a child node before this node in the parent collection "bumps"
the nodes ahead of 'node', and the XmlNode iterator will end up counting
'node' as one of it's own children.

Another point to make, is that while it isn't the situation with your example
XML instance document, removing all elements of a specific namespace
can leave you with an invalid XML document. Suppose the document were
as follows:

- - - ValidXml.xml
<?xml version="1.0" ?>
<ns0:Proposal xmlns:ns0="http://schemas.example.com/FIRST" xmlns:w="http://schemas.example.com/SECOND">
<w:p>
</w:p>
<w:r>
</w:r>
</ns0:Proposal>
- - -

Naive processing of this document will result in the following invalid XML,
because an XML document must have exactly one document element
(this instance has two).

- - - InvalidXml.xml
<?xml version="1.0" ?>
<w:p>
</w:p>
<w:r><!-- ERROR: Second document root element. -->
</w:r>
- - -

Thus, when calling RemoveNS( ) or something similar, it's highly recommended
you nest your document in an artificial root element (whose namespace URI is
different from that you are removing), like this.

// Move the existing XML document inside of an artificial root, in
// case the results of namespace element removal would produce
// multiple document elements.

XmlElement root = xmlDoc.CreateElement("root");
root.AppendChild( xmlDoc.DocumentElement);
xmlDoc.AppendChild( root);

RemoveNS( root, "http://schemas.example.com/FIRST");

Then you can properly test the resulting document after RemoveNS()
has been called by counting the number of child nodes (if there are
more than one element node beneath the artificial root, then you would
throw an exception that the result of the removal of all elements of the
requested namespace is an invalid XML document with multiple root
elements).

if ( xmlDoc.DocumentElement.ChildNodes.Count > 1 )
{
throw new XmlException( String.Format( "Result of removing all"+
" elements with namespace URI {0} is a document with multiple"+
" root elements.", null);
}

Otherwise, you can pull out the artificial root element and elevate the
resulting node tree (sans elements of the namespace URI you wanted
to filter out).
Derek Harmon
Nov 12 '05 #2
"Nikhil Patel" <ni********@aol.com> wrote in message news:Oj*************@TK2MSFTNGP10.phx.gbl...
I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following
example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?


Simple matter of walking the node tree, throwing out nodes with
the namespace URI corresponding to the ns0 prefix, while cloning
and "bubbling" up their children.

Here's a little recursive routine that should accomplish that:

- - - ElementNsFilter.cs (partial)
// . . .
public void RemoveNS( XmlNode node, string nsURI)
{
// Descend first.
if ( node.HasChildNodes )
RemoveNS( node.FirstChild, nsURI);

// Then move laterally.
if ( node.NextSibling != null )
RemoveNS( node.NextSibling, nsURI);

// Then ignore text, PI, comment, CDATA nodes.
if ( node.NodeType == XmlNodeType.Element )

// Your filter criterion goes here.
if ( node.NamespaceURI == nsURI )
{

// Must copy the child nodes to bubble up.
XmlNodeList xnl = node.ChildNodes;
int i = -1, iMax = xnl.Count;
ArrayList copies = new ArrayList( xnl.Count);
while ( ++i < iMax )
copies.Add( xnl[ i].Clone());

// Insert child nodes in document order as they appear
// beneath this node, prior to it in it's parent's node coll.
i = -1;
while ( ++i < iMax )
node.ParentNode.InsertBefore( (XmlNode)( copies[ i]), node);

// Remove this node (the copied children now exist in
// the parent's node coll).
node.ParentNode.RemoveChild( node);
}
}
// . . .
- - -

The reason a copy of child nodes is necessary, is that the more straight-
forward loop:

foreach( XmlNode xn in node.ChildNodes)
node.ParentNode.InsertBefore( xn, node);

Actually incurs unintended side effects in the ChildNodes collection. The
insertion of a child node before this node in the parent collection "bumps"
the nodes ahead of 'node', and the XmlNode iterator will end up counting
'node' as one of it's own children.

Another point to make, is that while it isn't the situation with your example
XML instance document, removing all elements of a specific namespace
can leave you with an invalid XML document. Suppose the document were
as follows:

- - - ValidXml.xml
<?xml version="1.0" ?>
<ns0:Proposal xmlns:ns0="http://schemas.example.com/FIRST" xmlns:w="http://schemas.example.com/SECOND">
<w:p>
</w:p>
<w:r>
</w:r>
</ns0:Proposal>
- - -

Naive processing of this document will result in the following invalid XML,
because an XML document must have exactly one document element
(this instance has two).

- - - InvalidXml.xml
<?xml version="1.0" ?>
<w:p>
</w:p>
<w:r><!-- ERROR: Second document root element. -->
</w:r>
- - -

Thus, when calling RemoveNS( ) or something similar, it's highly recommended
you nest your document in an artificial root element (whose namespace URI is
different from that you are removing), like this.

// Move the existing XML document inside of an artificial root, in
// case the results of namespace element removal would produce
// multiple document elements.

XmlElement root = xmlDoc.CreateElement("root");
root.AppendChild( xmlDoc.DocumentElement);
xmlDoc.AppendChild( root);

RemoveNS( root, "http://schemas.example.com/FIRST");

Then you can properly test the resulting document after RemoveNS()
has been called by counting the number of child nodes (if there are
more than one element node beneath the artificial root, then you would
throw an exception that the result of the removal of all elements of the
requested namespace is an invalid XML document with multiple root
elements).

if ( xmlDoc.DocumentElement.ChildNodes.Count > 1 )
{
throw new XmlException( String.Format( "Result of removing all"+
" elements with namespace URI {0} is a document with multiple"+
" root elements.", null);
}

Otherwise, you can pull out the artificial root element and elevate the
resulting node tree (sans elements of the namespace URI you wanted
to filter out).
Derek Harmon
Nov 12 '05 #3
Nikhil Patel wrote:
Following is a portion of an XML document. I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following
example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?

<ns0:Proposal>
<ns0:Company>
<w:p>
<w:r>
<w:t>test company</w:t>
</w:r>
</w:p>
</ns0:Company>
</ns0:Proposal>


There is zillon ways of doing it. Which one is better depends on
particular details of your design.
Here is XSLT solution (which is usually the most simple and maintainable
one):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="foo">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Proposal|ns0:Company">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Nikhil Patel wrote:
Following is a portion of an XML document. I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following
example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?

<ns0:Proposal>
<ns0:Company>
<w:p>
<w:r>
<w:t>test company</w:t>
</w:r>
</w:p>
</ns0:Company>
</ns0:Proposal>


There is zillon ways of doing it. Which one is better depends on
particular details of your design.
Here is XSLT solution (which is usually the most simple and maintainable
one):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="foo">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Proposal|ns0:Company">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #5
Thanks.

"Derek Harmon" <lo*******@msn.com> wrote in message
news:ud**************@TK2MSFTNGP11.phx.gbl...
"Nikhil Patel" <ni********@aol.com> wrote in message news:Oj*************@TK2MSFTNGP10.phx.gbl...
I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?


Simple matter of walking the node tree, throwing out nodes with
the namespace URI corresponding to the ns0 prefix, while cloning
and "bubbling" up their children.

Here's a little recursive routine that should accomplish that:

- - - ElementNsFilter.cs (partial)
// . . .
public void RemoveNS( XmlNode node, string nsURI)
{
// Descend first.
if ( node.HasChildNodes )
RemoveNS( node.FirstChild, nsURI);

// Then move laterally.
if ( node.NextSibling != null )
RemoveNS( node.NextSibling, nsURI);

// Then ignore text, PI, comment, CDATA nodes.
if ( node.NodeType == XmlNodeType.Element )

// Your filter criterion goes here.
if ( node.NamespaceURI == nsURI )
{

// Must copy the child nodes to bubble up.
XmlNodeList xnl = node.ChildNodes;
int i = -1, iMax = xnl.Count;
ArrayList copies = new ArrayList( xnl.Count);
while ( ++i < iMax )
copies.Add( xnl[ i].Clone());

// Insert child nodes in document order as they appear
// beneath this node, prior to it in it's parent's node coll.
i = -1;
while ( ++i < iMax )
node.ParentNode.InsertBefore( (XmlNode)( copies[ i]),

node);
// Remove this node (the copied children now exist in
// the parent's node coll).
node.ParentNode.RemoveChild( node);
}
}
// . . .
- - -

The reason a copy of child nodes is necessary, is that the more straight-
forward loop:

foreach( XmlNode xn in node.ChildNodes)
node.ParentNode.InsertBefore( xn, node);

Actually incurs unintended side effects in the ChildNodes collection. The
insertion of a child node before this node in the parent collection "bumps" the nodes ahead of 'node', and the XmlNode iterator will end up counting
'node' as one of it's own children.

Another point to make, is that while it isn't the situation with your example XML instance document, removing all elements of a specific namespace
can leave you with an invalid XML document. Suppose the document were
as follows:

- - - ValidXml.xml
<?xml version="1.0" ?>
<ns0:Proposal xmlns:ns0="http://schemas.example.com/FIRST" xmlns:w="http://schemas.example.com/SECOND"> <w:p>
</w:p>
<w:r>
</w:r>
</ns0:Proposal>
- - -

Naive processing of this document will result in the following invalid XML, because an XML document must have exactly one document element
(this instance has two).

- - - InvalidXml.xml
<?xml version="1.0" ?>
<w:p>
</w:p>
<w:r><!-- ERROR: Second document root element. -->
</w:r>
- - -

Thus, when calling RemoveNS( ) or something similar, it's highly recommended you nest your document in an artificial root element (whose namespace URI is different from that you are removing), like this.

// Move the existing XML document inside of an artificial root, in
// case the results of namespace element removal would produce
// multiple document elements.

XmlElement root = xmlDoc.CreateElement("root");
root.AppendChild( xmlDoc.DocumentElement);
xmlDoc.AppendChild( root);

RemoveNS( root, "http://schemas.example.com/FIRST");

Then you can properly test the resulting document after RemoveNS()
has been called by counting the number of child nodes (if there are
more than one element node beneath the artificial root, then you would
throw an exception that the result of the removal of all elements of the
requested namespace is an invalid XML document with multiple root
elements).

if ( xmlDoc.DocumentElement.ChildNodes.Count > 1 )
{
throw new XmlException( String.Format( "Result of removing all"+
" elements with namespace URI {0} is a document with multiple"+ " root elements.", null);
}

Otherwise, you can pull out the artificial root element and elevate the
resulting node tree (sans elements of the namespace URI you wanted
to filter out).
Derek Harmon

Nov 12 '05 #6
Thanks.

"Derek Harmon" <lo*******@msn.com> wrote in message
news:ud**************@TK2MSFTNGP11.phx.gbl...
"Nikhil Patel" <ni********@aol.com> wrote in message news:Oj*************@TK2MSFTNGP10.phx.gbl...
I need to remove all nodes
that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?


Simple matter of walking the node tree, throwing out nodes with
the namespace URI corresponding to the ns0 prefix, while cloning
and "bubbling" up their children.

Here's a little recursive routine that should accomplish that:

- - - ElementNsFilter.cs (partial)
// . . .
public void RemoveNS( XmlNode node, string nsURI)
{
// Descend first.
if ( node.HasChildNodes )
RemoveNS( node.FirstChild, nsURI);

// Then move laterally.
if ( node.NextSibling != null )
RemoveNS( node.NextSibling, nsURI);

// Then ignore text, PI, comment, CDATA nodes.
if ( node.NodeType == XmlNodeType.Element )

// Your filter criterion goes here.
if ( node.NamespaceURI == nsURI )
{

// Must copy the child nodes to bubble up.
XmlNodeList xnl = node.ChildNodes;
int i = -1, iMax = xnl.Count;
ArrayList copies = new ArrayList( xnl.Count);
while ( ++i < iMax )
copies.Add( xnl[ i].Clone());

// Insert child nodes in document order as they appear
// beneath this node, prior to it in it's parent's node coll.
i = -1;
while ( ++i < iMax )
node.ParentNode.InsertBefore( (XmlNode)( copies[ i]),

node);
// Remove this node (the copied children now exist in
// the parent's node coll).
node.ParentNode.RemoveChild( node);
}
}
// . . .
- - -

The reason a copy of child nodes is necessary, is that the more straight-
forward loop:

foreach( XmlNode xn in node.ChildNodes)
node.ParentNode.InsertBefore( xn, node);

Actually incurs unintended side effects in the ChildNodes collection. The
insertion of a child node before this node in the parent collection "bumps" the nodes ahead of 'node', and the XmlNode iterator will end up counting
'node' as one of it's own children.

Another point to make, is that while it isn't the situation with your example XML instance document, removing all elements of a specific namespace
can leave you with an invalid XML document. Suppose the document were
as follows:

- - - ValidXml.xml
<?xml version="1.0" ?>
<ns0:Proposal xmlns:ns0="http://schemas.example.com/FIRST" xmlns:w="http://schemas.example.com/SECOND"> <w:p>
</w:p>
<w:r>
</w:r>
</ns0:Proposal>
- - -

Naive processing of this document will result in the following invalid XML, because an XML document must have exactly one document element
(this instance has two).

- - - InvalidXml.xml
<?xml version="1.0" ?>
<w:p>
</w:p>
<w:r><!-- ERROR: Second document root element. -->
</w:r>
- - -

Thus, when calling RemoveNS( ) or something similar, it's highly recommended you nest your document in an artificial root element (whose namespace URI is different from that you are removing), like this.

// Move the existing XML document inside of an artificial root, in
// case the results of namespace element removal would produce
// multiple document elements.

XmlElement root = xmlDoc.CreateElement("root");
root.AppendChild( xmlDoc.DocumentElement);
xmlDoc.AppendChild( root);

RemoveNS( root, "http://schemas.example.com/FIRST");

Then you can properly test the resulting document after RemoveNS()
has been called by counting the number of child nodes (if there are
more than one element node beneath the artificial root, then you would
throw an exception that the result of the removal of all elements of the
requested namespace is an invalid XML document with multiple root
elements).

if ( xmlDoc.DocumentElement.ChildNodes.Count > 1 )
{
throw new XmlException( String.Format( "Result of removing all"+
" elements with namespace URI {0} is a document with multiple"+ " root elements.", null);
}

Otherwise, you can pull out the artificial root element and elevate the
resulting node tree (sans elements of the namespace URI you wanted
to filter out).
Derek Harmon

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: snarks | last post by:
Is there an XML file editor that will allow me to delete nodes? I tried XMLSpy Home Edition and I can't get it to do this. Ideally I would like to collapse all nodes and just select and delete the...
0
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
0
by: dhnriverside | last post by:
Hi guys What's up with this code? TreeNode oldNode = new TreeNode(); oldNode = treeMyTree.GetNodeFromIndex(treeMyTree.SelectedNodeIndex); treeMyTree.Nodes.Remove(oldNode); It says the Node...
5
by: Christoffer | last post by:
Hi, I'm having trouble with namespaces when importing nodes. I'd like to get this output: <exampleRoot xmlns="http://mynamespace"> <row Lsm_Info="ABC123" /> <row Lsm_Info="DEF456" />...
3
by: Woon Kiat | last post by:
I have this Xml file, bookstore.xml <Bookstore xmlns="http://www.book.com"> <Shelve index="1"> <Book name="Da Vinci Code" /> </Shelve> <Shelve index="2"> <Book name="The Holy Grail" />...
1
by: martin1 | last post by:
Hi, All, I want to remove xml namespace attribute (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance") in the following file <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...
6
by: pavel.repkin | last post by:
Hey! How would you do the following task? Let you have an XML tree on input. Suppose, there is a special kind of node you want to remove. Let it have "bad" name. Each "bad" node has a parent...
0
by: nmaddock | last post by:
Hi Guys, seen this example on msdn, how can i remove the invalide nodes and elements when they fail against the schema and then save to a new xml document. any help would be great my code is...
11
by: Michael Bray | last post by:
I'm playing around with XElement stuff, and I've come across a difficulty. The XML document that I'm reading contains an xmlns declaration on the main node... <root xmlns="http://www.me.com">...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.