2016-06-14 50 views
0

因此,我有這個XML文件(下面),我試圖按照Customer列出Reservations。由於客戶是「內部」Reservations並且不是唯一的(多個客戶可以執行不同的預訂),所以我一直面臨一個問題:我可以根據Customer檢索合適的數量Reservations,但它們顯示客戶在文件,因爲我每次都拋出所有Reservations ...對於XSL文件中沒有重複節點的每個循環

你能幫我一下嗎?我不需要發送到已經處理的HTML文件Customers


我的XML文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<System xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Users/ASUS/Documents/lprog-xml/Reservations.xsd"> 
    <Reservations> 
     <Reservation id="b1"> 
      <CourierToken>abb456fb</CourierToken> 
      <CustomerToken>fsdg432n</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>12</day> 
        <month>12</month> 
        <year>2016</year> 
       </Calendar> 
      </BeginDate> 
      <Term>20</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>pms</Username> 
       <Password>sdasda</Password> 
       <FirstName>Pedro</FirstName> 
       <LastName>Pinto</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="a2"> 
      <CourierToken>dfsd43b2</CourierToken> 
      <CustomerToken>f3hu32mu</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>13</day> 
        <month>January</month> 
        <year>2017</year> 
       </Calendar> 
      </BeginDate> 
      <Term>5</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>xss</Username> 
       <Password>asdq</Password> 
       <FirstName>Xavier</FirstName> 
       <LastName>Silva</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="a342"> 
      <CourierToken>d4fd43b2</CourierToken> 
      <CustomerToken>f3htg2mu</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>10</day> 
        <month>March</month> 
        <year>2016</year> 
       </Calendar> 
      </BeginDate> 
      <Term>45</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>xss</Username> 
       <Password>asdq</Password> 
       <FirstName>Xavier</FirstName> 
       <LastName>Silva</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="b3"> 
      <CourierToken>desfg236</CourierToken> 
      <CustomerToken>43jmfh23</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>1</day> 
        <month>January</month> 
        <year>2017</year> 
       </Calendar> 
      </BeginDate> 
      <Term>8</Term> 
      <DropPoint id="g34"/> 
      <Customer> 
       <Username>dal</Username> 
       <Password>dasdg</Password> 
       <FirstName>Daniel</FirstName> 
       <LastName>Almeida</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="a56"> 
      <CourierToken>5bh4fdsf</CourierToken> 
      <CustomerToken>onfdsn43</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>23</day> 
        <month>9</month> 
        <year>2018</year> 
       </Calendar> 
      </BeginDate> 
      <Term>15</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>pops</Username> 
       <Password>iHack</Password> 
       <FirstName>Migas</FirstName> 
       <LastName>what</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="t345"> 
      <CourierToken>432njdas</CourierToken> 
      <CustomerToken>efg234jn</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>25</day> 
        <month>11</month> 
        <year>2567</year> 
       </Calendar> 
      </BeginDate> 
      <Term>56</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>SenorJobs</Username> 
       <Password>gr8b8m8ir88/8</Password> 
       <FirstName>Steve</FirstName> 
       <LastName>Jobs</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="p45"> 
      <CourierToken>acw4tsca</CourierToken> 
      <CustomerToken>ascfdv32</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>19</day> 
        <month>5</month> 
        <year>2020</year> 
       </Calendar> 
      </BeginDate> 
      <Term>7</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>SenorGates</Username> 
       <Password>VivaLaMicrosoft</Password> 
       <FirstName>Billy</FirstName> 
       <LastName>Gator</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="df45"> 
      <CourierToken>34rfdf3d</CourierToken> 
      <CustomerToken>554fgdvv</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>21</day> 
        <month>10</month> 
        <year>2020</year> 
       </Calendar> 
      </BeginDate> 
      <Term>20</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>CristianoPenaldo</Username> 
       <Password>saudadesIrina</Password> 
       <FirstName>Cristiano</FirstName> 
       <LastName>Ronaldo</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="po454"> 
      <CourierToken>dfsg345d</CourierToken> 
      <CustomerToken>sadfg345</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>31</day> 
        <month>2</month> 
        <year>2345</year> 
       </Calendar> 
      </BeginDate> 
      <Term>23</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>ZLATAN</Username> 
       <Password>iAmZlatan</Password> 
       <FirstName>Zlatan</FirstName> 
       <LastName>Ibrahimovic</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
     <Reservation id="sd34"> 
      <CourierToken>34567asd</CourierToken> 
      <CustomerToken>dssfdgh4</CustomerToken> 
      <BeginDate> 
       <Calendar type="Gregorian"> 
        <day>10</day> 
        <month>10</month> 
        <year>2030</year> 
       </Calendar> 
      </BeginDate> 
      <Term>4</Term> 
      <DropPoint id="f33"/> 
      <Customer> 
       <Username>PashaBiceps</Username> 
       <Password>maFriend</Password> 
       <FirstName>Pasha</FirstName> 
       <LastName>Biceps</LastName> 
       <Email>[email protected]</Email> 
      </Customer> 
     </Reservation> 
    </Reservations> 
    <DropPoints> 
     <DropPoint id="f33"> 
      <Designation>Quinta das Freiras</Designation> 
      <State>Free</State> 
      <Location> 
       <Street>Rio Tinto</Street> 
       <ZipCode>4435-074</ZipCode> 
       <Country>Portugal</Country> 
       <Coordinates> 
        <Longitude>32</Longitude> 
        <Latitude>52</Latitude> 
       </Coordinates> 
      </Location> 
     </DropPoint> 
     <DropPoint id="g34"> 
      <Designation>Norte Shopping</Designation> 
      <State>Free</State> 
      <Location> 
       <Street>Porto</Street> 
       <ZipCode>4488</ZipCode> 
       <Country>Portugal</Country> 
       <Coordinates> 
        <Longitude>24</Longitude> 
        <Latitude>12</Latitude> 
       </Coordinates> 
      </Location> 
     </DropPoint> 
    </DropPoints> 
