xsl-list
[Top] [All Lists]

[xsl] Modes (was RE: [xsl] Re: Keys, IDs lookup tables.)

2012-06-11 11:01:41
I notice that you're using "mode" in this solution.  Can someone point me to a 
good explanation of modes, with good examples?  I haven't worked much with them 
and am finding them a bit confusing.

Thanks

Michele

On Wed, Jun 6, 2012 at 6:04 PM, Wendell Piez 
<wapiez(_at_)mulberrytech(_dot_)com> wrote:
Daniel,


On 6/6/2012 3:31 PM, daniel whitney wrote:

OK so I came up with a solution for Part 2 of my question. Not sure 
if it's the ideal, but it works:

Would Part 1 have a similar solution? Would I store the "lookup"
portion in a global variable and just call that again and again?


Basically, yes, although you will get closer to your ideal of clarity, 
grace and maintainability if you refactor with templates. When doing 
so, you can pass parameters to retain context.

So something like:

<xsl:template match="financialgroup">

 <tr>
   <td>
     <xsl:value-of select="key('legend', @financiallegendcode)"/>
   </td>
   <xsl:apply-templates select="ancestor::financials/perioddate/period"
     mode="period-cell">
     <xsl:with-param name="fgroup" select="."/>
   </xsl:apply-templates>
 </tr>
</xsl:template>

... and to receive the call:

<xsl:template match="period" mode="period-cell">
 <xsl:param name="fgroup" as="element(financialgroup)"/>

 <xsl:variable name="idBase" select="@id"/>
 <td>
   <xsl:value-of
     select="($fgroup/financial[@periodref=$idBase],'....')[1]"/>
 </td>
</xsl:template>

Obviously the details aren't worked out here, and note also I'm using 
an XPath 2.0 idiom to provide the fallback value when the particular 
'financialgroup' element has no 'financial' value for the period.

This also fixes a bug in your code where every period covered is 
assigned all values for all of them (or the first of them, if run as XSLT 
1.0).

I hope this helps,
Wendell


<xsl:template match="financialgroup"> <xsl:variable 
name="currentFinancial" select="financial"/> <tr> <td><xsl:value-of 
select="key('legend', @financiallegendcode)"/></td> <xsl:for-each 
select="ancestor::financials/perioddata/period">
<xsl:variable name="idBase" select="@id"/> <td align="right"> 
<xsl:choose> <xsl:when test="$currentFinancial/@periodref=$idBase">
<xsl:value-of select="$currentFinancial[@periodref=$idBase]"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>....</xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
</xsl:for-each>
</tr>
</xsl:template>


On Wed, Jun 6, 2012 at 10:41 AM, daniel 
whitney<dbf(_dot_)whitney(_at_)gmail(_dot_)com>
 wrote:

Hi I'm having problems trying to figure out how to accomplish this 
and what the best strategy might be.

It's a 2 part problem. The first with keys. The second with ID/IDREF.

1. I have an xml file with a "lookup" table at the top 
(presentation/financiallegend). I want to output, as html, the 
"longname" element in a table cell whether it exists in the "data" 
or not. If it does exist in the data then the following  cells will 
contain the financial data. If it doesn't exist, the following cells 
would just be empty. My XSL file outputs the rows with financial 
data correctly, but does not include rows where only the "longname" 
exists (no financial data).

2. The header row of the table is a row of dates, each with a unique 
id. The subequent rows are financial data that have to align with 
the corresponding date id. The financial data include refs to their 
corresponding date ids, but of course there might be ids without a 
corresponding idref. So I have no idea how get the columns to align 
correctly.

This is a very basic example. The file itself has dozens of lookup 
tables (not too many IDs). So I'm wondering how to accomplisth this 
and what is the best strategy to keep things as clean and simple as 
possible.

Does having a schema contribute to the simplicity of the XSL 
somehow, in regards to keys? Or are they essentially separate entities?

Any help is, of course, greatly appreciated.

Thanks,

Dan

XML file:

<?xml version="1.0" encoding="windows-1252"?> <requesteddata> 
<presentation> <financiallegend> <code>60.00</code> 
<longname>Revenue - Operating/Total</longname> </financiallegend> 
<financiallegend> <code>29.00</code> <longname>Total 
Assets</longname> </financiallegend> <financiallegend> 
<code>73.00</code> <longname>Pre-Tax Income</longname> 
</financiallegend> <financiallegend> <code>2.00</code> 
<longname>Made-up filler</longname> </financiallegend> 
<financiallegend> <code>82.00</code> <longname>Net Income</longname> 
</financiallegend> </presentation> <company> <fpid>154</fpid> 
<legal>High River Gold Mines Ltd.</legal> <financials> <perioddata> 
<period id="154_2011-12-31_A_A_414785"> 
<perioddate>2011-12-31</perioddate>
<reporttype code="A">As reported</reporttype> </period> <period 
id="154_2010-12-31_A_R_414804"> <perioddate>2010-12-31</perioddate>
<reporttype code="R">Restated</reporttype> </period> <period 
id="154_2009-12-31_A_A_414700"> <perioddate>2010-12-31</perioddate>
<reporttype code="A">As reported</reporttype> </period> 
</perioddata> <financialdata> <financialgroup 
financiallegendcode="29.00" legend="Total Assets"> <financial 
periodref="154_2011-12-31_A_A_414785">1007827</financial>
<financial periodref="154_2010-12-31_A_R_414804">1102209</financial>
<financial periodref="154_2009-12-31_A_A_414700">209987</financial>
</financialgroup>
<financialgroup financiallegendcode="73.00" legend="Pre-Tax Income"> 
<financial periodref="154_2011-12-31_A_A_414785">231673</financial>
<financial periodref="154_2009-12-31_A_A_414700">191115</financial>
</financialgroup>
<financialgroup financiallegendcode="82.00" legend="Net Income"> 
<financial periodref="154_2010-12-31_A_R_414804">28999</financial>
<financial periodref="154_2009-12-31_A_A_414700">191203</financial>
</financialgroup>
<financialgroup financiallegendcode="60.00" legend="Revenue - 
Operating/Total"> <financial 
periodref="154_2011-12-31_A_A_414785">573949</financial>
<financial periodref="154_2010-12-31_A_R_414804">529765</financial>
<financial periodref="154_2009-12-31_A_A_414700">447635</financial>
</financialgroup>
</financialdata>
</financials>
</company>
<company>
<fpid>11</fpid>
<legal>Barrick Gold</legal>
<financials>
<perioddata>
<period id="11_2011-12-31_A_A_1">
<perioddate>2011-12-31</perioddate>
<reporttype code="A">As reported</reporttype> </period> <period 
id="11_2010-12-31_A_A_2"> <perioddate>2010-12-31</perioddate>
<reporttype code="A">As reported</reporttype> </period> 
</perioddata> <financialdata> <financialgroup 
financiallegendcode="29.00" legend="Total Assets"> <financial 
periodref="11_2011-12-31_A_A_1">1007827</financial>
<financial periodref="11_2010-12-31_A_A_2">1102209</financial>
</financialgroup>
<financialgroup financiallegendcode="82.00" legend="Net Income"> 
<financial periodref="11_2010-12-31_A_A_2">191203</financial>
</financialgroup>
<financialgroup financiallegendcode="60.00" legend="Revenue - 
Operating/Total"> <financial 
periodref="11_2011-12-31_A_A_1">573949</financial>
</financialgroup>
</financialdata>
</financials>
</company>
</requesteddata>


XSL file:

<?xml version="1.0" encoding="windows-1252"?> <xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
<xsl:output method="html" indent="yes" encoding="windows-1252"/> 
<xsl:key name="legend" match="longname" use="../code"/>

<xsl:template match="requesteddata"> <xsl:for-each select="company"> 
<table border="1"> <tr> <td colspan="4"> <xsl:value-of 
select="legal"/> </td> </tr> <xsl:apply-templates 
select="financials/perioddata"/> <xsl:apply-templates 
select="financials/financialdata/financialgroup[key('legend',
@financiallegendcode)]"/>
</table>
</xsl:for-each>
</xsl:template>

<xsl:template match="perioddata">
<tr>
<td>&#160;</td>
<xsl:for-each select="period">
<td>
<xsl:value-of select="perioddate"/><xsl:text>  - 
</xsl:text><xsl:value-of select="reporttype"/> </td> </xsl:for-each> 
</tr> </xsl:template>

<xsl:template match="financials/financialdata/financialgroup">
<tr>
<td><xsl:value-of select="key('legend', 
@financiallegendcode)"/></td> <xsl:for-each select="financial"> 
<td><xsl:value-of select="."/></td> </xsl:for-each> </tr> 
</xsl:template>

<xsl:template name="presentation"/>
</xsl:stylesheet>

Output HTML example:

<table border="1">
<tr>
<td colspan="4">High River Gold Mines Ltd.</td> </tr> <tr>
<td>&#160;</td><td>2011-12-31 - As Reported</td><td>2010-12-31 -
Restated</td><td>2009-12-31 - As Reported</td> </tr> <tr> 
<td>Revenue - 
Operating/Total</td><td>573949</td><td>529765</td><td>447635</td>
</tr>
<tr>
<td>Total Assets</td><td>1007827</td><td>1102209</td><td>209987</td>
</tr>
<tr>
<td>Pre-Tax Income</td><td>231673</td><td>&#160;</td><td>191115</td>
</tr>
<tr>
<td>Made-up filler</td><td>&#160;</td><td>&#160;</td><td>&#160;</td>
</tr>
<tr>
<td>Net Income</td><td>&#160;</td><td>28999</td><td>191203</td>
</tr>
</table>

<table border="1">
<tr>
<td colspan="4">Barrick Gold</td>
</tr>
<tr>
<td>&#160;</td><td>2011-12-31 - As Reported</td><td>2010-12-31 - As 
Reported</td> </tr> <tr> <td>Revenue - 
Operating/Total</td><td>573949</td><td>&#160;</td>
</tr>
<tr>
<td>Total Assets</td><td>1007827</td><td>1102209</td>
</tr>
<tr>
<td>Pre-Tax Income</td><td>&#160;</td><td>&#160;</td>
</tr>
<tr>
<td>Made-up filler</td><td>&#160;</td><td>&#160;</td>
</tr>
<tr>
<td>Net Income</td><td>&#160;</td><td>191203</td>
</tr>
</table>


--
======================================================================
Wendell Piez                           
 mailto:wapiez(_at_)mulberrytech(_dot_)com 
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635 
Suite 207                                          Phone: 301/315-9631 
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
 Mulberry Technologies: A Consultancy Specializing in SGML and XML 
======================================================================

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--