remove 2 empty XML nodes

Neil Thompson
Neil Thompson used Ask the Experts™
on
Hi

I have the following XML which I need to remove the 2 elements that are empty, can someone provide the code to achieve this please?

I'm using ASP XMLDoc if that matters?

These need to go:
<WorksheetId></WorksheetId>
<M3JobType></M3JobType>


<?xml version="1.0" encoding="UTF-8"?>
<fod:Doc xmlns:fod="http://www.northgate-is.com/FrontOffice/BiztalkQuery">
  <fod:root>
    <record>
    <EntityId>25769877666</EntityId>
    [b]<WorksheetId></WorksheetId>
    <M3JobType></M3JobType>[/b]
    <WorksheetId>WK/201500181</WorksheetId>
    <M3JobType>Missed Bin</M3JobType>
    <WorksheetId>WK/201500182</WorksheetId>
    <M3JobType>Missed Bin</M3JobType>
</record>
</fod:root>
</fod:Doc>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
why do you want to remove them?
and is the XML the first place to start with, or is it on the generation of the XML you may want to skip them being created?

in vbscript, using the xml "plain string", I would eventually do a plain "replace" of
"<WorksheetId></WorksheetId>" by "" and "<M3JobType></M3JobType>" by "" ...
http://www.w3schools.com/vbscript/func_replace.asp
Neil ThompsonSenior Systems Developer

Author

Commented:
The XML is coming from another system so I cannot stop them being created unfortunately so need to work on the XML itself before I pump it on.

I've found out this is the way to do it:

	Set myNode = XMLDoc.SelectSingleNode("//WorksheetId")
        myNode.ParentNode.RemoveChild(myNode)
	
	Set myNode = XMLDoc.SelectSingleNode("//M3JobType")
        myNode.ParentNode.RemoveChild(myNode)

Open in new window


Regards
Neil
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
that is indeed a "pure xml" way to do it.
however, I must presume that it is likely not as efficient as the simple string replace method.
Neil ThompsonSenior Systems Developer

Author

Commented:
Thanks Guy, Appreciate the assistance

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial