Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an extremely dense XML file with 116 columns and 700+ rows on average, sometimes more sometimes less. Currently the only way we have of handling this XML file is to import it into Excel, delete the unneeded columns and rows and then manipulate the data from there.

What I'm looking for is a better way of doing this, ideally I just want to bring in 10 columns (each is defined by name) and ignore the rest of excess data. Is there a way to do this either a program or coding-wise? I took a quick look at XSLT but I'm not sure where to start.

Here is the sample file Click Here

I only need the following fields,

ns1:ActivityNumber2 ns1:Status ns1:ActivityDate ns1:TypeCodeName ns1:LandId3 ns1:ServiceClientName ns1:ActivityNumber6 ns1:ClientName8 ns1:LtoPlanNumber

ns1:ActivityNumber6 ns1:ClientName8 may occasionally be named 7 and 9 respectively for some unknown reason

share|improve this question

1 Answer 1

One way to do this is to have two templates in your XSLT: one that copies everything from the source to the destination as-is and one that removes everything but the elements you specify.

For example, if, say, I wanted to only keep the root element, <fieldToKeep1> and <fieldToKeep2> from such a document, the XSLT basis for doing this might look like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template
    match="*[not(
             contains(
               '|rootElement|fieldToKeep1|fieldToKeep2|',
               concat('|', name(), '|')
             )
           )]" />

</xsl:stylesheet>

I like this approach because, should you require other fields be kept, you can simply modify the |fieldToKeep1|fieldToKeep2| string to include the elements you want. Again, this is a basis and would probably need to be modified for your specific example.

share|improve this answer
    
Here is a sample file filedropper.com/plsrtwp059-10w5 I only need the following fields ns1:ActivityNumber2 ns1:Status ns1:ActivityDate ns1:TypeCodeName ns1:LandId3 ns1:ServiceClientName ns1:ActivityNumber6 ns1:ClientName8 ns1:LtoPlanNumber –  mbmiller34 Jun 4 '13 at 16:45
    
ns1:ActivityNumber6 ns1:ClientName8 can also be 7 and 9 respectively for some unknown reason –  mbmiller34 Jun 4 '13 at 16:47
    
@mbmiller34 - my solution above should give you an understanding of how to use XSLT to solve your actual example. Feel free to ask new questions as you attempt to solve it and the community will assist. –  ABach Jun 4 '13 at 18:22
    
Where do I start learning some basics on how to apply this concept? I can easily modify the above for my variables but I have no idea on how to use this. I could piece it together if I had a working example. I don't if I need to put the above in .xslt file and run it through another program, or if I have to put the above into my existing file. –  mbmiller34 Jun 7 '13 at 19:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.