xslt calculating insurance writeoff not working - xslt

I am trying to calculate the contractual write off for medical insurance but the calculation doesn't seem to be working. For example, I need to take the value of the charge attribute on the <charge> tag and subtract the value of the amount attribute on the <adjustment> tag but only when the group and code attributes on the <adjustment> tag are: group ='CO' and code = '45' or '385'
I was able to get everything but the calculations working. I've tried looking at several answers on this forum but am obviously missing something. Any help would be appreciated.
Here is my XML (simplified):
<result check_number="05173408" ...>
<claim ... total_charge="720" total_paid="43">
<charge charge="200">
<adjustment amount="125" code="45" group="CO" />
<adjustment amount="35" code="385" group="CO" />
<adjustment amount="20" code="3" group="PR" />
</charge>
<charge charge="70">
<adjustment amount="70" code="204" group="PR" />
</charge>
<charge charge="300">
<adjustment amount="273" code="45" group="CO" />
<adjustment amount="15" code="3" group="PR" />
</charge>
<charge charge="150">
<adjustment amount="139" code="45" group="CO" />
</charge>
</claim>
</result>
Here is my xslt to date:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<!-- maps to the beginning result tag -->
<xsl:for-each select="/result/claim/charge">
<!-- amount allowed by the insurance for line item -->
<COL>
<DATA>
<xsl:apply-templates select="adjustment" mode="calcAdjTotal" >
</xsl:apply-templates>
</DATA>
</COL>
</xsl:for-each>
<!-- Template created to test Parfait's suggestion -->
<xsl:template match="adjustment" mode="calcAdjTotal" >
<xsl:variable name="condition" select="adjustment[#group='CO' and (#code='45' or #code='385')]"></xsl:variable>
<xsl:value-of select="../#charge - sum($condition/#amount)" />
</xsl:if>
</xsl:template>
</xsl:template>
</xsl:stylesheet>
Here is my output: (updated output)
<COL>
</DATA>
</COL>
<COL>
</DATA>
</COL>
<COL>
</DATA>
</COL>
<COL>
</DATA>
</COL>
Here's what I want to get:
<COL>
<DATA>
40
</DATA>
</COL>
<COL>
<DATA>
</DATA>
</COL>
<COL>
<DATA>
27
</DATA>
</COL>
<COL>
<DATA>
11
</DATA>
</COL>
The format will then be used to import data into Filemaker.

Consider wrapping your conditional equation in an <xsl:if>. Additionally, you can do so with a variable storing the conditional value to avoid repetition:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<ROOT>
<xsl:apply-templates select="*"/>
</ROOT>
</xsl:template>
<xsl:template match="charge">
<xsl:variable name="condition" select="adjustment[(#code='45' or #code='385') and #group='CO']"/>
<COL>
<DATA>
<xsl:if test="$condition">
<xsl:value-of select="#charge - sum($condition/#amount)"/>
</xsl:if>
</DATA>
</COL>
</xsl:template>
</xsl:transform>

I am guessing (!) you want something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/result">
<RESULT>
<xsl:for-each select="claim/charge">
<ROW>
<COL>
<DATA>
<xsl:value-of select="#charge - sum(adjustment[#group='CO' and (#code='45' or #code='385')]/#amount)" />
</DATA>
</COL>
</ROW>
</xsl:for-each>
</RESULT>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<RESULT>
<ROW>
<COL>
<DATA>40</DATA>
</COL>
</ROW>
<ROW>
<COL>
<DATA>70</DATA>
</COL>
</ROW>
<ROW>
<COL>
<DATA>27</DATA>
</COL>
</ROW>
<ROW>
<COL>
<DATA>11</DATA>
</COL>
</ROW>
</RESULT>
This assumes you want to create a record (with one field) for each charge.
Note that this is not valid FileMaker XML syntax.
-- edited in response to clarifications in comments: --
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/result">
<RESULT>
<xsl:for-each select="claim/charge[adjustment[not(#group='PR')]]">
<ROW>
<COL>
<DATA>
<xsl:value-of select="#charge - sum(adjustment[not(#group='PR')]/#amount)" />
</DATA>
</COL>
</ROW>
</xsl:for-each>
</RESULT>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<RESULT>
<ROW>
<COL>
<DATA>40</DATA>
</COL>
</ROW>
<ROW>
<COL>
<DATA>27</DATA>
</COL>
</ROW>
<ROW>
<COL>
<DATA>11</DATA>
</COL>
</ROW>
</RESULT>

