Whitespace inserted when formatting cell parts via XSLT - excel

I need to format portions of an Excel cell based on a variable. Each time I try I get rouge whitespace that I cannot understand. As a test I made a very simple xsl that was devoid of any real code to try to understand what Excel was doing. I'm still at a loss as to how to avoid the end result. I've removed whitespace within the XSL, since I know this can be an issue with WordML, I've tried to enclose the text via CDATA, I've tried using xsl:text and disabling escaping, and I've tried using normalize-space. In the end the result is always the same (except normalize-space which is logically one space shorter; but the base result applies).
My testing XSL, which can be run with any XML given it isn't actually doing a transform.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Workbook>
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Developer</Author>
<Created>2018-02-01T15:43:59Z</Created>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>12345</WindowHeight>
<WindowWidth>25200</WindowWidth>
<WindowTopX>0</WindowTopX>
<WindowTopY>0</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell>
<Data ss:Type="String">1 TEST</Data>
</Cell>
</Row>
<Row>
<Cell>
<ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40">
<Font html:Color="#000000"><![CDATA[2 ]]></Font>
<B>
<Font html:Color="#000000"><![CDATA[TEST]]></Font>
</B>
</ss:Data>
</Cell>
</Row>
<Row ss:Height="18.75"><Cell><ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40"><Font html:Color="#000000">3 </Font><Font html:Size="14" html:Color="#000000">TEST</Font></ss:Data></Cell></Row>
<Row ss:Height="18.75">
<Cell>
<ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40">
<Font html:Color="#000000"><xsl:text disable-output-escaping="yes">4 </xsl:text></Font>
<B>
<Font html:Size="14" html:Color="#000000"><xsl:text disable-output-escaping="yes">TEST</xsl:text></Font>
</B>
</ss:Data>
</Cell>
</Row>
<Row ss:Height="18.75">
<Cell>
<ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40">
<Font html:Color="#000000"><xsl:value-of select="normalize-space('5 ')"/></Font>
<Font html:Size="14" html:Color="#000000"><xsl:value-of select="normalize-space('TEST')"/></Font>
</ss:Data>
</Cell>
</Row>
<Row ss:Height="18.75">
<Cell>
<ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40">
<Font html:Color="#000000">6 </Font>
<B>
<Font html:Size="14" html:Color="#000000">TEST</Font>
</B>
</ss:Data>
</Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveCol>1</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
</xsl:template>
This results in
I would expect no whitespacing before any of the elements in rows 2 through 6. what really seems strange is that when looked at via Visual Studio Code there is no white space in the file. This spacing appears to be coming via something within the Font and B, but the structure is identical to what you would get when viewing a file created directly in Excel without all the whitespace.

Related

Update XSLT for XLSX

I have an old XSLT stylesheet that still works, but only transforms to Excel xls files. I've used ClosedXml to convert grid data to Excel xlsx files and it works great, but I have some formatted Excel documents that are not grids and require some formatting and layout. I would like to convert those old XSLT stylesheets to create the new(er) XLSX format files but only replacing
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
with
xmlns="http://schemas.openxmlformats.org/officeDocument/2006/
doesn't work.
Has anyone updated an XSLT stylesheet to produce output to Excel xlsx? If yes, please share what you needed to do to accomplish it.
Here's the XSLT stylesheet that I'm trying to convert:
<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">
<Styles>
.. styles
</Styles>
<Worksheet ss:Name="name">
<Table ss:ExpandedColumnCount="9" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="13.2">
<Column ss:Index="2" ss:Width="77"/>
<Column ss:Index="5" ss:Width="54"/>
<Column ss:Index="8" ss:Width="178"/>
<xsl:for-each select="ReportRows/ReportRow">
<xsl:if test="position()=1">
<Row>
<Cell ss:MergeAcross="8" ss:StyleID="s30">
<Data ss:Type="String">
<xsl:value-of select="SE_Year" /> Title #Lab
</Data>
</Cell>
</Row>
</xsl:if>
<xsl:if test="Pr_Short_Name != Prev_Project">
<Row>
<Cell ss:MergeAcross="8" ss:StyleID="s24" />
</Row>
<Row>
<Cell ss:MergeAcross="8" ss:StyleID="s27">
<Data ss:Type="String">
<xsl:value-of select="Pr_Long_Name" />
</Data>
</Cell>
</Row>
<Row ss:AutoFitHeight="0">
cells...
</Row>
</xsl:if>
<xsl:if test="GR_Short_Name != Prev_SampleTypeGroup">
<Row>
<Cell ss:MergeAcross="8" ss:StyleID="s31" />
</Row>
<Row>
<Cell ss:MergeAcross="8" ss:StyleID="s32">
<Data ss:Type="String">
<xsl:value-of select="GR_Short_Name" />
</Data>
</Cell>
</Row>
</xsl:if>
<Row ss:Height="41.4">
cells...
</Row>
</xsl:for-each>
<Row ss:Height="13.8">
<Cell ss:MergeAcross="8" ss:MergeDown="1" ss:StyleID="m15419890" />
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0" />
<Footer x:Margin="0.3" x:Data="&L&8Form Print Date/Time: &D &T&C&8Page &P of &N" />
<PageMargins x:Bottom="0.95" x:Left="0.5" x:Right="0.5" x:Top="0.5" />
</PageSetup>
<Print>
<ValidPrinterInfo />
<Scale>100</Scale>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected />
<DoNotDisplayGridlines />
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>8</ActiveRow>
<ActiveCol>10</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
</xsl:template>
</xsl:stylesheet>

