我需要转换这个输入 XML:
<Log1>
<Connection User="Peter" Host="Computer01" Port="22"/>
<Connection User="Peter" Host="Computer02" Port="22"/>
<Connection User="Peter" Host="Computer02" Port="80"/>
<Connection User="David" Host="Computer01" Port="8080"/>
<Connection User="David" Host="Computer01" Port="8080"/>
<Connection User="David" Host="Computer01" Port="8080"/>
<Connection User="David" Host="Computer03" Port="22"/>
<Connection User="David" Host="Computer04" Port="21"/>
</Log1>
进入该输出 XML:
<Log2>
<Event Name="David" Target="Computer01|Computer03|Computer04"/>
<Event Name="Peter" Target="Computer01|Computer02"/>
</Log2>
到目前为止,我使用了 Muenchian 分组并获得了类似的结果:
<Log2>
<Event Name="David" Target="Computer01|Computer01|Computer01|Computer03|Computer04"/>
<Event Name="Peter" Target="Computer01|Computer02|computer02"/>
</Log2>
问题是我无法摆脱输出中的重复机器。
这是我的 XSLT:
<xsl:key name="myKey" match="Connection" use="@User"/>
<xsl:variable name="separator" select="'|'"/>
<xsl:template match="Log1">
<xsl:element name="Log2">
<xsl:for-each select="Connection[count(. | key('myKey', @User)[1]) = 1]">
<xsl:sort select="@User" />
<xsl:element name="Event">
<xsl:attribute name="Name">
<xsl:value-of select="@User" />
</xsl:attribute>
<xsl:attribute name="Target">
<xsl:for-each select="key('myKey', @User)">
<xsl:sort select="@Host" />
<xsl:value-of select="@Host" />
<xsl:if test="position() != last()">
<xsl:value-of select="$separator" />
</xsl:if>
</xsl:for-each>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
你们能帮我使用 XSLT 1.0 处理器吗?