【问题标题】:Using for-each-group with having a condition in group-by使用 for-each-group 并在 group-by 中具有条件
【发布时间】:2018-09-06 02:08:04
【问题描述】:

我想通过id 使用for-each-group 对项目进行分组,并且仅在它们位于不同来源时进行分组,如果来源相同,即使它具有相同的ID也不分组

<items>
    <item id="123" source="loc1">
        <price>12</price>
        <description>test</description> 
    </item>

    <item id="123" source="loc2">
        <price>122</price>
        <description>test</description> 
    </item>

    <item id="234" source="loc1">
        <price>566</price>
        <description>test</description> 
    </item>

    <item id="456" source="loc2">
        <price>222</price>
        <description>desc</description> 
    </item>

    <item id="456" source="loc2">
        <price>312</price>
        <description>desc</description> 
    </item>

    <item id="768" source="loc1">
        <price>212</price>
        <description>desc</description> 
    </item>

    <item id="768" source="loc2">
        <price>934</price>
        <description>desc</description> 
    </item>
  </items>

然后输出这样的东西

<items>
    <group>
         <item id="123" source="loc1">
            <price>12</price>
            <description>test</description> 
         </item>

         <item id="123" source="loc2">
            <price>122</price>
            <description>test</description> 
         </item>
    </group>

    <group>
        <item id="234" source="loc1">
           <price>566</price>
           <description>test</description> 
        </item>
    </group>

    <group>
        <item id="456" source="loc2">
           <price>222</price>
           <description>desc</description> 
        </item>
    </group>

     <group>
        <item id="456" source="loc2">
           <price>222</price>
           <description>desc</description> 
        </item>
    </group>

    <group>
       <item id="768" source="loc1">
          <price>212</price>
          <description>desc</description> 
       </item>

       <item id="768" source="loc2">
           <price>934</price>
           <description>desc</description> 
       </item>
    </group>
</items>

更新

按 id 分组

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

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

  <xsl:template match="items">
    <xsl:copy>
        <xsl:for-each-group select="item" group-by="@id">
            <group>
                <xsl:apply-templates select="current-group()"/>
            </group>
        </xsl:for-each-group>

    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

【问题讨论】:

    标签: xslt-2.0 xslt-grouping


    【解决方案1】:

    我认为您的要求可以通过使用group-by="@id" 来实现,然后在内部使用检查count(distinct-values(current-group()/@source)) = count(current-group()) 来决定是否将current-group() 中的所有项目包装到单个group 元素包装器中,或者是否包装每个项目自己的:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:output method="xml" indent="yes"/>
    
    
      <xsl:template match="items">
          <xsl:copy>
              <xsl:for-each-group select="item" group-by="@id">
                  <xsl:choose>
                      <xsl:when test="count(distinct-values(current-group()/@source)) = count(current-group())">
                          <group>
                              <xsl:apply-templates select="current-group()"/>
                          </group>
                      </xsl:when>
                      <xsl:otherwise>
                          <xsl:for-each select="current-group()">
                              <group>
                                  <xsl:apply-templates select="."/>
                              </group>
                          </xsl:for-each>
                      </xsl:otherwise>
                  </xsl:choose>
              </xsl:for-each-group>
          </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/pPqsHTR 的在线示例,并不是说我更正了输入示例中的最后两个 item 元素,使其具有 source 属性,而不是它们在您的帖子中的 name

    【讨论】:

    • 感谢您的解决方案,我已经更正了 xml,是否可以在 group-by 中有不同的条件
    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    相关资源
    最近更新 更多