Related

Removing comma from number format with unique XSLT coding

With the example below, the XSLT is doing a few things, it is grouping by column 1 and column2, if it is the same, then it will group the column 3 amount. As well, within the PayAmount tags, it is reversing negative (-) number into a positive and vice versa. What I am having troubles with is writing logic to remove the comma (,) from column 3 in the output.
Below is my XSLT Code
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="data">
<xsl:for-each select="row[generate-id(.) = generate-id(key(''rows'', concat(column1, ''||'', column2)))]">
</Record>
<Detail>
<Amount>
<xsl:variable name="mySum">
<xsl:value-of select="sum(key(''rows'', concat(column1, ''||'', column2))/column3)" />
</xsl:variable>
<xsl:value-of select="translate(($mySum * ($mySum >= 0) - $mySum * not($mySum >= 0)),'','','''')" />
</Amount>
</Detail>
</Record>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have tried the following logic, but the comma remains in the output:
- <xsl:value-of select="translate(($mySum * ($mySum >= 0) - $mySum * not($mySum >= 0)),'','', '''')" />
- <xsl:value-of select="format-number($mySum * ($mySum >= 0) - $mySum * not($mySum >= 0),'#,##0.00')" />
- <xsl:value-of select="translate(sum(key(''rows'', concat(column1, ''||'', column2))/column3),'','','''')" />
Below is a sample data in XML:
<data>
<row>
<column1>200040</column1>
<column2>Auto</column2>
<column3>-500.00</column3>
</row>
<row>
<column1>200040</column1>
<column2>Auto</column2>
<column3>-5,000.00</column3>
</row>
<row>
<column1>200040</column1>
<column2>Auto</column2>
<column3>-1,000.00</column3>
</row>
<row>
<column1>200040</column1>
<column2>Auto</column2>
<column3>300.00</column3>
</row>
<row>
<column1>200040</column1>
<column2>Auto</column2>
<column3>-4,000.00</column3>
</row>
<row>
<column1>200041</column1>
<column2>Bike</column2>
<column3>-1,700.00</column3>
</row>
<row>
<column1>200041</column1>
<column2>Bike</column2>
<column3>-1,000.00</column3>
</row>
<row>
<column1>200041</column1>
<column2>Bike</column2>
<column3>800.00</column3>
</row>
<row>
<column1>200045</column1>
<column2>Bus</column2>
<column3>200.00</column3>
</row>
<row>
<column1>200045</column1>
<column2>Bus</column2>
<column3>-10,000.00</column3>
</row>
<row>
<column1>200045</column1>
<column2>Bus</column2>
<column3>5,000.00</column3>
</row>
</data>
The output I would like to achieve after running the XSLT is
200040 | Auto | 10200.00
200041 | Bike | 1900.00
200045 | Bus | 4800.00
Any help would be greatly appreciated!
A value that contains a comma is not a number and cannot be summed. You need to remove the commas before you attempt to sum the values.
Here's a simplified example:
XSLT 1.0 + EXSLT node-set()
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:key name="a" match="amount" use="#key"/>
<xsl:template match="data">
<!-- first pass: convert amounts to numbers -->
<xsl:variable name="amounts">
<xsl:for-each select="row">
<amount key="{concat(column1, '|', column2)}">
<xsl:value-of select="translate(column3, ',', '')"/>
</amount>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<Output>
<xsl:for-each select="exsl:node-set($amounts)/amount[generate-id() = generate-id(key('a', #key))]">
<Record>
<Detail1>
<xsl:value-of select="substring-before(#key, '|')"/>
</Detail1>
<Detail2>
<xsl:value-of select="substring-after(#key, '|')"/>
</Detail2>
<xsl:variable name="sum" select="sum(key('a', #key))"/>
<Amount>
<xsl:value-of select="format-number($sum, '0.00')"/>
</Amount>
</Record>
</xsl:for-each>
</Output>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, this will produce:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Output>
<Record>
<Detail1>200040</Detail1>
<Detail2>Auto</Detail2>
<Amount>-10200.00</Amount>
</Record>
<Record>
<Detail1>200041</Detail1>
<Detail2>Bike</Detail2>
<Amount>-1900.00</Amount>
</Record>
<Record>
<Detail1>200045</Detail1>
<Detail2>Bus</Detail2>
<Amount>-4800.00</Amount>
</Record>
</Output>
To reverse negative amounts to positive (and vice versa), you could simply change:
<xsl:value-of select="format-number($sum, '0.00')"/>
to:
<xsl:value-of select="format-number(-$sum, '0.00')"/>