how to access variable structure under for-each to create excel sheet in XSLT

I have input like:
<item id="CON_1985_14">
<prv id="p1">
<no>1</no>
<text>This "May" take some moment, you "shall" not go there.</text>
<text>This is ok.</text>
</prv>
</item>
Need to create a excel table on the basis of quoted values.
=========================
A B
=========================
May CON_1985_14
shall CON_1985_14
=========================
Created XSLT to generate excel output like:
<Worksheet ss:Name="R6">
<Table>
<Row>
<Cell>
<Data ss:Type="String">
<xsl:text>A</xsl:text>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<xsl:text>B</xsl:text>
</Data>
</Cell>
</Row>
<xsl:for-each select="$path">
<xsl:for-each select="descendant::prv//descendant::text">
<xsl:variable name="STRUCTUREONBASISOFTOKENS" as="node()">
<Report>
<dd>
<xsl:for-each select="tokenize(normalize-space(.), '("|“)')">
<xsl:if test="(position() gt 1) and (position() mod 2 = 0)">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position() ne (last() - 1)">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
</dd>
<LID>
<xsl:value-of select="/item/#id"/>
</LID>
</Report>
</xsl:variable>
<xsl:for-each select="tokenize($STRUCTUREONBASISOFTOKENS/Report/dd, ',')">
<Row>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="normalize-space(.)"/>
</Data>
</Cell>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="$STRUCTUREONBASISOFTOKENS/Report/LID"/>
</Data>
</Cell>
</Row>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</Table>
</Worksheet>
The variable created inside for-each having output printed in message like:
<Report xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:LxNx="http://www.LXNX.com"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:dict="http://www.lexis-nexis.com/glp/dict" xmlns:ci="http://www.lexis-nexis.com/ci"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<dd>May, shall</dd>
<LID>CON_1985_14</LID>
</Report>
Now the nested for-each, just below the variable define is trying to fetch the values from the variable structure to generate the ROWs, but not succeed..
Not able to fetch the values from the variable by xpath to generate row.
This syntax '' means the collection() path.
Provide the suitable solution for that.
I could not follow the logic of your attempted XSLT. Consider the following simplified example:
XML
<input>
<item>Lorem ipsum "alpha" dolor sit amet, consectetur "bravo" adipiscing elit "charlie".</item>
<item>"Delta" sed do eiusmod tempor "echo" incididunt ut labore et dolore magna aliqua.</item>
</input>
XSLT 2.0
<xsl:stylesheet version="2.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="/input">
<table>
<xsl:for-each select="item">
<xsl:analyze-string select="." regex=""(.*?)"">
<xsl:matching-substring>
<row>
<cell>
<xsl:value-of select="regex-group(1)" />
</cell>
</row>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<cell>alpha</cell>
</row>
<row>
<cell>bravo</cell>
</row>
<row>
<cell>charlie</cell>
</row>
<row>
<cell>Delta</cell>
</row>
<row>
<cell>echo</cell>
</row>
</table>
Demo: https://xsltfiddle.liberty-development.net/6rewNy4

