应该过滤掉重复的连接,因此如果Destination和LineShortName的值存在多次,则只应显示第一次出现。
XML数据:
<Stop>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>10</LineName>
<LineShortName>10</LineShortName>
<LineNumber>10</LineNumber>
<Destination>HAUPTBAHNHOF</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T10:52:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>2/12</LineName>
<LineShortName>2/12</LineShortName>
<LineNumber>2</LineNumber>
<Destination>WALLERSHEIM</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T10:47:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>1</LineName>
<LineShortName>1</LineShortName>
<LineNumber>1</LineNumber>
<Destination>GOLDGRUBE</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:16:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>1</LineName>
<LineShortName>1</LineShortName>
<LineNumber>1</LineNumber>
<Destination>ALTSTADT</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T10:44:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>9</LineName>
<LineShortName>9</LineShortName>
<LineNumber>9</LineNumber>
<Destination>KO-ZENTRUM</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:24:00</ScheduledDepartureTime
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>2/12</LineName>
<LineShortName>2/12</LineShortName>
<LineNumber>2</LineNumber>
<Destination>WALLERSHEIM</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:17:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>8</LineName>
<LineShortName>8</LineShortName>
<LineNumber>8</LineNumber>
<Destination>HAUPTBAHNHOF</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:25:00</ScheduledDepartureTime
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>3/13</LineName>
<LineShortName>3/13</LineShortName>
<LineNumber>3</LineNumber>
<Destination>HAUPTBAHNHOF</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:08:00</ScheduledDepartureTime
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>1</LineName>
<LineShortName>1</LineShortName>
<LineNumber>1</LineNumber>
<Destination>ALTSTADT</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:14:00</ScheduledDepartureTime
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>20</LineName>
<LineShortName>20</LineShortName>
<LineNumber>20</LineNumber>
<Destination>HAUPTBAHNHOF</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:24:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>2/12</LineName>
<LineShortName>2/12</LineShortName>
<LineNumber>2</LineNumber>
<Destination>NEUENDORF</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:32:00</ScheduledDepartureTime>
</Connection>
<Connection>
<ConnectionType>ProtectedConnection</ConnectionType>
<LineName>10</LineName>
<LineShortName>10</LineShortName>
<LineNumber>10</LineNumber>
<Destination>HAUPTBAHNHOF</Destination>
<ConnectionState>ConnectionOK</ConnectionState>
<VehicleTypeRef>7</VehicleTypeRef>
<ScheduledDepartureTime>2017-11-07T11:22:00</ScheduledDepartureTime>
</Connection>
</Stop>
预期输出(仅纯文本):
LineShortName: 10 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T10:52:00
LineShortName: 2/12 Destination: WALLERSHEIM ScheduledDepartureTime: 2017-11-07T10:47:00
LineShortName: 1 Destination: GOLDGRUBE ScheduledDepartureTime: 2017-11-07T11:16:00
LineShortName: 1 Destination: ALTSTADT ScheduledDepartureTime: 2017-11-07T10:44:00
LineShortName: 9 Destination: KO-ZENTRUM ScheduledDepartureTime: 2017-11-07T11:24:00
LineShortName: 8 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T11:25:00
LineShortName: 3/13 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T11:08:00
LineShortName: 20 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T11:24:00
LineShortName: 2/12 Destination: NEUENDORF ScheduledDepartureTime: 2017-11-07T11:32:00
模板:
<xsl:key name="unique-connections" match="Connection" use="concat(Destination, '|', LineShortName)"/>
<xsl:template match="Connection[generate-id() = generate-id(key('unique-connections', concat(Destination, '|', LineShortName)))]">
<xsl:copy>
<xsl:text>

</xsl:text>
<xsl:text>LineShortName: </xsl:text><xsl:copy-of select="LineShortName"/><xsl:text> </xsl:text>
<xsl:text>Destination: </xsl:text><xsl:copy-of select="Destination"/><xsl:text> </xsl:text>
<xsl:text>ScheduledDepartureTime: </xsl:text><xsl:copy-of select="ScheduledDepartureTime"/><xsl:text> </xsl:text>
<xsl:text>
</xsl:text><xsl:text>
</xsl:text>
<!--
<xsl:for-each select="key('unique-connections', concat(Destination, '|', LineShortName))">
<xsl:text>VehicleTypeRef: </xsl:text><xsl:value-of select="VehicleTypeRef"/><xsl:text>
</xsl:text>
</xsl:for-each>
-->
</xsl:copy>
</xsl:template>
模板由另一个模板调用,该模板处理停止:
<xsl:for-each select="Connection">
<xsl:apply-templates select="current()"/>
</xsl:for-each>
实际输出:
ProtectedConnection101010HAUPTBAHNHOFConnectionOK72017-11-07T10:52:00
LineShortName: 2/12 Destination: WALLERSHEIM ScheduledDepartureTime: 2017-11-07T10:47:00
ProtectedConnection111GOLDGRUBEConnectionOK72017-11-07T11:16:00
LineShortName: 1 Destination: ALTSTADT ScheduledDepartureTime: 2017-11-07T10:44:00
ProtectedConnection999KO-ZENTRUMConnectionOK72017-11-07T11:24:00ProtectedConnection2/122/122WALLERSHEIMConnectionOK72017-11-07T11:17:00
LineShortName: 8 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T11:25:00
LineShortName: 3/13 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T11:08:00
ProtectedConnection111ALTSTADTConnectionOK72017-11-07T11:14:00
LineShortName: 20 Destination: HAUPTBAHNHOF ScheduledDepartureTime: 2017-11-07T11:24:00
LineShortName: 2/12 Destination: NEUENDORF ScheduledDepartureTime: 2017-11-07T11:32:00
ProtectedConnection101010HAUPTBAHNHOFConnectionOK72017-11-07T11:22:00
我认为模板每次都不匹配,但为什么呢? 对于某些连接集,它可以很好地工作,但每次使用这些特定连接都会失败。
我有一种强烈的感觉,最好只使用连接的父节点调用模板一次,但我无法解决好的解决方案。
答案 0 :(得分:0)
因此,您希望使用label
处理所有Connection
元素,但不为复制品输出任何内容。然后不要处理所有这些,而是使用xsl:apply-templates
或确保你处理所有那些你不想输出的东西:
<xsl:apply-templates select="Connection[generate-id() = generate-id(key('unique-connections', concat(Destination, '|', LineShortName)))]"/>
否则,默认模板会启动并显示任何文本数据。
在线示例http://xsltransform.hikmatu.com/6qM2e26,其中包含重复项输出的空模板
<xsl:template match="Connection[not(generate-id() = generate-id(key('unique-connections', concat(Destination, '|', LineShortName))))]"/>
我手动检查的数据太多,但如果密钥设置正确,我看不出它是如何失败的。