Captainslow
Captainslow

Reputation: 57

Using xml and xsl to produce a xlsx file

I am trying to produce a xlsx report from a bunch of data. I have the mobile number, area code, and name of friends, and want to insert them into the xlsx file using xml and xsl. I am having a hard time inserting the header of the data, and I can't seem to get the data on each column. Now, their just mashed together, with no seperator.

XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
    <friends>
        <jx:forEach var="friendsDetail" items="${friendList}">
            <friend
                    Name="${friendsDetail.getName()}/"                                                
                    MobileNumber="${friendsDetail.getMobileNumber()}"
                    AreaCode="${friendsDetail.getAreaCode()}"
             />
        </jx:forEach>
    </friends>
</jx:template>

XSL file

<?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="*" />
  <xsl:template match="friend">
  <xsl:value-of select="@Name" /><xsl:text/>
  <xsl:value-of select="@MobileNumber" /><xsl:text/>
  <xsl:value-of select="@AreaCode" /><xsl:text/>
</xsl:template>
</xsl:stylesheet>

So basically, I want the data to come out in an xlsx file as follows:

       Column 1.  Column 2.       Column 3.

Row 1. Name       Mobilenumber    Areacode

Row 2. Peter      48785635        4817

Any tips will be greatly appreciated!

The output comes like this.

enter image description here

Upvotes: 0

Views: 4947

Answers (1)

Tim C
Tim C

Reputation: 70618

If you want a tab-delimited file to import into Excel, you could start of by defining a parameter to hold the character for the delimited

<xsl:param name="separator" select="'&#x9;'" />

You could use a xsl:variable but a param could be overridden by the calling application, allowing you to easily swap to a comma.

You can then easily output the separator in your template

<xsl:value-of select="@Name" />
<xsl:value-of select="$separator" />

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<xsl:param name="separator" select="'&#x9;'" />

<xsl:strip-space elements="*" />

<xsl:template match="friends">
  <xsl:text>Name</xsl:text>
  <xsl:value-of select="$separator" />
  <xsl:text>Mobile Number</xsl:text>
  <xsl:value-of select="$separator" />
  <xsl:text>Area Code</xsl:text>
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates select="friend" />
</xsl:template>

<xsl:template match="friend">
  <xsl:value-of select="@Name" />
  <xsl:value-of select="$separator" />
  <xsl:value-of select="@MobileNumber" />
  <xsl:value-of select="$separator" />
  <xsl:value-of select="@AreaCode" />
  <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions