对元素进行分组并删除重复节点 - XSLT 1.0

发布于 2024-10-18 04:49:04 字数 2368 浏览 3 评论 0原文

我查看了 慕尼黑分组 - 组内节点,不在整个文档内,但它对我来说不太有效。仅靠慕尼黑方法也不适合我。

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

我有以下 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>

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

我已经尝试了几个小时,但商店编号总是重复,或者即使 PriceValue 相同,它们也不会聚合。

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.

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

I have the following 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>

The outcome should look like this:

<?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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除会话 2024-10-25 04:49:04

此转换:

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

This transformation:

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

when applied on the provided XML document:

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

produces the wanted, correct result:

<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>
℉服软 2024-10-25 04:49:04

我认为以下解决了问题,至少对于分组而言:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="k1" match="items/Recordset" use="concat(generate-id(..), '|', PriceValue)"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="items">
    <Mi item="{@item}">
      <PriceList>
        <xsl:apply-templates select="Recordset[generate-id() = generate-id(key('k1', concat(generate-id(..), '|', PriceValue))[1])]"/>
      </PriceList>
    </Mi>
  </xsl:template>

  <xsl:template match="Recordset">
    <Prices>
      <Price Value="{PriceValue}"/>
      <PriceConfig>
        <Stores>
          <xsl:apply-templates select="key('k1', concat(generate-id(..), '|', PriceValue))/CodeBusinessUnit"/>
        </Stores>
      </PriceConfig>
    </Prices>
 </xsl:template>

 <xsl:template match="CodeBusinessUnit">
   <xsl:if test="position() > 1">,</xsl:if>
   <xsl:value-of select="."/>
 </xsl:template>

</xsl:stylesheet>

I think the following solves the problem, at least for the grouping:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="k1" match="items/Recordset" use="concat(generate-id(..), '|', PriceValue)"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="items">
    <Mi item="{@item}">
      <PriceList>
        <xsl:apply-templates select="Recordset[generate-id() = generate-id(key('k1', concat(generate-id(..), '|', PriceValue))[1])]"/>
      </PriceList>
    </Mi>
  </xsl:template>

  <xsl:template match="Recordset">
    <Prices>
      <Price Value="{PriceValue}"/>
      <PriceConfig>
        <Stores>
          <xsl:apply-templates select="key('k1', concat(generate-id(..), '|', PriceValue))/CodeBusinessUnit"/>
        </Stores>
      </PriceConfig>
    </Prices>
 </xsl:template>

 <xsl:template match="CodeBusinessUnit">
   <xsl:if test="position() > 1">,</xsl:if>
   <xsl:value-of select="."/>
 </xsl:template>

</xsl:stylesheet>
幽梦紫曦~ 2024-10-25 04:49:04

我还将发布一个样式表,因为每个人都会这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kBUnitByItem-Price"
             match="CodeBusinessUnit"
             use="concat(../../@item, '++', ../PriceValue)"/>
    <xsl:template match="/">
        <MT_MATERIALDATA>
            <xsl:apply-templates/>
        </MT_MATERIALDATA>
    </xsl:template>
    <xsl:template match="items">
        <MI item="{@item}">
            <PriceList>
                <xsl:apply-templates/>
            </PriceList>
        </MI>
    </xsl:template>
    <xsl:template match="CodeBusinessUnit[
                            count(.|key('kBUnitByItem-Price',
                                        concat(../../@item,'++',../PriceValue)
                                    )[1]
                            ) = 1
                         ]">
        <Prices>
            <Price Value="{../PriceValue}"/>
            <PriceConfig>
                <Stores>
                    <xsl:apply-templates
                         select="key('kBUnitByItem-Price',
                                     concat(../../@item,'++',../PriceValue))"
                         mode="sequence"/>
                </Stores>
            </PriceConfig>
        </Prices>
    </xsl:template>
    <xsl:template match="text()"/>
    <xsl:template match="node()" mode="sequence">
        <xsl:if test="position()!=1">,</xsl:if>
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

注意:按商品和价格对商店进行分组。拉式风格比推式风格多一点(那是因为没有重复的@item。)

输出:

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

我认为我们涵盖了所有变体:键值、推拉、序列分隔符条件。

I'm also going to post an stylesheet, because everybody do it:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kBUnitByItem-Price"
             match="CodeBusinessUnit"
             use="concat(../../@item, '++', ../PriceValue)"/>
    <xsl:template match="/">
        <MT_MATERIALDATA>
            <xsl:apply-templates/>
        </MT_MATERIALDATA>
    </xsl:template>
    <xsl:template match="items">
        <MI item="{@item}">
            <PriceList>
                <xsl:apply-templates/>
            </PriceList>
        </MI>
    </xsl:template>
    <xsl:template match="CodeBusinessUnit[
                            count(.|key('kBUnitByItem-Price',
                                        concat(../../@item,'++',../PriceValue)
                                    )[1]
                            ) = 1
                         ]">
        <Prices>
            <Price Value="{../PriceValue}"/>
            <PriceConfig>
                <Stores>
                    <xsl:apply-templates
                         select="key('kBUnitByItem-Price',
                                     concat(../../@item,'++',../PriceValue))"
                         mode="sequence"/>
                </Stores>
            </PriceConfig>
        </Prices>
    </xsl:template>
    <xsl:template match="text()"/>
    <xsl:template match="node()" mode="sequence">
        <xsl:if test="position()!=1">,</xsl:if>
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Note: Grouping stores by item and price. A little more pull than push style (That's because there is no duplicate @item.)

Output:

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

I think we cover all the variations: key value, push-pull, sequence separator condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文