将for-each-group与分组条件一起使用

时间:2018-09-06 02:08:05

标签: xslt-2.0 xslt-grouping

我想使用idfor-each-group对项目进行分组,并且如果它们位于不同的源中,则仅对其进行分组,即使源具有相同的ID,即使其具有相同的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>

1 个答案:

答案 0 :(得分: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属性中帖子。

相关问题