fsi
fsi

Reputation: 1367

XLS-XML not showing data XSLT

I'm using birt and the file is a xls but xml format. Can someone tell me why is not working? I tried a few changes, but none of them works; If I put this:

<xsl:template match="/Workbook">

It will work in a wrong way, because it only throws inside <body>. After that I checked the node path, and it's like this:

/Workbook/Worksheet[1]/Table[1]/Row[1]/Cell[1]

I have my xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<?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/>
        <Title/>
        <Description/>
        <Subject/>
    </DocumentProperties>
<Worksheet ss:Name="Report">
<ss:Table>
    <ss:Column ss:Width="192.0" ss:AutoFitWidth="0"/>
    <ss:Column ss:Width="192.0" ss:AutoFitWidth="0"/>
    <ss:Column ss:Width="192.0" ss:AutoFitWidth="0"/>
    <Row ss:AutoFitHeight="1">
        <Cell ss:Index="1" ss:StyleID="20">
            <Data ss:Type="String">A</Data>
        </Cell>
        <Cell ss:Index="2" ss:StyleID="20">
            <Data ss:Type="String">B</Data>
        </Cell>
        <Cell ss:Index="3" ss:StyleID="20">
            <Data ss:Type="String">C</Data>
        </Cell>
    </Row>
</ss:Table>
<WorksheetOptions>
</WorksheetOptions>
</Worksheet>
</Workbook>

And my xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
<xsl:template match="/">
<html lang="en-us">
<head>
</head>
<body class="">
    <table border="1">
            <xsl:for-each select="Workbook/Worksheet/ss:Table">
            <xsl:for-each select="Row">
                <tr>
                    <xsl:for-each select="Cell">                     
                        <td>
                            <xsl:value-of select="Data" />
                        </td>                   
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 457

Answers (1)

Tim C
Tim C

Reputation: 70648

This is because of namespaces! Ignore them at your peril...

The root element of your XML looks like this:

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

The xmlns is a default namespace declaration. It means the Workbook and all descendants belong to that namespace (unless overridden by another one).

Now, an element in a namespace is different from a similarly named element in a different namespace, or one with no space at all.

Therefore, when you do this...

<xsl:for-each select="Workbook/Worksheet/ss:Table">

The Workbook and Worksheet will not match the XML because it is look for elements with no namespace.

The solution, is to declare the namespace in the XSLT, and prefix the relative elements. In fact, the namespace is already declared in your XSLT. You can use the ss prefix throughout. Try this

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
<xsl:template match="/">
<html lang="en-us">
<head>
</head>
<body class="">
    <table border="1">
            <xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table">
            <xsl:for-each select="ss:Row">
                <tr>
                    <xsl:for-each select="ss:Cell">                     
                        <td>
                            <xsl:value-of select="ss:Data" />
                        </td>                   
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions