XSL Foreach没有提取值

时间:2011-10-15 03:20:31

标签: html xml xslt foreach

我使用一些XSL和css设置了这个XML文档,使它看起来不错。我不能让这个foreach工作,我不知道我做错了什么。有人有任何想法吗?

XML

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="q3.xsl"?>
<expenseReport>
    <companyInfo>
        <name>John Doe</name>
        <email>jdoe@company.com</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 个答案:

答案 0 :(得分:1)

您的xsl:for-each已与expenseReport/technicalInfo个节点匹配,因此您需要在循环内只匹配当前节点:

<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>