Results 1 to 3 of 3

Thread: [RESOLVED] Getting rid of the empty sections of the XML

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Resolved [RESOLVED] Getting rid of the empty sections of the XML

    I have an XML file and some of the sections in the file do not contain any data. Before I send this XML to a webservice, I would like to know if there's any way to get rid of the empty sections.

    For example:
    PHP Code:
    <Root>
    <
    MainContainer>
    <
    SectionOne>
     <
    Collection/>
    </
    SectionOne>
    <
    SectionTwo>
    <
    Collection>
       <
    ItemOne>Has Value</ItemOne>
    </
    Collection>
    </
    SectionTwo>
    </
    MainContainer>
    </
    Root

    In the example above, I would want SectionOne to completely disappear and then the XML cleaned up will look like the one below.

    PHP Code:
    <Root>
    <
    MainContainer>
    <
    SectionTwo>
    <
    Collection>
       <
    ItemOne>Has Value</ItemOne>
    </
    Collection>
    </
    SectionTwo>
    </
    MainContainer>
    </
    Root

    How can I do this ? This is an example. There are several sections in the XML file and they are not named with the word section in there.
    I need to do this on more than 100 K files to shrink their size.


    DeanMc used to have an XAML formatter online. Anybody know what happened to it?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Getting rid of the empty sections of the XML

    This vbscript should work. Here I'm using files as input and output but you could easily use string as input and/or output. Just look at the comments.

    Code:
    Option Explicit
    ' Required variables
    Dim strXml
    Dim strOutput
    Dim XMLDocument
    Dim XSLDocument
    
    	' set the paths to the files
    	strXml = "C:\<path to file>\input.xml"
    	strOutput = "C:\<path to file>\output.xml"
    	
    	' Load the xml into a document object
    	Set XMLDocument = CreateDoc
    	XMLDocument.Load strXML
    	' Use LoadXml if the xml is coming from a string instead
    	' XMLDocument.LoadXML strXML
    	
    	' Load the xsl into a document object
    	Set XSLDocument = CreateDoc
    	XSLDocument.LoadXML GetXSL
    
    	' Use transformNode to remove the empty node
    	XMLDocument.LoadXml XMLDocument.transformNode(XSLDocument)
    	
    	' I'm just saving the transformed file. To access the
    	' transformed xml you would reference XMLDocument.Xml
    	XMLDocument.Save strOutput
    	
    
     Private Function CreateDoc()
    	Dim objXML 'As MSXML2.DOMDocument
    	Set objXML = CreateObject("MSXML2.DOMDocument")
    	'OR if using on the server
    	'Set objXML = Server.CreateObject("MSXML2.DOMDocument")
    	With objXML
    	    .async = False 
    	    .validateOnParse = False 
    	    .preserveWhiteSpace = True
    	    .resolveExternals = False 
    	End With
    		
    	Set CreateDoc = objXML
    	Set objXML = Nothing
    End Function
    
    Private Function GetXSL()
    Dim strXSL(12)
    	strXSL(0) = "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
    	strXSL(1) = "<xsl:output omit-xml-declaration='yes' indent='yes'/>"
    	strXSL(2) = "<xsl:strip-space elements='*' />"
    	strXSL(3) = "<xsl:template match='@* | node()'>"
    	strXSL(4) = "<xsl:if test="". != '' or ./@* != ''"">"
    	strXSL(5) = "<xsl:copy>"
    	strXSL(6) = "<xsl:apply-templates select='@*|node()'/>"
    	strXSL(7) = "</xsl:copy>"
    	strXSL(8) = "</xsl:if>"
    	strXSL(9) = "</xsl:template>"
    	strXSL(10) = "<xsl:template match='section[not(.//text())]'/>"
    	strXSL(11) = "<xsl:template match='*[ancestor::section and not(text())]'/>"
    	strXSL(12) = "</xsl:stylesheet>"
    	
    	GetXSL = Join(strXSL, vbCrLf)
    End Function

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Getting rid of the empty sections of the XML

    Nice one Mark! I tried a DOM approach but got nowhere — it was much more difficult than I thought.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width