2016-06-14 47 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'中使用这个键!再次感谢你 :) –