XSLT将总计从一个HTML表转换为另一HTML表

时间:2018-08-16 18:55:07

标签: xml xslt xslt-1.0

我有以下XML,需要使用XSLT 1进行转换。该XML是不可变的,我无法创建新的元素或属性。

我在Cell__GroupingName上创建了一个键,并使用Meunchian分组将每个Cell划分为块。我遍历了每个区块中的每个单元,并列出了每个单元中的人员。

     <?xml version="1.0" encoding="Windows-1252" ?> 
    <DATASETLIST xmlns:dt="urn:schemas-microsoft-com:datatypes">
     <DATASET entity="CELL">
        <ROW>
            <Cell__Id>1010025012000000031980475</Cell__Id>
            <Cell__Name>A Block</Cell__Name>
            <Cell__Occupancy>36</Cell__Occupancy>
            <Cell__OccupancyCount>1</Cell__OccupancyCount>
            <Cell__GroupingName>A</Cell__GroupingName>
        </ROW>
        <ROW>
            <Cell__Id>1010025012000000031980476</Cell__Id>
            <Cell__Name >EB.101 Handicap</Cell__Name>
            <Cell__Occupancy>1</Cell__Occupancy>
            <Cell__OccupancyCount>0</Cell__OccupancyCount>
            <Cell__GroupingName>B</Cell__GroupingName>
        </ROW>
        <ROW>
            <Cell__Id>1010025012000000031980477</Cell__Id>
            <Cell__Name>EB.102</Cell__Name>
            <Cell__Occupancy>1</Cell__Occupancy>
            <Cell__OccupancyCount>0</Cell__OccupancyCount>
            <Cell__GroupingName>B</Cell__GroupingName>
        </ROW>
        <ROW>
            <Cell__Id>1010025012000000032172223</Cell__Id>
            <Cell__Name>Ec.101</Cell__Name>
            <Cell__Occupancy>1</Cell__Occupancy>
            <Cell__OccupancyCount>1</Cell__OccupancyCount>
            <Cell__GroupingName>B</Cell__GroupingName>
        </ROW>
    </DATASET>
        <DATASET entity="EVENT;JT">
        <ROW>
            <EVENT__Id>162325012000000046845209</EVENT__Id>
            <EVENT__GPPCCustodyTypeG>Warrant - Felony</EVENT__GPPCCustodyTypeG>
            <EVENT__WhiteboardIndicatorsG>Epileptic</EVENT__WhiteboardIndicatorsG>
            <EVENT__LabelCell>EC.101</EVENT__LabelCell>
            <JT__Cell_ID />
            <JT__Remarks/> 
            <DATASET entity="CHARGE">
                <ROW>
                    <CHARGE__SectionMergedNS>594(a)pc</CHARGE__SectionMergedNS>
                </ROW>
            </DATASET>
            <DATASET entity="P">
                <ROW>
                    <P__SurnameG>Person</P__SurnameG>
                    <P__Given1>one</P__Given1>
                </ROW>
            </DATASET>
        </ROW>
        <ROW>
            <EVENT__Id>162325012000000046937292</EVENT__Id>
            <EVENT__GPPCCustodyTypeG>Felony</EVENT__GPPCCustodyTypeG>
            <EVENT__WhiteboardIndicatorsG/>
            <EVENT__LabelCell>A Block</EVENT__LabelCell>
            <JT__Cell_ID>1010025012000000031980475</JT__Cell_ID>
            <JT__Remarks />

            <DATASET entity="CHARGE">
                <ROW>
                    <CHARGE__SectionMergedNS>594(a)pc</CHARGE__SectionMergedNS>
                </ROW>
            </DATASET>
            <DATASET entity="P">
                <ROW>
                    <P__SurnameG>Person2</P__SurnameG>
                    <P__Given1>Two</P__Given1>
                </ROW>
            </DATASET>
        </ROW>
        <ROW>
            <EVENT__Id>162325012000000046763873</EVENT__Id>
            <EVENT__GPPCCustodyTypeG>Misdemeanor</EVENT__GPPCCustodyTypeG>
            <EVENT__WhiteboardIndicatorsG>High blood pressure</EVENT__WhiteboardIndicatorsG>
            <EVENT__LabelCell>EB.103</EVENT__LabelCell>
            <JT__Cell_ID>1010025012000000032172223</JT__Cell_ID>
            <JT__Remarks/>

            <DATASET entity="CHARGE">
               <ROW>                    
                  <CHARGE__SectionMergedNS>11377(a)hs</CHARGE__SectionMergedNS>
                </ROW>
            </DATASET>
            <DATASET entity="P">
                <ROW>
                    <P__SurnameG>PERSON3</P__SurnameG>
                    <P__Given1>THREE</P__Given1>
                </ROW>-                
            </DATASET>
        </ROW>
    </DATASET>
