Help, with deleteing a element

Questions about XML that are not covered by the other forums should go here.
htdub
Posts: 4
Joined: Wed Feb 04, 2009 2:30 am

Help, with deleteing a element

Post by htdub »

Hi

I'm a XML newbie, i'm apologizing ahead of time.

I have a several large XML files, some too large for Oxygen to open even.

So i've broken them up in small sets of data.

They are flat exports from a old crm, one of the data fields is old emails, including the body of the html emails. I would like to delete that element out of the XML file. I dont' need that field/data in the field. My File would probaly go from 100mb to 10mb.

Tree

Registrant#
>Name
>Phone
>Subject
>Body (need to delete this in the entire file)




Please help, I will buy you a pint in internet land.

Thanks
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Help, with deleteing a element

Post by george »

You can try an XSLT like below

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Body"/>
</xsl:stylesheet>
if you want to remove also the additional line that will remain add also the following template

Code: Select all


<xsl:template match="text()">
<xsl:if test="not(following-sibling::*[1][self::Body])"><xsl:copy-of select="."/></xsl:if>
</xsl:template>
Note that you can invoke the transformation without opening the XML file in oXygen. You can invoke it from the stylesheet or from the project panel contextual menu, see the "Apply transformation scenario" and "Transform with" actions.

If you still have problems let me know.

Regards,
George
George Cristian Bina
htdub
Posts: 4
Joined: Wed Feb 04, 2009 2:30 am

Re: Help, with deleteing a element

Post by htdub »

Works great, i'll try the transform on the big xml files.

Many thanks!
htdub
Posts: 4
Joined: Wed Feb 04, 2009 2:30 am

Re: Help, with deleteing a element

Post by htdub »

Any tips on converting XML to CSV?
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Help, with deleteing a element

Post by george »

I assume you want to convert the same document to comma separated value. For instance:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text"/>
<xsl:template match="Registrant">
<xsl:value-of select="* except Body" separator=","/>
<xsl:text>&#10;</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
will give you on a sample like:

Code: Select all


<x>
<Registrant>
<Name>name1</Name>
<Phone>phone1</Phone>
<Subject>subject1</Subject>
<Body>body1</Body>
</Registrant>
<Registrant>
<Name>name2</Name>
<Phone>phone2</Phone>
<Subject>subject2</Subject>
<Body>body2</Body>
</Registrant>
</x>
the following CSV:

Code: Select all


name1,phone1,subject1
name2,phone2,subject2
and if you want to add also the header add

Code: Select all


  <xsl:template match="/*">
<xsl:value-of select="Registrant[1]/(* except Body)/name()" separator=","/>
<xsl:text>&#10;</xsl:text>
<xsl:apply-templates/>
</xsl:template>
and you will get

Code: Select all


Name,Phone,Subject
name1,phone1,subject1
name2,phone2,subject2
Best Regards,
George
George Cristian Bina
Post Reply