且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 xslt 查找具有相同属性对的节点并添加一个新节点

更新时间:2023-11-30 09:18:16

这可以通过一键查找ITEM元素来实现

This can be achieved by means of a key to look up ITEM elements

<xsl:key name="item" match="ITEM" use="concat(@BEGIN, '|', @END)" />

然后,您只需要一个模板来匹配 ITEM 元素,其中 key 中至少有 2 个项目

Then, you just need a template that matches ITEM elements where there is at least 2 items in the key

<xsl:template match="ITEM[key('item', concat(@BEGIN, '|', @END))[2]]">

将此与 XSLT 身份转换结合使用,为您提供此 XSLT

Using this in conjunction with the XSLT identity transform, gives you this XSLT

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

  <xsl:key name="item" match="ITEM" use="concat(@BEGIN, '|', @END)" />

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

  <xsl:template match="ITEM[key('item', concat(@BEGIN, '|', @END))[2]]">
    <MULTIPLE />
    <xsl:call-template name="identity" />
  </xsl:template>
</xsl:stylesheet>

如果您希望限制它在同一产品和子类别中查找匹配项,请将键更改为此...

If you wish to restrict it to look for matches in the same product and sub-category, change the key to this...

<xsl:key name="item" match="ITEM" use="concat(../../@TYPE, '|', ../@CAT, '|', @BEGIN, '|', @END)" />

并相应地调整模板匹配....

And adjust the template match accordingly....

<xsl:template match="ITEM[key('item', concat(../../@TYPE, '|', ../@CAT, '|', @BEGIN, '|', @END))[2]]">