Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I use XSLT and XML to compose Excel , which has dynamic fields?

Posted on 2007-04-10
9
Medium Priority
?
291 Views
Last Modified: 2013-11-18
Hi All,
  My XML file has few static and few dynamic elements, like name is static, grap points is dynamic means its computed by average of "input" ( This I would do it from my application).

<userdata>
  <name>Manjesh</name>                  // Static
  <profession>Eng</profession>            // Static      
  <gradpoints>12</gradpoints>  // Dynamic field calculated by computing the average of input
  <acadrecord>      
    <input>10</input>            // Static
    <input>12</input>            // Static
    <input>14</input>            // Static
  </acadrecord>
</userdata>


Excel Sheet
Name            Profession            GradPoints
Manjesh            Engg            12

Once its done I would like to show only few relavent fields in my excel as mentioned above. Unfortunatley I dont have enough time to experiment XSLT and mapping to Excel. So if any expert can give the XSLT and steps for importing the same to excel .

Thanks for your earliest attention.



0
Comment
Question by:Manjesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 total points
ID: 18887672
There are several ways to get to Excel from XML.
Depending on your needs you could use an XML schema, bind it to a table and import the XML directly. Or you could use special tools dediacted to writing Excel files.

If you want to use XSLT, again you have some options (though you can't write binary Excel)
You could write the Excel XML format. You then have most of the bells and whistles, but it is a complex format.
You can also write simple HTML tables, in order to pass some formatting.
In simple cases, you can write tab seperated text, which you then import in Excel.

That is what this example does

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="newline" select="'&#13;&#10;'"/>
    <xsl:param name="tab" select="'&#9;'"/>
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:text>name</xsl:text>
        <xsl:value-of select="$tab"/>
        <xsl:text>profession</xsl:text>
        <xsl:value-of select="$tab"/>
        <xsl:text>gradpoints</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:apply-templates select="//userdata"/>
    </xsl:template>
    <xsl:template match="userdata">
        <xsl:value-of select="name"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="profession"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="gradpoints"/>
        <xsl:value-of select="$newline"/>
    </xsl:template>
</xsl:stylesheet>

cheers

Geert
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18887720
I don't know how much effort you do for calculating the mean,
but this is straightforward in XSLT

   <xsl:template match="userdata">
        <xsl:value-of select="name"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="profession"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="sum(acadrecord/input) div count(acadrecord/input)"/>
        <xsl:value-of select="$newline"/>
    </xsl:template>
0
 
LVL 1

Author Comment

by:Manjesh
ID: 18887870
Thanks for your quik reply.

Assuming I have the XML  and XSLT as mentioned above , How can I show the data in Excel?

Like how can I import XSLT in excel and get the view of my data ?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18887907
use a commandline XSLT processor, or set the proces up in ASP
save the result file as *.xls and double-click
or serve the result out with a ms-application-excel mime-type
0
 
LVL 1

Author Comment

by:Manjesh
ID: 18888270
Finally I found a VB Script which can convert XML( XSL ) to CSV file. But instead of being comma separeated it separated by ;

When I run the script its seperated by ";(Semicolon)" instead od Comma. Experts any clue how to tweak the code , to make it comma seperated.

C:\Test\testme.xsl

<?xml version="1.0"?>
<xsl:stylesheet version = "1.0"      xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements = "gazetteer"/>
<xsl:template match="place">
<xsl:text/><xsl:value-of select="normalize-space(name)"/>,<xsl:text/>
<xsl:value-of select="normalize-space(county)"/>,<xsl:text/>
<xsl:value-of select="normalize-space(ngr/easting)"/>,<xsl:text/>
<xsl:value-of select="normalize-space(ngr/northing)"/>,<xsl:text/>
<xsl:value-of select="normalize-space(geographical/latitude)"/>,<xsl:text/>
<xsl:value-of select="normalize-space(geographical/longitude)"/><xsl:text/>          
<xsl:text disable-output-escaping = "yes" >
</xsl:text>
</xsl:template>
</xsl:stylesheet>

C:\Test\testme.xml

<?xml version = "1.0"?>
<gazetteer>
  <place>
    <name>Wolverhampton</name>
    <county>West Midlands</county>
    <ngr>
          <easting>390</easting>
            <northing>298</northing>
    </ngr>
    <geographical>
          <latitude>52.583</latitude>
            <longitude>-2.133</longitude>
    </geographical>
  </place>
</gazetteer>

C:\Test\testme.vbs


  Dim xmlSource
  Dim xmlXForm
  Dim strErr
  Dim strResult
 
  Dim fso , file
  Dim strPath
  Const ForReading = 1
  Const ForWriting = 2
  Const ForAppending = 8
 
  Set xmlSource = CreateObject("MSXML.DOMDocument")
  Set xmlXForm = CreateObject("MSXML.DOMDocument")
 
  xmlSource.validateOnParse = True
  xmlXForm.validateOnParse = True
  xmlSource.async = False
  xmlXForm.async = False
 
  xmlSource.Load "c:\test\testme.xml"  ' This loads the text that I want to transform
  xmlXForm.Load "c:\test\testme.xsl" ' This loads the XSLT transform
 
  strResult = xmlSource.transformNode(xmlXForm) ' This transforms the data in xmlSource
 
  Set fso = CreateObject("Scripting.FileSystemObject")
  strPath = "c:\test\testme.csv"
 
  Set file = fso.opentextfile(strPath, ForWriting, True)

  file.write strResult
 
  file.Close
 
  Set file = Nothing
  Set fso = Nothing
  Set xmlSource = Nothing
  Set xmlXForm = Nothing


Thanks for your earliest attention.

Regards,

Manjesh Gowda S H
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18888291
If I look at the XSLT, I would expect the seperator being ','
because that is what is in the code

Have you tried using your own XML, with my XSLT in combination with this VB script?
It should give you what you need

cheers

Geert
0
 
LVL 1

Author Comment

by:Manjesh
ID: 18888345
Hi Gertone,
  Thanks for your quick reply and handy solution, I was able to get it done through VB Script.

Thanks a lot again.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18888459
welcome
0
 
LVL 1

Author Comment

by:Manjesh
ID: 18896594
Hi Greet,
  I got the thing workign fine...but a small hitch. I have the XSL and XML file and I want to open it excel, I dont want to create a CSV file using VB Script. I want to directly import in Excel. Any suggestions?

Thanks for your earliest response.

Regards,

Manjesh Gowda S H
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question