为什么我的 XSLT 1.0 模板不是每次都调用



应过滤掉重复的连接,因此如果Target和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>&#xa;&#xa;</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>&#xa;</xsl:text><xsl:text>&#xa;</xsl:text>
        <!--
        <xsl:for-each select="key('unique-connections', concat(Destination, '|', LineShortName))">
            <xsl:text>VehicleTypeRef: </xsl:text><xsl:value-of select="VehicleTypeRef"/><xsl:text>&#xa;</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

我假设模板不是每次都匹配,但为什么呢?对于某些连接集,它可以完美运行,但每次使用这些特定连接都会失败。

我有一种强烈的感觉,最好只用连接的父节点调用一次模板,但我无法围绕一个好的解决方案。

因此,您希望使用xsl:apply-templates处理所有Connection元素,但不为重复项输出任何内容。然后不要处理所有这些,而是使用<xsl:apply-templates select="Connection[generate-id() = generate-id(key('unique-connections', concat(Destination, '|', LineShortName)))]"/>或确保你处理了所有你不想输出任何东西的:

<xsl:template match="Connection[not(generate-id() = generate-id(key('unique-connections', concat(Destination, '|', LineShortName))))]"/>

否则,默认模板会启动并喷出任何文本数据。

使用空模板进行重复输出的在线样本 http://xsltransform.hikmatu.com/6qM2e26

<Stop>
    <Connection>
LineShortName: <LineShortName>10</LineShortName>  Destination: <Destination>HAUPTBAHNHOF</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T10:52:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>2/12</LineShortName>  Destination: <Destination>WALLERSHEIM</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T10:47:00</ScheduledDepartureTime>  
</Connection>
        <Connection>
LineShortName: <LineShortName>1</LineShortName>  Destination: <Destination>GOLDGRUBE</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:16:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>1</LineShortName>  Destination: <Destination>ALTSTADT</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T10:44:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>9</LineShortName>  Destination: <Destination>KO-ZENTRUM</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:24:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>8</LineShortName>  Destination: <Destination>HAUPTBAHNHOF</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:25:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>3/13</LineShortName>  Destination: <Destination>HAUPTBAHNHOF</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:08:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>20</LineShortName>  Destination: <Destination>HAUPTBAHNHOF</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:24:00</ScheduledDepartureTime>  
</Connection>
    <Connection>
LineShortName: <LineShortName>2/12</LineShortName>  Destination: <Destination>NEUENDORF</Destination>  ScheduledDepartureTime: <ScheduledDepartureTime>2017-11-07T11:32:00</ScheduledDepartureTime>  
</Connection>
</Stop>

我手动检查的数据太多,但如果密钥设置正确,我看不出它是如何失败的。

相关内容

  • 没有找到相关文章

最新更新