Search code examples
xslt

XSLT collect value in table thead


In this xslt I am try to get the count of how many th I am making. Because I need to know how many <input type="radio"></input> I need to make. I created this variable set But it looks like it is out of scope? is there any other way to do this?

XML

  <MeetingPollingQuestion>
    <MeetingPollingQuestionId>472</MeetingPollingQuestionId>
    <MeetingPollingId>66</MeetingPollingId>
    <MeetingPollingQuestionTypeId>3</MeetingPollingQuestionTypeId>
    <SequenceOrder>1</SequenceOrder>
    <IsVisible>true</IsVisible>
    <MeetingPollingParts>
      <MeetingPollingParts>
        <MeetingPollingPartsId>1368</MeetingPollingPartsId>
        <MeetingPollingPartsTypeId>1</MeetingPollingPartsTypeId>
        <MeetingPollingQuestionId>472</MeetingPollingQuestionId>
        <MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2270</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>1</MeetingPollingPartsValuesTypeId>
            <QuestionValue>sdd</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
        </MeetingPollingPartsValues>
      </MeetingPollingParts>
      <MeetingPollingParts>
        <MeetingPollingPartsId>1370</MeetingPollingPartsId>
        <MeetingPollingPartsTypeId>3</MeetingPollingPartsTypeId>
        <MeetingPollingQuestionId>472</MeetingPollingQuestionId>
        <MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2272</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>3</MeetingPollingPartsValuesTypeId>
            <QuestionValue>Yes</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2273</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>3</MeetingPollingPartsValuesTypeId>
            <QuestionValue>No</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2274</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>3</MeetingPollingPartsValuesTypeId>
            <QuestionValue>Abstain</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
        </MeetingPollingPartsValues>
      </MeetingPollingParts>
      <MeetingPollingParts>
        <MeetingPollingPartsId>1369</MeetingPollingPartsId>
        <MeetingPollingPartsTypeId>4</MeetingPollingPartsTypeId>
        <MeetingPollingQuestionId>472</MeetingPollingQuestionId>
        <MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2271</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>4</MeetingPollingPartsValuesTypeId>
            <QuestionValue>Is Required</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
        </MeetingPollingPartsValues>
      </MeetingPollingParts>
      <MeetingPollingParts>
        <MeetingPollingPartsId>1371</MeetingPollingPartsId>
        <MeetingPollingPartsTypeId>5</MeetingPollingPartsTypeId>
        <MeetingPollingQuestionId>472</MeetingPollingQuestionId>
        <MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2275</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>5</MeetingPollingPartsValuesTypeId>
            <QuestionValue>Row 1</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
          <MeetingPollingPartsValues>
            <MeetingPollingPartsValuesId>2276</MeetingPollingPartsValuesId>
            <MeetingPollingPartsId>0</MeetingPollingPartsId>
            <MeetingPollingPartsValuesTypeId>5</MeetingPollingPartsValuesTypeId>
            <QuestionValue>Row 3</QuestionValue>
            <FileManagerId>0</FileManagerId>
          </MeetingPollingPartsValues>
        </MeetingPollingPartsValues>
      </MeetingPollingParts>
    </MeetingPollingParts>
  </MeetingPollingQuestion>

XSLT

<xsl:for-each select="MeetingPollingParts/MeetingPollingParts">
   <table class="table" style="width:100%;border-color:gray">
   <xsl:choose>
       <xsl:when test="MeetingPollingPartsTypeId = 3">
           <thead>
             <tr>
               <xsl:for-each select="MeetingPollingPartsValues/MeetingPollingPartsValues">
                 <xsl:variable name="set" select="MeetingPollingPartsValues/MeetingPollingPartsValues" />
               
                 <th>
                   <xsl:value-of select="QuestionValue" disable-output-escaping="yes"/>
                 </th>
               </xsl:for-each>
             </tr>
           </thead>
       </xsl:when>
       <xsl:when test="MeetingPollingPartsTypeId = 5">
         <tbody>
               <xsl:for-each select="MeetingPollingPartsValues/MeetingPollingPartsValues">
                 <tr>
                   <td style="padding: 8px;line-height: 1.42857143;vertical-align: top;border-top: 1px solid #ddd;">
                     <xsl:value-of select="QuestionValue" disable-output-escaping="yes"/>
                   </td>
                   <td style="padding: 8px;line-height: 1.42857143;vertical-align: top;border-top: 1px solid #ddd;">
                     <input type="radio"></input>
                   </td>
                 </tr>
               </xsl:for-each>
           </tbody>
       </xsl:when>
   </xsl:choose>
   </table>
 </xsl:for-each>

OutPut enter image description here

HTML Desired Output

<table class="table">
  <thead>
    <tr>
      <th/>
      <th>Yes</th>
      <th>No</th>
      <th>Abstain</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="bold">Row 1</td>
      <td>
        <input type="radio"/>
      </td>
      <td>
        <input type="radio"/>
      </td>
      <td>
        <input type="radio"/>
      </td>
    </tr>
    <tr>
      <td class="bold">Row 3</td>
      <td>
        <input type="radio"/>
      </td>
      <td>
        <input type="radio"/>
      </td>
      <td>
        <input type="radio"/>
      </td>
    </tr>
  </tbody>
</table>


Solution

  • Try this:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:template match="MeetingPollingQuestion">
        <!-- Lets store the the headerCells in a variable for multiple use  -->
        <xsl:variable name="headerCells" select="MeetingPollingParts/MeetingPollingParts[MeetingPollingPartsTypeId=3]/MeetingPollingPartsValues/MeetingPollingPartsValues"/>
        <table class="table">
          <thead>
            <tr>
              <th/>
              <xsl:for-each select="$headerCells">
                <th>
                  <xsl:value-of select="QuestionValue" disable-output-escaping="yes"/>
                </th>
              </xsl:for-each>
            </tr>
          </thead>
          <tbody>
            <xsl:for-each select="MeetingPollingParts/MeetingPollingParts[MeetingPollingPartsTypeId='5']/MeetingPollingPartsValues/MeetingPollingPartsValues">
              <tr>
                <td class="bold">
                  <xsl:value-of select="QuestionValue" disable-output-escaping="yes"/>
                </td>
                <xsl:for-each select="$headerCells">
                  <td>
                    <input type="radio"></input>
                  </td>
                </xsl:for-each>
              </tr>
            </xsl:for-each>
          </tbody>
                
        </table>
      </xsl:template> 
      
    </xsl:stylesheet>