2017-07-19 62 views
0

這是我上一個問題的後續內容。我開始用一個表格更是把格式:XSLT:使用多個鍵製作網格

A1,B1,C1 

A2,B2,C2 

A3,B3,C3 

現在結果此表重複高達基於記錄的實例值的5倍。單元格中的值對於表格的每個實例都是不同的。

我正在使用的XML爲每條記錄提供一個單元值以及DayOfWeek(列),流(行)和實例(表的迭代)值。

感謝以前的幫手我現在有XSLT,它可以讓我創建一個網格(列x行)。但是,當我添加一個額外的鍵(逐個實例)遍歷實例時,XSLT失敗。

目標結果會是什麼樣子:

Instance 1 
A1,B1,C1 

A2, ,C2 

A3,B3,C3 

Instance 2 
D1,E1,F1 

D2, ,F2 

D3,E3,F3 

可以有多達5分的情況下,7天一週的任何數量的行。

UPDATE 我已經更新了Michael的貢獻之後的XSLT,但是我仍然不在那裏。目前的結果是

A1 A1 A1 A1 B1 B1 B1 C1 C1 C1 C1 
A2 A2 A2 A2   C2 C2 C2 C2 
A3 A3 A3 A3 B3 B3 B3 C3 C3 C3 C3 

A1 A1 A1 A1 B1 B1 B1 C1 C1 C1 C1 
A2 A2 A2 A2   C2 C2 C2 C2 
A3 A3 A3 A3 B3 B3 B3 C3 C3 C3 C3 

我有兩個迭代的表,但它們包含相同的數據,並且列重複了太多次!

XSL:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:key name="row-by-instance" match="DataRow" use="Instance" /> 

<xsl:key name="row-by-stream" match="DataRow" use="concat(Instance, '|', Stream)" /> 
<xsl:key name="row-by-day" match="DataRow" use="concat(Instance, '|', Stream,'|',DayOfWeek)" /> 

<xsl:template match="/QueryResults"> 
    <!-- a column for each distinct day of week --> 
    <xsl:variable name="columns" select="DataRow[generate-id() = generate-id(key('row-by-day',concat(Instance, '|', DayOfWeek))[1])]" /> 

    <xsl:for-each select="DataRow[count(. | key('row-by-instance', Instance)[1]) = 1]"> 
    <table border="1"> 
     <!-- a row for each distinct stream --> 
     <!--<xsl:for-each select="DataRow[count(. | key('row-by-stream', Instance)[1]) = 1]">--> 
     <xsl:for-each select="key('row-by-instance', Instance)[count(. | key('row-by-stream', concat(Instance, '|', Stream))[1]) = 1]"> 
      <xsl:variable name="stream" select="key('row-by-stream', concat(Instance, '|', Stream))" /> 
      <tr> 
       <!-- a cell for each column --> 
       <xsl:for-each select="key('row-by-instance', Instance)[count(. | key('row-by-day', concat(Instance, '|', Stream,'|',DayOfWeek))[1]) = 1]"> 
        <xsl:sort select="DayOfWeek" data-type="number" order="ascending"/> 
        <td> 
         <xsl:value-of select="$stream[DayOfWeek = current()/DayOfWeek]/DisplayStuff" /> 
        </td> 
       </xsl:for-each> 
      </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:for-each> 
</xsl:template> 

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<QueryResults> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>1</Stream> 
    <DayOfWeek>1</DayOfWeek> 
    <DisplayStuff>A1</DisplayStuff> 
</DataRow> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>1</Stream> 
    <DayOfWeek>2</DayOfWeek> 
    <DisplayStuff>B1</DisplayStuff> 
</DataRow> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>1</Stream> 
    <DayOfWeek>3</DayOfWeek> 
    <DisplayStuff>C1</DisplayStuff> 
</DataRow> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>2</Stream> 
    <DayOfWeek>1</DayOfWeek> 
    <DisplayStuff>A2</DisplayStuff> 
</DataRow> 

<DataRow> 
    <Instance>1</Instance> 
    <Stream>2</Stream> 
    <DayOfWeek>3</DayOfWeek> 
    <DisplayStuff>C2</DisplayStuff> 