XSLT Processing XML Converted from Excel

Here is the XML file converted from Excel Mac (Excel 2004 XML format):
The Excel file has four fields (Field 1 to 4) and each field has for data (Fieldn_Datam, n,m ranges from 1 to 4)
The XML output is like this:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<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">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Passiflora Cui</Author>
<LastAuthor>Passiflora Cui</LastAuthor>
<Created>2019-06-30T21:49:41Z</Created>
<LastSaved>2019-06-30T21:50:54Z</LastSaved>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>16940</WindowHeight>
<WindowWidth>27640</WindowWidth>
<WindowTopX>5580</WindowTopX>
<WindowTopY>3560</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="5" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="16">
<Column ss:AutoFitWidth="0" ss:Width="74"/>
<Row>
<Cell>
<Data ss:Type="String">Field1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data1</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data1</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data2</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data2</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data3</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data3</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data4</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data4</Data>
</Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>5</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
And my XSLT file is like this:
<?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 = "Table">
<Result>
<xsl:for-each select="Row[position() > 1]">
<Test>
<Field1><xsl:value-of select = "Cell[1]/Data"/></Field1>
<Field2><xsl:value-of select = "Cell[2]/Data"/></Field2>
<Field3><xsl:value-of select = "Cell[3]/Data"/></Field3>
<Field4><xsl:value-of select = "Cell[4]/Data"/></Field4>
</Test>
</xsl:for-each>
</Result>
</xsl:template>
</xsl:stylesheet>
And the desired result file looks like this:
<Result>
<Test>
<Field1>Field1_Data1</Field1>
<Field2>Field2_Data1</Field2>
<Field3>Field3_Data1</Field3>
<Field4>Field4_Data1</Field4>
</Test>
<Test>
<Field1>Field1_Data2</Field1>
<Field2>Field2_Data2</Field2>
<Field3>Field3_Data2</Field3>
<Field4>Field4_Data2</Field4>
</Test>
<Test>
<Field1>Field1_Data3</Field1>
<Field2>Field2_Data3</Field2>
<Field3>Field3_Data3</Field3>
<Field4>Field4_Data3</Field4>
</Test>
<Test>
<Field1>Field1_Data4</Field1>
<Field2>Field2_Data4</Field2>
<Field3>Field3_Data4</Field3>
<Field4>Field4_Data4</Field4>
</Test>
</Result>
However my XSLT does not work and it outputs all the strings as if the <xsl:template> has not being executed. I've tried a lot of XPaths such as //Table, Workbook/Worksheet/Table but none of them worked! Could anyone tell me where I'm wrong? Thanks in advance!
Your solution failed because you didn't take the namespaces into account. Your XML root element <Workbook> set the default namespace xmlns="urn:schemas-microsoft-com:office:spreadsheet", hence itself and all of its children do have the same namespace. So you have to set this namespace in your XSLT as well.
Here, I used the prefix wb to designate the namespace
xmlns:wb="urn:schemas-microsoft-com:office:spreadsheet"
So the XSLT-1.0 file you desire could look like
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wb="urn:schemas-microsoft-com:office:spreadsheet">
<xsl:output method = "xml" indent = "yes"/>
<!-- Ignore all free text() nodes -->
<xsl:template match="text()" />
<xsl:template match = "/wb:Workbook/wb:Worksheet/wb:Table">
<xsl:element name="Result">
<xsl:for-each select="wb:Row[position() > 1]">
<Test>
<Field1><xsl:value-of select = "wb:Cell[1]/wb:Data"/></Field1>
<Field2><xsl:value-of select = "wb:Cell[2]/wb:Data"/></Field2>
<Field3><xsl:value-of select = "wb:Cell[3]/wb:Data"/></Field3>
<Field4><xsl:value-of select = "wb:Cell[4]/wb:Data"/></Field4>
</Test>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Its result is as desired.
The prefix wb: of the newly defined namespace has been added to all XPath element references. That's all you had to do to make your code work.

Formatting cells in XSLX files generated by XSLT