</System> 

我的XSL文件:

<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/"> 
     <html> 
      <body> 
       <h2>Customers</h2> 
       <hr></hr><hr></hr> 
       <!-- select="System/Reservations/Reservation/Customer" --> 

       <xsl:for-each select="System/Reservations/Reservation/Customer"> 
        <xsl:variable name="customerEmail" select="Email"/> 

        <h4><b>Name: </b> <xsl:value-of select="FirstName"/> <xsl:value-of select="LastName"/> </h4> 
        <h4><b>E-mail: </b> <xsl:value-of select="Email"/> </h4> 

        <!-- Creates a table with information about reservations, for each client --> 
        <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>Reservation ID</th> 
         <th>Date</th> 
         <th>Duration (days)</th> 
         <th>Customer Token</th> 
        </tr> 

        <xsl:for-each select="../../Reservation"> 
         <xsl:variable name="customerEmailCurrentReservation" select="Customer/Email"/> 

         <!-- processes reservations for that customer only --> 
         <xsl:if test="$customerEmail=$customerEmailCurrentReservation"> 

          <tr>          
           <td><xsl:value-of select="@id"/></td> 
           <td><xsl:value-of select="BeginDate/Calendar/day"/>-<xsl:value-of select="BeginDate/Calendar/month"/>-<xsl:value-of select="BeginDate/Calendar/year"/></td> 
           <td><xsl:value-of select="Term"/></td> 
           <td><xsl:value-of select="CustomerToken"/></td> 
          </tr> 

         </xsl:if> 
        </xsl:for-each> 

        </table> <!-- End of the table for that customer --> 
        <br></br><hr></hr><br></br> 
       </xsl:for-each> 
      </body> 
     </html> 
    </xsl:template> 

</xsl:stylesheet> 

這是我獲得(在HTML轉換文件): enter image description here


這是我想要得到什麼: enter image description here

預先感謝您!

+0

這是一個*分組*問題 - 看到:HTTP://www.jenitennison。 com/xslt/grouping/muenchian.html以及Muenchian在SO上的衆多示例。 –

+0

@ michael.hor257k謝謝,我已經嘗試過使用它,但我遇到了一些問題...如果我在第一個for-each循環之前定義一個鍵,就像這個'',它說XSL無效......任何幫助?謝謝你,我是XSL新手... –

+0

你應該發佈你的嘗試,所以我們可以修復它,而不是從頭開始寫所有東西。而不是 - 你顯示的關鍵定義不會產生錯誤。 –

回答

1

我們需要的最後一件事是Muenchian分組的另一個例子。但因爲你似乎無法管理你自己,試試這樣:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:key name="reservation-by-customer" match="Reservation" use="Customer/Email" /> 

<xsl:template match="/System"> 
    <html> 
     <body> 
      <h2>Customers</h2> 
      <!-- for each distinct customer --> 
      <xsl:for-each select="Reservations/Reservation[count(. | key('reservation-by-customer', Customer/Email)[1]) = 1]"> 
       <!-- customer's details --> 
       <h4> 
        <xsl:text>Name: </xsl:text> 
        <xsl:value-of select="Customer/FirstName"/> 
        <xsl:text> </xsl:text> 
        <xsl:value-of select="Customer/LastName"/> 
       </h4> 
       <!-- customer's reservations --> 
       <table border="1"> 
        <tr> 
         <th>Reservation ID</th> 
         <th>Date</th> 
         <th>Duration (days)</th> 
         <th>Customer Token</th> 
        </tr> 
        <xsl:for-each select="key('reservation-by-customer', Customer/Email)"> 
         <tr>          
          <td> 
           <xsl:value-of select="@id"/> 
          </td> 
          <td> 
           <xsl:value-of select="BeginDate/Calendar/day"/> 
           <xsl:text>-</xsl:text> 
           <xsl:value-of select="BeginDate/Calendar/month"/> 
           <xsl:text>-</xsl:text> 
           <xsl:value-of select="BeginDate/Calendar/year"/> 
          </td> 
          <td> 
           <xsl:value-of select="Term"/> 
          </td> 
          <td> 
           <xsl:value-of select="CustomerToken"/> 
          </td> 
         </tr> 
        </xsl:for-each> 
       </table> 
       <hr/> 
      </xsl:for-each> 
     </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感謝!我不知道我們可以在'for-each'中使用這個鍵!再次感謝你 :) –