</DataRow> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>3</Stream> 
    <DayOfWeek>1</DayOfWeek> 
    <DisplayStuff>A3</DisplayStuff> 
</DataRow> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>3</Stream> 
    <DayOfWeek>2</DayOfWeek> 
    <DisplayStuff>B3</DisplayStuff> 
</DataRow> 
<DataRow> 
    <Instance>1</Instance> 
    <Stream>3</Stream> 
    <DayOfWeek>3</DayOfWeek> 
    <DisplayStuff>C3</DisplayStuff> 
</DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>1</Stream> 
     <DayOfWeek>1</DayOfWeek> 
     <DisplayStuff>D1</DisplayStuff> 
    </DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>1</Stream> 
     <DayOfWeek>2</DayOfWeek> 
     <DisplayStuff>E1</DisplayStuff> 
    </DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>1</Stream> 
     <DayOfWeek>3</DayOfWeek> 
     <DisplayStuff>F1</DisplayStuff> 
    </DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>2</Stream> 
     <DayOfWeek>1</DayOfWeek> 
     <DisplayStuff>D2</DisplayStuff> 
    </DataRow> 

    <DataRow> 
     <Instance>2</Instance> 
     <Stream>2</Stream> 
     <DayOfWeek>3</DayOfWeek> 
     <DisplayStuff>E2</DisplayStuff> 
    </DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>3</Stream> 
     <DayOfWeek>1</DayOfWeek> 
     <DisplayStuff>D3</DisplayStuff> 
    </DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>3</Stream> 
     <DayOfWeek>2</DayOfWeek> 
     <DisplayStuff>E3</DisplayStuff> 
    </DataRow> 
    <DataRow> 
     <Instance>2</Instance> 
     <Stream>3</Stream> 
     <DayOfWeek>3</DayOfWeek> 
     <DisplayStuff>F3</DisplayStuff> 
    </DataRow>  
</QueryResults> 
+1

目前還不清楚你的實例是什麼意思。轉換給定輸入示例的預期結果是什麼? –

+0

道歉 - 我添加了預期的輸出和一些更多的細節。 – iainc

+0

我不明白你的輸出。這應該是每個實例的單獨表格,或者是什麼? –

回答

0

嗯,你已經採取了一些錯誤的接入方式。看看這可以幫助你回到正軌:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="row-by-instance" match="DataRow" use="Instance" /> 
<xsl:key name="row-by-day" match="DataRow" use="DayOfWeek" /> 
<xsl:key name="row-by-stream" match="DataRow" use="concat(Instance, '|', Stream)" /> 

<xsl:template match="/QueryResults"> 
    <!-- a column for each distinct day of week (in any instance) --> 
    <xsl:variable name="columns" select="DataRow[generate-id() = generate-id(key('row-by-day', DayOfWeek)[1])]" /> 
    <!-- a table for each distinct instance --> 
    <xsl:for-each select="DataRow[count(. | key('row-by-instance', Instance)[1]) = 1]"> 
     <xsl:variable name="instance-rows" select="key('row-by-instance', Instance)" /> 
     <table border="1"> 
      <!-- a row for each distinct stream in this instance --> 
      <xsl:for-each select="$instance-rows[generate-id() = generate-id(key('row-by-stream', concat(Instance, '|', Stream))[1])]"> 
       <xsl:variable name="stream-rows" select="key('row-by-stream', concat(Instance, '|', Stream))" /> 
       <tr> 
        <!-- a cell for each column --> 
        <xsl:for-each select="$columns"> 
         <xsl:sort select="DayOfWeek" data-type="number" order="ascending"/> 
         <td> 
          <xsl:value-of select="$stream-rows[DayOfWeek = current()/DayOfWeek]/DisplayStuff" /> 
         </td> 
        </xsl:for-each> 
       </tr> 
      </xsl:for-each> 
     </table> 
    </xsl:for-each> 
</xsl:template>  

</xsl:stylesheet> 

請注意,這會產生所有表的一列裏的,這樣的表會保持一致。如果您希望每個表都有自己的列(即跳過沒有數據的星期幾),那麼您將不得不做不同的事情 - 最重要的是,您需要在row-by-day密鑰中包含Instance