XSLT当前组选择第一个和最后一个元素

时间:2018-09-23 17:23:01

标签: html xml xslt xpath

我有这个xml:

    <LocatorInfo>
        <LocationAddr>
            <StreetNum>182</StreetNum>
            <StreetName>Eastern Avenue</StreetName>
            <City>Fall River</City>
            <StateProv>MA</StateProv>
            <PostalCode>02723</PostalCode>
            <Country>USA</Country>
            <GEOCode>
                <Latitude>41.694600119635915</Latitude>
                <Longitude>-71.13038416718094</Longitude>
            </GEOCode>
        </LocationAddr>
        <LocationTimestamp>4/6/2018 4:17:37 PM</LocationTimestamp>
        <LocationDistance>4.338 miles</LocationDistance>
        <Accuracy>1.985 miles</Accuracy>
    </LocatorInfo>
    <LocatorInfo>
    <LocationAddr>
      <StreetNum>285</StreetNum>
      <StreetName>Wastern Avenue</StreetName>
      <City>Ball River</City>
      <StateProv>MA</StateProv>
      <PostalCode>02723</PostalCode>
      <Country>USA</Country>
      <GEOCode>
        <Latitude>41.694600119635915</Latitude>
        <Longitude>-71.13038416718094</Longitude>
      </GEOCode>
    </LocationAddr>
    <LocationTimestamp>4/7/2018 7:00:03 AM</LocationTimestamp>
    <LocationDistance>4.338 miles</LocationDistance>
    <Accuracy>1.985 miles</Accuracy>
  </LocatorInfo>
<LocatorInfo>
        <LocationAddr>
            <StreetNum>182</StreetNum>
            <StreetName>Eastern Avenue</StreetName>
            <City>Fall River</City>
            <StateProv>MA</StateProv>
            <PostalCode>02723</PostalCode>
            <Country>USA</Country>
            <GEOCode>
                <Latitude>41.694600119635915</Latitude>
                <Longitude>-71.13038416718094</Longitude>
            </GEOCode>
        </LocationAddr>
        <LocationTimestamp>4/7/2018 4:17:37 PM</LocationTimestamp>
        <LocationDistance>4.338 miles</LocationDistance>
        <Accuracy>1.985 miles</Accuracy>
    </LocatorInfo>

我有xslt的这一部分:

<xsl:for-each-group select="n1:MBMDVRs/mbg:LocatorInfo" group-by="string-join((mbg:LocationAddr/mbg:StreetNum, mbg:LocationAddr/mbg:StreetName, mbg:LocationAddr/mbg:City, mbg:LocationAddr/mbg:StateProv), '!')">
                                                    <tr style="border-collapse:collapse; font-size:inherit; text-align:center; " class="wide">
                                                        <td style="width:0.95in; ">
                                                        </td> 
                                                        <td style="width:2.57in; "> 
                                                            <xsl:copy-of select="mbg:LocationAddr/mbg:StreetNum, mbg:LocationAddr/mbg:StreetName, mbg:LocationAddr/mbg:City, mbg:LocationAddr/mbg:StateProv" />
                                                        </td>
                                                        <td style="width:1.50in; "> 
                                                        <xsl:for-each select="current-group()">
                                                            <xsl:copy-of select="(//mbg:LocationTimestamp)[last()]"/>
                                                            </xsl:for-each>
                                                        </td>
                                                        <td style="width:2.13in; ">
                                                        <xsl:for-each select="current-group()">
                                                            <xsl:copy-of select="mbg:LocationTimestamp[1]"/>
                                                            </xsl:for-each>
                                                        </td>
                                                        <td style="width:2.27in; ">
                                                             <xsl:value-of select="count(current-group())" />
                                                        </td>
                                                    </tr>
                                                       </xsl:for-each-group>

我不知道为什么在这种情况下last和[1]命令不起作用。他们什么都不做。我想在一个td中获取每个组的第一个时间戳,在另一个td中获取最后的时间戳。但是它们在两个单元中捆绑在一起。如果我使用[1]并且最后一次使用,则返回相同的结果。是否因为时间戳不在组中?我该如何绕过呢?不知道这样是否有可能,或者我是否必须尝试一种完全不同的方式。

名称空间使用正确,我只是将其保留在xml中。

1 个答案:

答案 0 :(得分:0)

将所需的花括号添加到第二个表达式中,如下所示。在示例代码中,第一个和最后一个值由于xsl:for-each而输出两次(不知道这有什么用)。

<td style="width:1.50in; "> 
    <xsl:for-each select="current-group()">
        <xsl:copy-of select="(//mbg:LocationTimestamp)[last()]"/>
    </xsl:for-each>
</td>
<td style="width:2.13in; ">
    <xsl:for-each select="current-group()">
        <xsl:copy-of select="(//mbg:LocationTimestamp)[1]"/>
    </xsl:for-each>
</td>

输出(部分)为:

<td style="width:1.50in; ">    <!-- last() -->
    <LocationTimestamp xmlns="mbg.com">4/7/2018 4:17:37 PM</LocationTimestamp>
    <LocationTimestamp xmlns="mbg.com">4/7/2018 4:17:37 PM</LocationTimestamp>
</td>
<td style="width:2.13in; ">   <!-- [1] -->
    <LocationTimestamp xmlns="mbg.com">4/6/2018 4:17:37 PM</LocationTimestamp>
    <LocationTimestamp xmlns="mbg.com">4/6/2018 4:17:37 PM</LocationTimestamp>
</td>
...
<td style="width:1.50in; ">    <!-- last() -->
    <LocationTimestamp xmlns="mbg.com">4/7/2018 4:17:37 PM</LocationTimestamp>
</td>
<td style="width:2.13in; ">    <!-- [1] -->
    <LocationTimestamp xmlns="mbg.com">4/6/2018 4:17:37 PM</LocationTimestamp>
</td>