Generating a Fact Table from a presentation linkbase
<p>This is a follow up question from <a href="https://stackoverflow.com/questions/36087829/xbrl-dimensions-linkbase-parsing">this SO question</a>.</p>

<p>Having now created the Model Structure from the presentation linkbase found <a href="https://www.sec.gov/Archives/edgar/data/320193/000119312516439878/0001193125-16-439878-index.htm" rel="nofollow noreferrer">here</a>, I now would like to extract the Fact Table information.</p>

<p>The model structure is:</p>

<pre><code>us-gaap_IncomeStatementAbstract

Statement [Table]                                           us-gaap_StatementTable
    Legal Entity [Axis]                                     dei_LegalEntityAxis
        Entity [Domain]                                     dei_EntityDomain
    Statement [Line Items]                                  us-gaap_StatementLineItems
        Net sales                                           us-gaap_SalesRevenueNet
        Cost of sales                                       us-gaap_CostOfGoodsAndServicesSold
        Gross margin                                        us-gaap_GrossProfit
        Operating expenses:                                 us-gaap_OperatingExpensesAbstract
            Research and development                        us-gaap_ResearchAndDevelopmentExpense
            Selling, general and administrative             us-gaap_SellingGeneralAndAdministrativeExpense
            Total operating expenses                        us-gaap_OperatingExpenses
        Operating income                                    us-gaap_OperatingIncomeLoss
        Other income/(expense), net                         us-gaap_NonoperatingIncomeExpense
        Income before provision for income taxes            us-gaap_IncomeLossFromContinuingOperationsBeforeIn...
        Provision for income taxes                          us-gaap_IncomeTaxExpenseBenefit
        Net income                                          us-gaap_NetIncomeLoss
        Earnings per share:                                 us-gaap_EarningsPerShareAbstract
            Basic                                           us-gaap_EarningsPerShareBasic
            Diluted                                         us-gaap_EarningsPerShareDiluted
        Shares used in computing earnings per share:        us-gaap_WeightedAverageNumberOfSharesOutstandingAb...
            Basic                                           us-gaap_WeightedAverageNumberOfSharesOutstandingBa...
            Diluted                                         us-gaap_WeightedAverageNumberOfDilutedSharesOutsta...
        Cash dividends declared per share                   us-gaap_CommonStockDividendsPerShareDeclared
</code></pre>

<p>The Fact Table that I am trying to recreate from the xml documents contains this information:</p>

<p><a href="https://i.stack.imgur.com/inqai.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/inqai.png" alt="enter image description here"></a></p>

<p>This fact table can then be rendered to produce this table:</p>

<p><a href="https://i.stack.imgur.com/GLwR5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GLwR5.png" alt="enter image description here"></a></p>

<p>The problem is that I am not sure how to get the specific facts for each line item. For example, the model structure contains the element <code>Net sales</code> which has the concept <code>us-gaap_SalesRevenueNet</code>. The <a href="https://www.sec.gov/Archives/edgar/data/320193/000119312516439878/aapl-20151226.xml" rel="nofollow noreferrer">instance document</a> contains the following references to <code>us-gaap_SalesRevenueNet</code>:</p>

<pre><code>    contextRef  decimals    fact_value  id_x    tag unitRef {http://www.w3.org/1999/xlink}arcrole   {http://www.w3.org/1999/xlink}href  {http://www.w3.org/1999/xlink}type  {http://www.w3.org/2001/XMLSchema-instance}nil  cik_x   cik_y   id_y    startDate   endDate instant
471 eol_PE2035----1510-Q0015_STD_91_20141227_0  -6  74599000000 id_5473321_00DC1C18-22C7-426B-AF70-F6B281547EC4_2_0 {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20141227_0  2014-09-28  2014-12-27  
527 eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1138497  -6  30566000000 id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_1002_0  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1138497  2014-09-28  2014-12-27  
529 eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1173722  -6  5448000000  id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_2002_6  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1173722  2014-09-28  2014-12-27  
531 eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1181338  -6  16144000000 id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_3002_4  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1181338  2014-09-28  2014-12-27  
533 eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1184739  -6  17214000000 id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_4002_2  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1184739  2014-09-28  2014-12-27  
535 eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1257623  -6  5227000000  id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_5002_8  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20141227_0_1106947x1257623  2014-09-28  2014-12-27  
628 eol_PE2035----1510-Q0015_STD_91_20151226_0  -6  75872000000 id_5473321_00DC1C18-22C7-426B-AF70-F6B281547EC4_1_0 {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20151226_0  2015-09-27  2015-12-26  
701 eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1138497  -6  29325000000 id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_1001_0  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1138497  2015-09-27  2015-12-26  
703 eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1173722  -6  4794000000  id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_2001_6  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1173722  2015-09-27  2015-12-26  
705 eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1181338  -6  18373000000 id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_3001_4  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1181338  2015-09-27  2015-12-26  
707 eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1184739  -6  17932000000 id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_4001_2  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1184739  2015-09-27  2015-12-26  
709 eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1257623  -6  5448000000  id_5473321_493F2921-7163-4A93-9BC8-89E0F5E62B3B_5001_8  {http://fasb.org/us-gaap/2015-01-31}SalesRevenueNet iso4217_USD                 0000320193  0000320193  eol_PE2035----1510-Q0015_STD_91_20151226_0_1106947x1257623  2015-09-27  2015-12-26
</code></pre>

<p>Of the 12 facts shown, only two are required for the fact table - they are the facts with the contextRef of <code>eol_PE2035----1510-Q0015_STD_91_20141227_0</code> and <code>eol_PE2035----1510-Q0015_STD_91_20151226_0</code>. </p>

<p>These contextRef id's correspond to the following facts in the instance document:</p>

<pre><code>&lt;context id="eol_PE2035----1510-Q0015_STD_91_20141227_0"&gt;
&lt;entity&gt;
&lt;identifier scheme="http://www.sec.gov/CIK"&gt;0000320193&lt;/identifier&gt;
&lt;/entity&gt;
&lt;period&gt;
&lt;startDate&gt;2014-09-28&lt;/startDate&gt;
&lt;endDate&gt;2014-12-27&lt;/endDate&gt;
&lt;/period&gt;
&lt;/context&gt;

&lt;context id="eol_PE2035----1510-Q0015_STD_91_20151226_0"&gt;
&lt;entity&gt;
&lt;identifier scheme="http://www.sec.gov/CIK"&gt;0000320193&lt;/identifier&gt;
&lt;/entity&gt;
&lt;period&gt;
&lt;startDate&gt;2015-09-27&lt;/startDate&gt;
&lt;endDate&gt;2015-12-26&lt;/endDate&gt;
&lt;/period&gt;
&lt;/context&gt;
</code></pre>

<p>These contextRefs are essentially the <code>Period Axis</code> elements. I can't tell how the period axis are being determined from the xml <a href="https://www.sec.gov/Archives/edgar/data/320193/000119312516439878/0001193125-16-439878-index.htm" rel="nofollow noreferrer">documents</a> and, therefore, cannot reproduce the fact table. Can anyone shed any light on how to do this?</p>