解析XML文件中IDREF引用的标记内容

时间:2016-06-14 15:14:22

标签: xml xslt

所以,我有我的XML文件,我正在尝试为预订中的每个DropPoint检索DropPoint的名称,以便将其放在我的表格上。每个预留中的DropPoint字段由IDREF引用。

有没有办法将指定解析到我的表格而不保存id并迭代所有DropPoints以找出我正在处理哪一个?

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

这是XSL文件:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="Designation" match="Designation" use="@id"/>

<xsl:template match="/System/Reservations/">
<html>
<body>
<table border="1">
        <tr>
            <th>Courier Token</th>
            <th>Customer Token</th>
            <th>Begin Date</th>
            <th>Term</th>
            <th>Drop Point</th>
            <th>Customer Username</th>
            <th>Customer First Name</th>
            <th>Customer Email</th>
        </tr>
        <xsl:for-each select="/System/Reservations/Reservation">
            <tr>
                <td><xsl:value-of select="CourierToken"/></td>
                <td><xsl:value-of select="CustomerToken"/></td>
                <td><xsl:value-of select="BeginDate/Calendar/day"/></td>
                <td><xsl:value-of select="Term"/></td>
                <td><xsl:value-of select="key('Designation', @Designation)"/></td>
                <td><xsl:value-of select="Customer/Username"/></td>
                <td><xsl:value-of select="Customer/FirstName"/></td>
                <td><xsl:value-of select="Customer/Email"/></td>
            </tr>
        </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

如果您设置了密钥

<xsl:key name="drop-point" match="DropPoints/DropPoint" use="@id">

然后在处理Reservation时,您可以使用<xsl:value-of select="key('drop-point', DropPoint/@id)/Designation"/>输出引用的Designation的{​​{1}}。

另请注意,DropPoint应为<xsl:template match="/System/Reservations/">,不得尾随<xsl:template match="/System/Reservations">