Grouping and moving remaining nodes using XSLT 1.0

I have the following xml,
<?xml version="1.0" encoding="UTF-8"?>
<response>
<case>
<CMEDIA>Phone</CMEDIA>
</case>
<results>
<row>
<IKEY>TestKey1</IKEY>
<OBJECTID>TestObject1</OBJECTID>
</row>
<row>
<IKEY>TestKey1</IKEY>
<OBJECTID>TestObject2</OBJECTID>
</row>
<row>
<IKEY>TestKey1</IKEY>
<OBJECTID>TestObject3</OBJECTID>
</row>
<row>
<IKEY>TestKey4</IKEY>
<OBJECTID>TestObject4</OBJECTID>
</row>
</results>
</response>
My requirement is to group all the matching <IKEY> rows and move them under one <row> and moving all <OBJECTID> nodes under that new <row>.
<?xml version="1.0" encoding="UTF-8"?>
<response>
<case>
<CMEDIA>Phone</CMEDIA>
</case>
<results>
<row>
<IKEY>TestKey1</IKEY>
<OBJECTID>TestObject1</OBJECTID>
<OBJECTID>TestObject2</OBJECTID>
<OBJECTID>TestObject3</OBJECTID>
</row>
<row>
<IKEY>TestKey4</IKEY>
<OBJECTID>TestObject4</OBJECTID>
</row>
</results>
</response>
I am trying with the following xsl for grouping based on <IKEY>, but I am not able to move all <OBJECTID> nodes to new <row>(Here I have to use only XSLT 1.0).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="ikey" match="row" use="string(IKEY)" />
<xsl:template match="results">
<xsl:copy>
<xsl:apply-templates select="row[generate-id() = generate-id(key('ikey', string(IKEY))[1])]" mode="ikey" />
</xsl:copy>
</xsl:template>
<xsl:template match="row" mode="ikey">
<xsl:choose>
<xsl:when test="IKEY">
<row>
<xsl:apply-templates select="IKEY|OBJECTID" />
</row>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Can somebody tell me what I am missing here?
Change
<xsl:apply-templates select="IKEY|OBJECTID" />
to
<xsl:apply-templates select="IKEY|key('ikey', IKEY)/OBJECTID" />

H2 refine XSLT to transform Shopify XML for FileMaker import

