Gustavo Jose
Gustavo Jose

Reputation: 55

XSLT variables not working

I have this bit of XML

<MODULO>
    <NAME>ObtemSumarioSNC_P</NAME>
    <VALUES>
      <ROW>
        <DataEscritura>19770324</DataEscritura>
        <DataUltimoBalanco>20161231</DataUltimoBalanco>
        <TotalVendas>**********</TotalVendas>
      </ROW>
    </VALUES>
  </MODULO>

What i need to do is use the <DataEscritura> and <DataUltimoBalanco> to form a date in the day-month-year format.

for this i've done this XSLT

<xsl:template match="MODULO[NAME='ObtemSumarioSNC_P']" name="Tmp_Sumario">
    <table class="TabelaSumario">
      <th colspan="2">SUMÁRIO</th>
      <xsl:for-each select="VALUES/ROW">
      <xsl:variable name="yyyy" select ="substring(DataEscritura,1,4)"/>
      <xsl:variable name="mm" select="substring(DataEscritura,5,2)"/>
      <xsl:variable name="dd" select="substring(DataEscritura,7,4)"/>     
      <xsl:variable name="yyyy2" select ="substring(DataUltimoBalanco,1,4)"/>
      <xsl:variable name="mm2" select="substring(DataUltimoBalanco,5,2)"/>
      <xsl:variable name="dd2" select="substring(DataUltimoBalanco,7,4)"/>
        <tr>
          <td>
            Fundação
          </td>
          <td>
            <xsl:value-of select="concat($dd,'-',$mm,'-',$yyyy)"/>
          </td>
        </tr>
        <tr>
          <td>
            Vendas em <xsl:value-of select="concat($dd2,'-',$mm2,'-',$yyyy2)"/>
          </td>
          <td>
            <xsl:value-of select="TotalVendas"/>
          </td>
        </tr>
        <tr>
          <td>
            Capital Próprio em <xsl:value-of select="concat($dd2,'-',$mm2,'-',$yyyy2)"/>
          </td>
          <td>
            <xsl:value-of select="CapitalProprio"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
</xsl:template>

Note , i have already converted a big file of XML (the same one has this , but this is just a small bit, original has around 5000 lines) to Excel format whit multiple spreadsheets and this piece of code , i mean the declaration and the using of the variables , worked 100% fine, now im trying to do this in HTML and the value shown by and the other selects are only the -- characters from the concat.

Hope someone can help!

Upvotes: 0

Views: 252

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 31011

It seems that reading of date parts is OK (actually, almost OK, for details see below).

I added doctype-public and doctype-system attributes to xsl:output command, with example values for XHTML. You may change them according to your needs.

I added also a number of HTML tags, i.e.:

  • <html> and <body> surrounding the whole content.
  • <th> surrounding the title row (SUMÁRIO).

When you read both day parts from the source strings, the length (3rd) argument) should be 2 (not 4).

So the corrected XSLT script is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"
    doctype-public="-//W3C//DTD XHTML 1.1//EN"
    doctype-system= "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

  <xsl:template match="MODULO[NAME='ObtemSumarioSNC_P']" name="Tmp_Sumario">
    <html><body>
    <table class="TabelaSumario" border="1" cellspacing="2">
      <tr><th colspan="2">SUMÁRIO</th></tr>
      <xsl:for-each select="VALUES/ROW">
        <xsl:variable name="yyyy" select ="substring(DataEscritura,1,4)"/>
        <xsl:variable name="mm" select="substring(DataEscritura,5,2)"/>
        <xsl:variable name="dd" select="substring(DataEscritura,7,2)"/>     
        <xsl:variable name="yyyy2" select ="substring(DataUltimoBalanco,1,4)"/>
        <xsl:variable name="mm2" select="substring(DataUltimoBalanco,5,2)"/>
        <xsl:variable name="dd2" select="substring(DataUltimoBalanco,7,2)"/>
        <tr>
          <td>Fundação</td>
          <td><xsl:value-of select="concat($dd,'-',$mm,'-',$yyyy)"/></td>
        </tr>
        <tr>
          <td>Vendas em <xsl:value-of select="concat($dd2,'-',$mm2,'-',$yyyy2)"/></td>
          <td><xsl:value-of select="TotalVendas"/></td>
        </tr>
        <tr>
          <td>Capital Próprio em <xsl:value-of select="concat($dd2,'-',$mm2,'-',$yyyy2)"/></td>
          <td><xsl:value-of select="CapitalProprio"/></td>
        </tr>
      </xsl:for-each>
    </table>
    </body></html>
  </xsl:template>
</xsl:stylesheet>

Using the above script and your XML, I got:

 <!DOCTYPE html
   PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 <html>
    <body>
       <table class="TabelaSumario" border="1" cellspacing="2">
          <tr>
             <th colspan="2">SUMÁRIO</th>
          </tr>
          <tr>
             <td>Fundação</td>
             <td>24-03-1977</td>
          </tr>
          <tr>
             <td>Vendas em 31-12-2016</td>
             <td>**********</td>
          </tr>
          <tr>
             <td>Capital Próprio em 31-12-2016</td>
             <td></td>
          </tr>
       </table>
    </body>
 </html>

with proper HTML view. So at least as both dates are concerned, everything is OK.

Note also that your template contains value-of reading data from CapitalProprio, but your XML source does not include it, so the last cell of the output is empty.

Upvotes: 1

Related Questions