首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XSLT分组&删除重复

XSLT分组&删除重复
EN

Stack Overflow用户
提问于 2016-07-18 21:21:38
回答 1查看 34关注 0票数 0

我有一个XML,需要忽略这些副本。在我给定的示例中,如果FaciltyId是重复的,那么我就忽略了所有匹配的记录。应该只为存储-2和存储-3生成输出。我有下面的例子,但这是我不想看到的重复节点的最后一次出现。有人能帮我指点一下吗?谢谢。

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-18 22:49:37

如果FaciltyId是重复的,那么我就忽略了所有匹配的记录。应该只为存储-2和存储-3生成输出。

如果我正确理解了这一点,你想做的是:

XSLT1.0

代码语言:javascript
运行
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38446201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档