I'm trying to transform the following Shopify XML ...
<orders>
<order>
<order-no>1001</order-no>
<line-items>
<line-item>
<product-id>knife</product-id>
</line-item>
<line-item>
<product-id>fork</product-id>
</line-item>
</line-items>
<shipping-address>
<post-code>rh10 3hs</post-code>
</shipping-address>
</order>
</orders>
to the FileMaker FMPXMLRESULT grammer..
<ROW>
<COL>
<DATA>1001</DATA>
</COL>
<COL>
<DATA>Knife</DATA>
</COL>
<COL>
<DATA>rh10 3hs</DATA>
</COL>
</ROW>
<ROW>
<COL>
<DATA>1001</DATA>
</COL>
<COL>
<DATA>Fork</DATA>
</COL>
<COL>
<DATA>rh10 3hs</DATA>
</COL>
</ROW>
i.e.
1001 Knife rh10 3hs
1001 Fork rh10 3hs
using a FileMaker xslt example...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/*">
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="" NAME="" VERSION=""/>
<DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="" RECORDS="{count(/*/*)}" TIMEFORMAT="h:mm:ss a"/>
<METADATA>
<xsl:for-each select="/*/*[position()=1]/*">
<FIELD>
<xsl:attribute name="EMPTYOK">YES</xsl:attribute>
<xsl:attribute name="MAXREPEAT">1</xsl:attribute>
<xsl:attribute name="NAME"><xsl:value-of select="name()"/></xsl:attribute>
<xsl:attribute name="TYPE">TEXT</xsl:attribute>
</FIELD>
</xsl:for-each>
</METADATA>
<RESULTSET>
<xsl:attribute name="FOUND"><xsl:value-of select="count(child::*)"/></xsl:attribute>
<xsl:for-each select="child::*">
<ROW>
<xsl:attribute name="MODID">0</xsl:attribute>
<xsl:attribute name="RECORDID">0</xsl:attribute>
<xsl:for-each select="child::*">
<COL>
<DATA>
<xsl:value-of select="."/>
</DATA>
</COL>
</xsl:for-each>
</ROW>
</xsl:for-each>
</RESULTSET>
</FMPXMLRESULT>
</xsl:template>
</xsl:stylesheet>
BUT this extracts the top level order details for each order ignoring the children and grandchildren.
I'm a complete beginner and have trawled the net and forums trying many permutations of the line '' but to be honest it's guess work and initial elation is begin replaced by desperation ;-(
Any tips greatly appreciated
Many thanks,
Jim
In Filemaker, you can only import into one table at a time. You need a separate stylesheet for each import. This one for importing into the LineItems table:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="" NAME="" VERSION=""/>
<DATABASE DATEFORMAT="" LAYOUT="" NAME="" RECORDS="" TIMEFORMAT=""/>
<METADATA>
<FIELD NAME="OrderID" TYPE="TEXT" EMPTYOK="YES" MAXREPEAT=""/>
<FIELD NAME="ProductID" TYPE="TEXT" EMPTYOK="YES" MAXREPEAT=""/>
</METADATA>
<RESULTSET FOUND="">
<xsl:for-each select="orders/order/line-items/line-item">
<ROW MODID="" RECORDID="">
<COL><DATA><xsl:value-of select="../../order-no"/></DATA></COL>
<COL><DATA><xsl:value-of select="product-id"/></DATA></COL>
</ROW>
</xsl:for-each>
</RESULTSET>
</FMPXMLRESULT>
</xsl:template>
</xsl:stylesheet>
and this one for importing into the Orders table:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="" NAME="" VERSION=""/>
<DATABASE DATEFORMAT="" LAYOUT="" NAME="" RECORDS="" TIMEFORMAT=""/>
<METADATA>
<FIELD NAME="OrderID" TYPE="TEXT" EMPTYOK="YES" MAXREPEAT=""/>
<FIELD NAME="PostCode" TYPE="TEXT" EMPTYOK="YES" MAXREPEAT=""/>
</METADATA>
<RESULTSET FOUND="">
<xsl:for-each select="orders/order">
<ROW MODID="" RECORDID="">
<COL><DATA><xsl:value-of select="order-no"/></DATA></COL>
<COL><DATA><xsl:value-of select="shipping-address/post-code"/></DATA></COL>
</ROW>
</xsl:for-each>
</RESULTSET>
</FMPXMLRESULT>
</xsl:template>
</xsl:stylesheet>

Sorting and moving elements into a new element

Even with all the good tips on this site, I still have some trouble with my xslt. I'm pretty new to it. I have this source file:
<?xml version="1.0" encoding="utf-8"?>
<file>
<id>1</id>
<row type="A">
<name>ABC</name>
</row>
<row type="B">
<name>BCA</name>
</row>
<row type="A">
<name>CBA</name>
</row>
</file>
and I want to add an element and sort the rows on type, to get this result
<file>
<id>1</id>
<details>
<row type="A">
<name>ABC</name>
</row>
<row type="A">
<name>CBA</name>
</row>
<row type="B">
<name>BCA</name>
</row>
</details>
</file>
I'm able to sort the rows using this:
<xsl:template match="file">
<xsl:copy>
<xsl:apply-templates select="#*/row"/>
<xsl:apply-templates>
<xsl:sort select="#type" data-type="text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
and I'm able to move the rows using this
<xsl:template match="file">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates select="*[not(name(.)='row')]" />
<details>
<xsl:apply-templates select="row" />
</details>
</xsl:copy>
</xsl:template>
but I'm not able to produce the correct answer when I try to combine them. Hopefully I understand more of XSLT when I see how things are combined. Since I'm creating a new element <details>, I think the sorting has to be done before the creation of the new <details> element. I have to use xslt 1.0.
Something like this seems to work:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="file">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:copy-of select="row[1]/preceding-sibling::*" />
<details>
<xsl:for-each select="row">
<xsl:sort select="#type" data-type="text"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</details>
<xsl:copy-of select="row[last()]/following-sibling::*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Here is the result I got:
<?xml version="1.0" encoding="utf-8"?>
<file>
<id>1</id>
<details>
<row type="A">
<name>ABC</name>
</row>
<row type="A">
<name>CBA</name>
</row>
<row type="B">
<name>BCA</name>
</row>
</details>
</file>

Create Excel (SpeadsheetML) output with XSLT

I have this XML file and I want to create an XSL file to convert it to Excel. Each row should represent a logo. The columns will be the key attributes like color, id, description plus any other key for other logos.
<Top>
<logo>
<field key="id">172-32-1176</field>
<field key="color">Blue</field>
<field key="description"><p>Short Description</p></field>
<field key="startdate">408 496-7223</field>
</logo>
<logo>
<field key="id">111-111-111</field>
<field key="color">Red</field>
</logo>
<!-- ... -->
</Top>
The XSL file is something like this:
<xsl:stylesheet
version="1.0"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
>
<xsl:template match="/">
<Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40"
>
<xsl:apply-templates/>
</Workbook>
</xsl:template>
<xsl:template match="/*">
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of> select="local-name(/*)"/>
</xsl:attribute>
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<xsl:for-each select="*/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="#key"/>
</Data>
</Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates/>
</Table>
</Worksheet>
</xsl:template>
<xsl:template match="/*/*">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>
<xsl:template match="/*/*/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
<!-- <xsl:apply-templates/> -->
</xsl:template>
</xsl:stylesheet>
But data are not correctly placed under the columns and column names are repeating. How can this be done?
The columns could be in any order and also column stardate should be empty for second row in excel. Similarly for more .
You were very close. Try to be more specific when it comes to template matching - don't say template match"/*/*/*" when you can say template match="field".
Other than that, this is your approach, only slightly modified:
<xsl:stylesheet
version="1.0"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
>
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40"
>
<xsl:apply-templates select="Top" />
</Workbook>
</xsl:template>
<xsl:template match="Top">
<Worksheet ss:Name="{local-name()}">
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<!-- header row, made from the first logo -->
<xsl:apply-templates select="logo[1]/field/#key" />
</Row>
<xsl:apply-templates select="logo" />
</Table>
</Worksheet>
</xsl:template>
<!-- a <logo> will turn into a <Row> -->
<xsl:template match="logo">
<Row>
<xsl:apply-templates select="field" />
</Row>
</xsl:template>
<!-- convenience: <field> and #key both turn into a <Cell> -->
<xsl:template match="field | field/#key">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:template>
</xsl:stylesheet>
Your "repeating column names" problem roots in this expression:
<xsl:for-each select="*/*">
In your context, this selects any third level element in the document (literally all <field> nodes in all <logo>s), and makes a header row out of them. I replaced it with
<xsl:apply-templates select="logo[1]/field/#key" />
which makes a header row out of the first <logo> only.
If a certain column order is required (other than document order) or not all <field> nodes are in the same order for all <logo>s, things get more complex. Tell me if you need that.