I'm using XSLT to generate a report with a date column. In the spreadsheet XLSX file, if I pass through the date as a string, when opening it with a spreadsheet package such as Excel, it will assume it's a date (which is correct) but then format it as MM/dd/yyyy when what we actually want is dd/MM/yyyy. This appears to be the spreadsheet package that is causing this, however I was wondering if you could define cell formatting from the XSLT?
Here is the code I'm using to do this:
<xsl:template match="SomeTemplate">
<Row>
...
<Cell>
<xsl:value-of select="ms:format-date(StartTime, 'dd/MM/yyyy')" />
<xsl:text> </xsl:text>
<xsl:value-of select="ms:format-time(StartTime, 'HH:mm:ss')" />
</Cell>
...
</Row>
</xsl:template>
I've attempted other ways around this such as starting the cell with ' but strangely this didn't work!
Here's an XML sample relevant to the XSLT above:
...
<Destination>
<StartTime>2013-03-07T00:01:09</StartTime>
<EndTime>2013-03-07T10:41:09</EndTime>
...
</Destination>
...
The cell formatting in Excel xml can be done via styles.
I.e you fill the cells with dates in xs:datetime format (you do not need any ms:format-date conversion at all in your example) but specify the cell style though ss:styleId:
<Destination>
<StartTime>2013-03-07T00:01:09</StartTime>
<EndTime>2013-03-07T10:41:09</EndTime>
</Destination>
+
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
version="1.0">
<xsl:template match="/">
<Workbook>
<Styles>
<Style ss:ID="style_default">
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="style_ddslashmmslashyy">
<NumberFormat ss:Format="dd/mm/yy;#"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table>
<xsl:apply-templates/>
</Table>
</Worksheet>
</Workbook>
</xsl:template>
<xsl:template match="Destination">
<Row>
<Cell ss:StyleID="style_default">
<Data ss:Type="DateTime"><xsl:value-of select="StartTime"/></Data>
</Cell>
<Cell ss:StyleID="style_ddslashmmslashyy">
<Data ss:Type="DateTime"><xsl:value-of select="StartTime"/></Data>
</Cell>
</Row>
</xsl:template>
</xsl:stylesheet>

XSL Taking XML transforming it into Excel

