2011-10-15 38 views
0

我得到這個XML文檔使用一些XSL和CSS設置,使它看起來不錯。我不能讓這個foreach工作,但我不知道我做錯了什麼。任何人有任何想法?XSL Foreach不拉值

XML

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="q3.xsl"?> 
<expenseReport> 
    <companyInfo> 
     <name>John Doe</name> 
     <email>[email protected]</email> 
     <empNum>1</empNum> 
     <companyCode>10010011</companyCode> 
    </companyInfo> 
    <timeFrame> 
     <costCenter /> 
     <startDate /> 
     <endDate /> 
    </timeFrame> 
    <purpose /> 
    <technicalInfo> 
     <date>9/9/09</date> 
     <desc>heading to shanghai</desc> 
     <miles>a lot</miles> 
     <mileage amt="0.00">1.21</mileage> 
     <airFare>7</airFare> 
     <other>fish?</other> 
     <meals>yes</meals> 
     <conf>gesundheit</conf> 
     <misc>champaign</misc> 
     <code>red</code> 
     <total price="0.00">107.00</total> 
    </technicalInfo> 
    <technicalInfo> 
     <date>10/10/10</date> 
     <desc>hawaii vacation</desc> 
     <miles>a bunch</miles> 
     <mileage amt="0.00">100.9</mileage> 
     <airFare>1234.29</airFare> 
     <other>nope</other> 
     <meals>lobster dinner</meals> 
     <conf>Still not sure what this is</conf> 
     <misc>clubs</misc> 
     <code>bluish green</code> 
     <total price="0.00">3000.87</total> 
    </technicalInfo> 
    <technicalInfo> 
     <date>11/11/11</date> 
     <desc>Moving to Flordia</desc> 
     <miles>not too many</miles> 
     <mileage amt="0.00">12.3</mileage> 
     <airFare /> 
     <other>Stopped at McDonalds</other> 
     <meals>Big Mac and Fries</meals> 
     <conf>No Idea</conf> 
     <misc>Bought Gold Steering Wheel</misc> 
     <code>clear</code> 
     <total price="0.00">35.83</total> 
    </technicalInfo> 
</expenseReport> 

XSL

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <html> 
    <head> 
    <title>Expense Report</title> 
    <link type="text/css" rel="stylesheet" href="q3.css" /> 
    </head> 
    <body> 
    <div id="container"> 
     <h1>Expense Report</h1> 
      <br /> 
     <table id="companyInfo" cellspacing="0px"> 
      <tr> 
       <td>Name:</td> 
       <td class="data"><xsl:value-of select="expenseReport/companyInfo/name"/></td> 
      </tr> 
      <tr> 
       <td>Email:</td> 
       <td class="data"><xsl:value-of select="expenseReport/companyInfo/email"/></td> 
      </tr> 
      <tr> 
       <td>Employee Number:</td> 
       <td class="data"><xsl:value-of select="expenseReport/companyInfo/empNum"/></td> 
      </tr> 
      <tr> 
       <td>Company Number:</td> 
       <td class="data"><xsl:value-of select="expenseReport/companyInfo/companyCode"/></td> 
      </tr> 
     </table> 
     <table id="timeFrame" cellspacing="0px"> 
      <tr> 
       <td>Cost Center: </td> 
       <td><xsl:value-of select="expenseReport/timeFrame/costCenter"/></td> 
      </tr> 
      <tr> 
       <td>Start Date: </td> 
       <td><xsl:value-of select="expenseReport/timeFrame/startDate"/></td> 
      </tr> 
      <tr> 
       <td>End Date:</td> 
       <td><xsl:value-of select="expenseReport/timeFrame/endDate"/></td> 
      </tr> 
     </table> 
     <table id="purpose" cellspacing="0px"> 
      <tr> 
       <td> 
        Purpose: 
       </td> 
       <td width="700px"> 
        <xsl:value-of select="expenseReport/purpose"/> 
       </td> 
      </tr> 
     </table> 
      <br /> 
      <br /> 
     <table id="techInfo" cellspacing="0px" width="900px"> 
      <tr> 
       <th>Date</th> 
       <th>Description</th> 
       <th>Miles</th> 
       <th>Milage</th> 
       <th>Airfare</th> 
       <th>Other</th> 
       <th>Meals</th> 
       <th>Conf.</th> 
       <th>Misc</th> 
       <th>Code</th> 
       <th>Total</th> 
      </tr> 
      <xsl:for-each select="expenseReport/technicalInfo"> 
      <tr> 

        <td><xsl:value-of select="expenseReport/technicalInfo/date"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/desc"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/miles"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/milage"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/airfare"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/other"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/meals"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/conf"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/misc"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/code"/></td> 
        <td><xsl:value-of select="expenseReport/technicalInfo/total"/></td> 
      </tr> 
      </xsl:for-each> 
     </table> 
    </div> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

它顯示它爲空白數據。也許我不確定這個foreach是如何工作的。

回答

1

xsl:for-each已經匹配expenseReport/technicalInfo節點,therfore內循環,你只需要在當前節點匹配這樣:

<xsl:for-each select="expenseReport/technicalInfo"> 
    <tr> 

    <td> 
     <xsl:value-of select="date"/> 
    </td> 
    <td> 
     <xsl:value-of select="desc"/> 
    </td> 
    <td> 
     <xsl:value-of select="miles"/> 
    </td> 
    <td> 
     <xsl:value-of select="milage"/> 
    </td> 
    <td> 
     <xsl:value-of select="airfare"/> 
    </td> 
    <td> 
     <xsl:value-of select="other"/> 
    </td> 
    <td> 
     <xsl:value-of select="meals"/> 
    </td> 
    <td> 
     <xsl:value-of select="conf"/> 
    </td> 
    <td> 
     <xsl:value-of select="misc"/> 
    </td> 
    <td> 
     <xsl:value-of select="code"/> 
    </td> 
    <td> 
     <xsl:value-of select="total"/> 
    </td> 
    </tr> 
</xsl:for-each> 
+0

ohhhhh好了,這是有道理的xD謝謝! –