我有一个XML,需要忽略重复项。在我给出的例子中,如果Facility Id重复,那么我将忽略所有匹配的记录。应仅为STORE-2和STORE-3生成输出。我有下面的例子,但它采用了我不想要的重复节点的最后一次出现。有人能指导我吗?非常感谢。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="x" match="FacilityId" use="."/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item">
<xsl:for-each select=".">
<xsl:if test="generate-id(FacilityId) = generate-id(key('x', FacilityId)[1])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<MessageHeader>
<Version>1.0</Version>
</MessageHeader>
<Item>
<FacilityId>STORE-1</FacilityId>
<ItemId>1001</ItemId>
<ItemsType>FS</ItemsType>
<InventoryDateTime>2016-07-05T07:00:05-07:00</InventoryDateTime>
</Item>
<Item>
<FacilityId>STORE-1</FacilityId>
<ItemId>1002</ItemId>
<ItemsType>FS</ItemsType>
<InventoryDateTime>2016-07-05T07:00:05-07:00</InventoryDateTime>
</Item>
<Item>
<FacilityId>STORE-2</FacilityId>
<ItemId>1003</ItemId>
<ItemsType>FS</ItemsType>
<InventoryDateTime>2016-07-05T07:00:05-07:00</InventoryDateTime>
</Item>
<Item>
<FacilityId>STORE-3</FacilityId>
<ItemId>1004</ItemId>
<ItemsType>FS</ItemsType>
<InventoryDateTime>2016-07-05T07:00:05-07:00</InventoryDateTime>
</Item>
</Items>
如果Facility Id重复,那么我将忽略所有匹配的记录。应仅为STORE-2和STORE-3生成输出。
如果我理解正确,你想做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="item-by-facility" match="Item" use="FacilityId" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Items">
<xsl:copy>
<xsl:apply-templates select="Item[count(key('item-by-facility', FacilityId)) = 1]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>