2016-07-18 14 views
0

重複を無視する必要があるXMLがあります。私の例では、FaciltyIdが重複している場合は、一致するすべてのレコードを無視しています。出力は、STORE-2およびSTORE-3用に生成する必要があります。私は次の例を持っていますが、それは私が望ましくない重複ノードの最後の発生を取っています。誰かが私を案内してくれますか?ありがとうございました。XSLTグループ化と重複の削除

<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> 
+0

? 2.0には1.0よりも優れたツールがあります。 (XSLT FAQウェブサイトで多くの有益な例を確認するための標準的な提案、http://www.dpawson.co.uk/xsl – keshlam

+0

1.0に固執する必要があります。 – GSR

答えて

0

FaciltyIdが重複しているならば、私はすべての一致するレコードを無視しています。 出力は、STORE-2およびSTORE-3に対してのみ生成する必要があります。

私はこれを正しく理解していれば、あなたは何をしたい:

XSLTのバージョン

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> 
+0

ありがとうございました。私の質問に含まれていないことの1つは、重複をチェックする前に出力にコピーする必要があるメッセージヘッダーブロックがあることです。入力ファイルのようなものです。 – GSR

+0

''を2番目のテンプレートに追加するだけでよいでしょう。 –