</DATASETLIST>

我现在要做的是创建一个单独的表(代码底部的表),该表计算每个单元块的重罪,轻罪行为和可用空间。最好不必再次遍历循环。

    <xsl:key name="cells-by-blocks" match="//*[@entity='CELL']/ROW" 
    use="Cell__GroupingName" />
    <xsl:for-each select="//*[@entity='CELL']/ROW[count(. | key('cells-by- 
     blocks',Cell__GroupingName)[1]) = 1]">
    <xsl:variable name="blocks" select="key('cells-by-blocks', 
    Cell__GroupingName)" /> 
    <table>
        <thead>
            <th>Cell</th>
            <th>Name</th>
            <th>Charge</th>
            <th>Level</th> (felony or misd)
            <th>Remarks</th>
        </thead>
        <tbody>                                          
            <xsl:for-each select="$blocks">
              <xsl:for-each select="//*[JT__Cell_ID = current()/Cell__Id]">                                                             
                <tr>
                    <th class="cell-num">
                        <xsl:choose>
                            <xsl:when test="contains(EVENT__LabelCell, 'Block')">
                                <xsl:value-of select="position()" />
                            </xsl:when>
                            <xsl:otherwise>
                               <xsl:value-of select="EVENT__LabelCell" />
                            </xsl:otherwise>
                        </xsl:choose>                                                               
                    </th>
                    <td>
                        <xsl:value-of select="current()/DATASET/ROW/P__SurnameG"/>
                        <xsl:text>, </xsl:text>
                        <xsl:value-of select="current()/DATASET/ROW/P__Given1"/>
                    </td>                 
                    <td>
                        <xsl:value-of select="current()/DATASET/ROW/CHARGE__SectionMergedNS" />
                    </td>
                    <td>
                        <xsl:value-of select="EVENT__GPPCCustodyTypeG"/>
                    </td>
                    <td class="notes">
                        <xsl:value-of select="EVENT__WhiteboardIndicatorsG" />
                        <xsl:if test=" not(JT__Remarks = '') ">
                            <xsl:text>; </xsl:text>
                            <xsl:value-of select="JT__Remarks" /> 
                        </xsl:if>
                    </td>
                </tr>                                                               
            </xsl:for-each>                                                                 
        </xsl:for-each>
       </tbody>
    </table>
    <table id="totals" class="col-md-6 table-bordered totals-table">                                                    
    <tr>
        <h4>Count Breakdown:</h4>
      </tr>                                                     
            <tr>
                <th>Total</th>
                <th>Felony</th>
                <th>Misdemeanor</th>
                <th>Cells Available</th>
            </tr>                                                   
            <tr >
                <td>
                    <xsl:value-of select= How many bodies in this cell block/>
                </td>
                <td>
                   <xsl:value-of select= How many felonies in this block/> 
                </td>
                <td></td>
                <td></td>
            </tr>
    </table>
   </div>
   </div>
   <div class="pagebreak"></div>
   </div>                                  
  </xsl:for-each>           

如果我尝试将参数传递到模板中,则我的变量不在范围内,因为我不在for-each循环中。我已经尝试过计算HTML元素的数量,但是由于HTML是包装在CDATA中的,因此我无法猜测。 我敢肯定有一个简单的解决方案,但似乎看不到。

0 个答案:

没有答案