2015-06-17 82 views
0

我是一位XSL新手,並試圖根據數據子串的分組總結一些XML數據。將XSL 2.0計數按當前組邏輯轉換爲XSL 1.0邏輯

我一直試圖讓我的頭繞着Muenchian分組和計數,但一直未能得到它的工作。我看了一下XSL 2.0格式,並在5分鐘內用下面的代碼設法解決了我的問題。

我的問題是,我需要這個工作在XSL 1.0。

有人可以幫我把這個邏輯轉換成XSL 1.0格式嗎?

這是工作XSL 2.0代碼

<?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"/> 
<xsl:template match="/"> 
    <html> 
     <head> 
      <meta charset="utf-8"/> 
      <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0"/> 
     </head> 
     <body> 
      <p> 
       <table align="center"> 
        <thead> 
         <tr> 
          <th>Bay</th> 
          <th>Units</th> 
          <th>20'</th> 
          <th>40'</th> 
          <th>45'</th> 
          <th>Breakbulk</th> 
         </tr> 
        </thead> 
        <tbody> 
         <xsl:for-each-group select="StowplanTransactions/Group2" 
          group-by="substring(LocationIdentification/@LocationID, 1, 3)"> 
          <xsl:sort select="LocationIdentification/@LocationID"/> 
          <tr> 
           <td> 
            <xsl:value-of select="current-grouping-key()"/> 
           </td> 
           <td> 
            <xsl:value-of select="count(current-group())"/> 
           </td> 
           <td> 
            <xsl:value-of 
             select="count(current-group()/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '2']/@EquipmentSizeTypeIdentification)" 
            /> 
           </td> 
           <td> 
            <xsl:value-of 
             select="count(current-group()/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '4']/@EquipmentSizeTypeIdentification)" 
            /> 
           </td> 
           <td> 
            <xsl:value-of 
             select="count(current-group()/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '9' or substring(@EquipmentSizeTypeIdentification, 1, 1) = 'L']/@EquipmentSizeTypeIdentification)" 
            /> 
           </td> 
           <td> 
            <xsl:value-of 
             select="count(current-group()/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '']/@EquipmentSizeTypeIdentification)" 
            /> 
           </td> 
          </tr> 
         </xsl:for-each-group> 
        </tbody> 
       </table> 
      </p> 
     </body> 
    </html> 
</xsl:template> 

這是我的當前XLS 1.0代碼。分組正在工作,並且該組中的成員數正在工作,但是我需要計算組中屬性具有特定值的位置。目前,最後4計數字段都顯示爲0。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 
<xsl:key name="StowageCellBay" match="//StowplanTransactions/Group2/LocationIdentification" 
    use="substring(@LocationID, 1, 3)"/> 
<xsl:template match="/"> 
    <html> 
     <head> 
      <meta charset="utf-8"/> 
      <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0"/> 
     </head> 
     <body> 
      <p> 
       <table align="center" border="1"> 
        <thead> 
         <tr> 
          <th>Bay</th> 
          <th>Units</th> 
          <th>20'</th> 
          <th>40'</th> 
          <th>45'</th> 
          <th>Breakbulk</th> 
         </tr> 
        </thead> 
        <tbody> 
         <xsl:for-each select="StowplanTransactions/Group2"> 
          <xsl:sort select="LocationIdentification/@LocationID"/> 
          <xsl:for-each 
           select=" 
            LocationIdentification[generate-id() = 
            generate-id(key('StowageCellBay', substring(@LocationID, 1, 3))[1])]"> 
           <tr> 
            <td> 
             <xsl:value-of select="substring(@LocationID, 1, 3)"/> 
            </td> 
            <td> 
             <xsl:value-of 
              select="count(key('StowageCellBay', substring(@LocationID, 1, 3)))" 
             /> 
            </td> 
            <td> 
             <xsl:value-of 
              select="count(Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '2']/@EquipmentSizeTypeIdentification)" 
             /> 
            </td> 
            <td> 
             <xsl:value-of 
              select="count(Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '4']/@EquipmentSizeTypeIdentification)" 
             /> 
            </td> 
            <td> 
             <xsl:value-of 
              select="count(Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = 'L' or substring(@EquipmentSizeTypeIdentification, 1, 1) = '9']/@EquipmentSizeTypeIdentification)" 
             /> 
            </td> 
            <td> 
             <xsl:value-of 
              select="count(Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '']/@EquipmentSizeTypeIdentification)" 
             /> 
            </td> 
           </tr> 
          </xsl:for-each> 
         </xsl:for-each> 
        </tbody> 
       </table> 
      </p> 
     </body> 
    </html> 
