For-Each循环,在XSL文件中没有重复的节点

时间:2016-06-14 19:01:34

标签: xml xslt

所以,我有这个XML文件(下面),我正在尝试按Reservations列出Customer。由于客户“内部”Reservations并且不是唯一的(多个客户可以执行不同的预订),因此我遇到了一个问题:我可以检索到ReservationsCustomerReservations但是它们显示了客户在文件中存在的次数,因为我每次都会抛出所有Customers ...

请你帮帮我吗?我不需要发送已经处理过的HTML文件<?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>xmllover4everAndEver64cm@email.com</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>theincredibleone@email.com</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>theincredibleone@email.com</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>dokkanBattleFTW@email.com</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>getHackedM8@rekt.com</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>iOwnAppleBitches@email.com</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>cyaSteve@email.com.apple.pt</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>esteMessiJaMeteNojo@email.com</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>rektByZlatan@email.com</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>youAreMyBrotherMyFriend@email.com</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>

我的XML文件:

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

我的XSL文件:

scG.Clear(SystemColors.Control);

这是我得到的(在HTML转换文件中): enter image description here

这就是我想要获得的: enter image description here

提前谢谢!

1 个答案:

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