分组元素和删除重复节点 - XSLT 1.0 [英] Grouping elements and deleting duplicate nodes - XSLT 1.0

查看:49
本文介绍了分组元素和删除重复节点 - XSLT 1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过慕尼黑分组 -在一个节点内分组,而不是在整个文档中,但它对我来说不太适用.单独的 Muenchian 方法也不适合我.

I have looked at Muenchian Grouping - group within a node, not within the entire document but it is not quite working for me. The Muenchian method alone does not do it either for me.

我还查看了 XSLT 1.0:分组和删除重复项 但不能完全遵循.

I have also looked at XSLT 1.0: grouping and removing duplicate but cannot follow it completely.

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<MT_MATERIALDATA>
<items item="475053">
    <Recordset>
        <CodeBusinessUnit>99</CodeBusinessUnit>
        <PriceValue>250</PriceValue>
    </Recordset>
    <Recordset>
        <CodeBusinessUnit>1</CodeBusinessUnit>
        <PriceValue>250</PriceValue>
    </Recordset>
</items>
<items item="475054">
    <Recordset>
        <CodeBusinessUnit>1</CodeBusinessUnit>
        <PriceValue>255.34</PriceValue>
    </Recordset>
    <Recordset>
        <CodeBusinessUnit>10</CodeBusinessUnit>
        <PriceValue>299</PriceValue>
    </Recordset>
</items>
</MT_MATERIALDATA>

结果应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<MT_MATERIALDATA>
<Mi item="475053">
    <PriceList>
        <Prices>
            <Price Value="250"/>
            <PriceConfig>
                <Stores>99,1</Stores>
            </PriceConfig>
        </Prices>
    </PriceList>
</Mi>
<Mi item="475054">
    <PriceList>
        <Prices>
            <Price Value="255.34"/>
            <PriceConfig>
                <Stores>1</Stores>
            </PriceConfig>
        </Prices>
        <Prices>
            <Price Value="299"/>
            <PriceConfig>
                <Stores>10</Stores>
            </PriceConfig>
        </Prices>
    </PriceList>
</Mi>
</MT_MATERIALDATA>

所以对于匹配 元素在 中,所有相应的 都需要在 中列出.如果没有,则需要创建一个额外的 节点.

So for matching <PriceValue> elements in <Recordset>, all respective <CodeBusinessUnits> need to be listed in <Stores>. If not, an extra <Prices> node needs to be created.

我已经尝试了几个小时,但商店编号总是重复,或者即使 PriceValue 相同,它们也没有汇总.我感谢任何帮助,我不知道该怎么办了.感谢您的帮助!

I have been trying for hours but either the Store-numbers are always duplicate or they are not aggregated even if the PriceValue is the same. I appreciate any help, I don't know what to do anymore. Thank you for your help!

最好的问候,彼得

推荐答案

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kPriceByValAndItem" match="PriceValue"
  use="concat(../../@item, '|', .)"/>

 <xsl:template match="/*">
  <MT_MATERIALDATA>
   <xsl:apply-templates/>
  </MT_MATERIALDATA>
 </xsl:template>

 <xsl:template match="items">
  <MI item="{@item}">
   <PriceList>
     <xsl:for-each select=
      "*/PriceValue
          [generate-id()
          =
           generate-id(key('kPriceByValAndItem',
                           concat(../../@item, '|', .)
                           )[1]
                       )
           ]
      ">
       <Prices>
        <Price Value="{.}"/>
        <PriceConfig>
          <Stores>
            <xsl:for-each select=
            "key('kPriceByValAndItem',
                           concat(../../@item, '|', .)
                           )">
             <xsl:value-of select="../CodeBusinessUnit"/>
             <xsl:if test="not(position()=last())">,</xsl:if>
            </xsl:for-each>
          </Stores>
        </PriceConfig>
       </Prices>
     </xsl:for-each>
   </PriceList>
  </MI>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<MT_MATERIALDATA>
    <items item="475053">
        <Recordset>
            <CodeBusinessUnit>99</CodeBusinessUnit>
            <PriceValue>250</PriceValue>
        </Recordset>
        <Recordset>
            <CodeBusinessUnit>1</CodeBusinessUnit>
            <PriceValue>250</PriceValue>
        </Recordset>
    </items>
    <items item="475054">
        <Recordset>
            <CodeBusinessUnit>1</CodeBusinessUnit>
            <PriceValue>255.34</PriceValue>
        </Recordset>
        <Recordset>
            <CodeBusinessUnit>10</CodeBusinessUnit>
            <PriceValue>299</PriceValue>
        </Recordset>
    </items>
</MT_MATERIALDATA>

产生想要的、正确的结果:

<MT_MATERIALDATA>
    <MI item="475053">
        <PriceList>
            <Prices>
                <Price Value="250"/>
                <PriceConfig>
                    <Stores>99,1</Stores>
                </PriceConfig>
            </Prices>
        </PriceList>
    </MI>
    <MI item="475054">
        <PriceList>
            <Prices>
                <Price Value="255.34"/>
                <PriceConfig>
                    <Stores>1</Stores>
                </PriceConfig>
            </Prices>
            <Prices>
                <Price Value="299"/>
                <PriceConfig>
                    <Stores>10</Stores>
                </PriceConfig>
            </Prices>
        </PriceList>
    </MI>
</MT_MATERIALDATA>

这篇关于分组元素和删除重复节点 - XSLT 1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