</xsl:template> 

這裏是XML源的例子

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<StowplanTransactions> 
<Group2> 
    <LocationIdentification LocationID="0610390" /> 
    <Group3> 
     <EquipmentDetails EquipmentIdentificationNumber="11111111111" 
      EquipmentSizeTypeIdentification="22G1"/> 
    </Group3> 
</Group2> 
<Group2> 
    <LocationIdentification LocationID="0340612" /> 
    <Group3> 
     <EquipmentDetails EquipmentIdentificationNumber="22222222222" 
      EquipmentSizeTypeIdentification="42G1" /> 
    </Group3> 
</Group2> 
<Group2> 
    <LocationIdentification LocationID="0650004"/> 
    <Group3> 
     <EquipmentDetails EquipmentIdentificationNumber="33333333333" 
      EquipmentSizeTypeIdentification="" /> 
    </Group3> 
</Group2> 
<Group2> 
    <LocationIdentification LocationID="0650306"/> 
    <Group3> 
     <EquipmentDetails EquipmentIdentificationNumber="44444444444" 
      EquipmentSizeTypeIdentification="22G1" /> 
    </Group3> 
</Group2> 
<Group2> 
    <LocationIdentification LocationID="0730220"/> 
    <Group3> 
     <EquipmentDetails EquipmentIdentificationNumber="55555555555" 
      EquipmentSizeTypeIdentification="L591"/> 
    </Group3> 
</Group2> 

+1

請發佈您的輸入示例,以便我們可以使用某些東西。 –

+0

用正在使用的XML的示例更新了問題 – MarkT

回答

0

看看這個對你有意義:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 

<xsl:key name="grp" match="Group2" use="substring(LocationIdentification/@LocationID, 1, 3)"/> 

<xsl:template match="/StowplanTransactions"> 
    <table align="center" border="1"> 
     <thead> 
      <tr> 
       <th>Bay</th> 
       <th>Units</th> 
       <th>20'</th> 
       <th>40'</th> 
       <th>45'</th> 
       <th>Breakbulk</th> 
      </tr> 
     </thead> 
     <tbody> 
      <xsl:for-each select="Group2[generate-id()=generate-id(key('grp', substring(LocationIdentification/@LocationID, 1, 3))[1])]"> 
       <xsl:sort select="LocationIdentification/@LocationID"/> 
       <xsl:variable name="curr-key" select="substring(LocationIdentification/@LocationID, 1, 3)" />   
       <xsl:variable name="curr-group" select="key('grp', $curr-key)" /> 
       <tr> 
        <td> 
         <xsl:value-of select="$curr-key"/> 
        </td> 
        <td> 
         <xsl:value-of select="count($curr-group)"/> 
        </td> 
        <td> 
         <xsl:value-of select="count($curr-group/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '2']/@EquipmentSizeTypeIdentification)"/> 
        </td> 
        <td> 
         <xsl:value-of select="count($curr-group/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '4']/@EquipmentSizeTypeIdentification)"/> 
        </td> 
        <td> 
         <xsl:value-of select="count($curr-group/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '9' or substring(@EquipmentSizeTypeIdentification, 1, 1) = 'L']/@EquipmentSizeTypeIdentification)"/> 
        </td> 
        <td> 
         <xsl:value-of select="count($curr-group/Group3/EquipmentDetails[substring(@EquipmentSizeTypeIdentification, 1, 1) = '']/@EquipmentSizeTypeIdentification)"/> 
        </td> 
       </tr> 
      </xsl:for-each> 
     </tbody> 
    </table> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感謝Michael。這正是我需要的。 – MarkT