2017-09-26 99 views
0

我想創建xslt,我需要根據XML節點值檢查或取消選中複選框。如果descr節點包含或等於'Student',我想檢查學生複選框,否則應該取消選中它。如果decr節點包含Faculty或等於Faculty,則應該檢查Faculty複選框,否則應該取消選中該複選框。基於節點值的XSLT選擇複選框

我的XML是

<WI> 
    <Wi> 
    <last_nam>CLARK       </last_nam> 
    <first_nam>HUNTER    </first_nam> 
    <wit_flag>1</wit_flag> 
    <descr>Faculty  </descr> 
    </Wi> 
</WI> 
<WI> 
    <Wi> 
    <last_nam>MCMENAMIN      </last_nam> 
    <first_nam>COLLIN    </first_nam> 
    <wit_flag>1</wit_flag> 
    <descr>Student  </descr> 
    </Wi> 
</WI> 

XSLT是

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     </head> 
     <body> 
     <div class="container"> 
      <table border="1"> 
      <tr> 
       <td colspan="2"> 
       Person 
       <br /> 
       <xsl:value-of select="WI/Wi/lat_nam" /> 
       </td> 
      </tr> 
      <tr> 
       <td> 
       <input id="student_flag" name="student_flag" type="checkbox" 
        value="1"> 
        <xsl:choose> 
        <xsl:when test="WI/Wi/descr[contains(text(),Student]"> 
         <xsl:attribute name="value">1</xsl:attribute> 
         <xsl:attribute name="checked">true</xsl:attribute> 
         <xsl:value-of select="'Student'" /> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:attribute name="value">0</xsl:attribute> 
         <xsl:value-of select="'Student'" /> 
        </xsl:otherwise> 
        </xsl:choose> 
       </input> 
       </td> 
       <td> 
       <input id="Faculty_flag" name="Faculty_flag" type="checkbox" 
        value="1"> 
        <xsl:choose> 
        <xsl:when test="WI/Wi/descr[contains(text(),Faculty]"> 
         <xsl:attribute name="value">1</xsl:attribute> 
         <xsl:attribute name="checked">true</xsl:attribute> 
         <xsl:value-of select="'Faculty'" /> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:attribute name="value">0</xsl:attribute> 
         <xsl:value-of select="'Faculty'" /> 
        </xsl:otherwise> 
        </xsl:choose> 
       </input> 
       </td> 
      </tr> 
      </table> 
     </div> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

回答

0

嘗試設置checked="checked"而非checked="true"。這是HTML工作中布爾屬性的方式...

+0

謝謝您的回覆。它檢查了所有的複選框。我想根據條件檢查框,如果descr包含學生,那麼只應檢查複選框。但即使descr不包含Faculty,它也會檢查這兩個複選框。 – user2897967

+0

我修改了 1 Checked 它通過在''中添加descr文本並將true替換爲checked來工作。 – user2897967