New guy here so bear with me. Ive got a basic XSL file that will read my xml data. Im trying to put xml into Excel. Heres my issue. With a small XML file it seems to convert it easly, BUT with this XML file that had several nodes ( I think they are called), when I call up the data, its not right. I want to only show info from the check portion of XML and then show it in Excel in a way that shows the 6 or 7 columns that I want, then show the data. Heres what I have so far:
XML:
<bdiData>
<documentControlInfo>
<documentInfo>
<docDescription>Checks for Company X</docDescription>
<docID>
<ID>123456789</ID>
</docID>
<docModifier>My Company</docModifier>
<docCreateDate>2010-08-23</docCreateDate>
<docCreateTime>07:08:54-0700</docCreateTime>
<standardVersion>1.0</standardVersion>
<testIndicator>0</testIndicator>
<resendIndicator>0</resendIndicator>
</documentInfo>
<sourceInfo>
<sourceName>My Banking Name</sourceName>
<sourceID>
<idOther>ShortBankName</idOther>
</sourceID>
</sourceInfo>
<destinationInfo>
<destinationName>My Company</destinationName>
<destinationID>
<idOther>MYCO</idOther>
</destinationID>
</destinationInfo>
</documentControlInfo>
<checkItemCollection>
<collectionInfo>
<description>Items</description>
<ID>654811650</ID>
<Classification>
<classification>Items</classification>
</Classification>
</collectionInfo>
<checkItemBatch>
<checkItemBatchInfo>
<description>Paid Checks</description>
<ID>1239668334710</ID>
<Classification>
<classification>Paid Checks</classification>
</Classification>
</checkItemBatchInfo>
<checkItem>
<checkItemType>check</checkItemType>
<checkAmount>2960</checkAmount>
<postingInfo>
<date>2009-06-12</date>
<RT>87654321</RT>
<accountNumber>123465798</accountNumber>
<seqNum>007725552898</seqNum>
<trancode>001152</trancode>
<amount>2960</amount>
<serialNumber>55225410</serialNumber>
</postingInfo>
XSL File:
<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="*[position() = 2]/*/checkItem/postingInfo/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates/>
</Table>
</Worksheet>
</xsl:template>
<xsl:template match="/*/checkItem/postingInfo/*">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>
<xsl:template match="/*/checkItem/postingInfo/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:template>
</xsl:stylesheet>
Does anyone have any Idea how I can get to JUSt the check portion f the XML file and have it format in an eay way??
Thanks
GabrielVA
I think you need this stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:x="urn:schemas-microsoft-com:office:excel">
<xsl:template match="/">
<xsl:processing-instruction name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction>
<Workbook>
<xsl:apply-templates/>
</Workbook>
</xsl:template>
<xsl:template match="/*">
<Worksheet ss:Name="{*/*/*[local-name()='docDescription']}">
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<xsl:for-each select="*/*/*[local-name()='checkItem'][1]//*[not(*)]">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates select="*/*/*[local-name()='checkItem']"/>
</Table>
</Worksheet>
</xsl:template>
<xsl:template match="*[local-name()='checkItem']" priority="1">
<Row>
<xsl:apply-templates select=".//*[not(*)]"/>
</Row>
</xsl:template>
<xsl:template match="*[not(*)]">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:template>
</xsl:stylesheet>
With this input (proper well formed):
<bdiData>
<documentControlInfo>
<documentInfo>
<docDescription>Checks for Company X</docDescription>
<docID>
<ID>123456789</ID>
</docID>
<docModifier>My Company</docModifier>
<docCreateDate>2010-08-23</docCreateDate>
<docCreateTime>07:08:54-0700</docCreateTime>
<standardVersion>1.0</standardVersion>
<testIndicator>0</testIndicator>
<resendIndicator>0</resendIndicator>
</documentInfo>
<sourceInfo>
<sourceName>My Banking Name</sourceName>
<sourceID>
<idOther>ShortBankName</idOther>
</sourceID>
</sourceInfo>
<destinationInfo>
<destinationName>My Company</destinationName>
<destinationID>
<idOther>MYCO</idOther>
</destinationID>
</destinationInfo>
</documentControlInfo>
<checkItemCollection>
<collectionInfo>
<description>Items</description>
<ID>654811650</ID>
<Classification>
<classification>Items</classification>
</Classification>
</collectionInfo>
<checkItemBatch>
<checkItemBatchInfo>
<description>Paid Checks</description>
<ID>1239668334710</ID>
<Classification>
<classification>Paid Checks</classification>
</Classification>
</checkItemBatchInfo>
<checkItem>
<checkItemType>check</checkItemType>
<checkAmount>2960</checkAmount>
<postingInfo>
<date>2009-06-12</date>
<RT>87654321</RT>
<accountNumber>123465798</accountNumber>
<seqNum>007725552898</seqNum>
<trancode>001152</trancode>
<amount>2960</amount>
<serialNumber>55225410</serialNumber>
</postingInfo>
</checkItem>
</checkItemBatch>
</checkItemCollection>
</bdiData>
Output:
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel">
<Worksheet ss:Name="Checks for Company X">
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<Cell>
<Data ss:Type="String">checkItemType</Data>
</Cell>
<Cell>
<Data ss:Type="String">checkAmount</Data>
</Cell>
<Cell>
<Data ss:Type="String">date</Data>
</Cell>
<Cell>
<Data ss:Type="String">RT</Data>
</Cell>
<Cell>
<Data ss:Type="String">accountNumber</Data>
</Cell>
<Cell>
<Data ss:Type="String">seqNum</Data>
</Cell>
<Cell>
<Data ss:Type="String">trancode</Data>
</Cell>
<Cell>
<Data ss:Type="String">amount</Data>
</Cell>
<Cell>
<Data ss:Type="String">serialNumber</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">check</Data>
</Cell>
<Cell>
<Data ss:Type="String">2960</Data>
</Cell>
<Cell>
<Data ss:Type="String">2009-06-12</Data>
</Cell>
<Cell>
<Data ss:Type="String">87654321</Data>
</Cell>
<Cell>
<Data ss:Type="String">123465798</Data>
</Cell>
<Cell>
<Data ss:Type="String">007725552898</Data>
</Cell>
<Cell>
<Data ss:Type="String">001152</Data>
</Cell>
<Cell>
<Data ss:Type="String">2960</Data>
</Cell>
<Cell>
<Data ss:Type="String">55225410</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Wich is properly open by Excel.
Note: those fn:local-name() are there because your input sample is unreliable.

Resources