Xml XSLT1.0:按属性分组和连接属性值

Xml XSLT1.0:按属性分组和连接属性值,xml,xslt,xslt-1.0,muenchian-grouping,Xml,Xslt,Xslt 1.0,Muenchian Grouping,我有以下简化的XML数据,我想按类别ID对其进行分组,看看有效负载中是否有任何类似的记录,如果有,我想将条件与找到的元素结合起来 <Items> <result> <id>11</id> <name>ABC</name> <condition>new</condition> </result> <result>

我有以下简化的XML数据,我想按类别ID对其进行分组,看看有效负载中是否有任何类似的记录,如果有,我想将条件与找到的元素结合起来

<Items>
   <result>
      <id>11</id>
      <name>ABC</name>
      <condition>new</condition>
   </result>
   <result>
      <id>22</id>
      <name>XYZ</name>
      <condition>new</condition>
   </result>
   <result>
      <id>11</id>
      <name>ABC</name>
      <condition>used</condition>
   </result>
   <result>
      <id>33</id>
      <name>PQR</name>
      <condition>used</condition>
   </result>
</Items>

11
基础知识
新的
22
XYZ
新的
11
基础知识
习惯于
33
PQR
习惯于
分组后的预期结果:

<Items>
   <result>
      <id>11</id>
      <name>ABC</name>
      <condition>new,used</condition>
   </result>
   <result>
      <id>22</id>
      <name>XYZ</name>
      <condition>new</condition>
   </result>
   <result>
      <id>11</id>
      <name>ABC</name>
      <condition>new,used</condition>
   </result>
   <result>
      <id>33</id>
      <name>PQR</name>
      <condition>used</condition>
   </result>
</Items>

11
基础知识
新的,用过的
22
XYZ
新的
11
基础知识
新的,用过的
33
PQR
习惯于
在XSLT1.0中,对于存在多个类似记录的大负载,如何做到这一点?使用分组法是否可行

当前逻辑:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sdml="http://selfhtml.org/sdml">
 <xsl:template match="/Items">
    <xsl:copy>
        <xsl:for-each-group select="result" group-by="id"> 
            <records type="{current-grouping-key()}" >
                <xsl:apply-templates select="current-group()" />
            </records>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template> 

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

目前的答复:

"@type": "11",
"$": "11ABC<itemdescription>.new11ABC<itemdescription>.used"
“@type”:“11”,
“$”:“11ABC.new11ABC.used”
编辑1:添加了响应

Edit2:键入编辑

,因为您只想合并条件数据,但保留单独的
结果
元素,并且只想为您可以使用的特定id标识组

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

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

  <xsl:key name="result-group" match="result[id = 11]/condition" use="../id"/>

  <xsl:template match="result[id = 11]/condition">
      <xsl:copy>
          <xsl:for-each select="key('result-group', ../id)">
              <xsl:if test="position() > 1">,</xsl:if>
              <xsl:value-of select="."/>
          </xsl:for-each>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

,


在XSLT 2或3中,您可以在键和模板匹配模式中使用一个变量:

,因为您只希望合并条件数据,但保留单独的
结果
元素,并且只希望为可以使用的特定id标识组

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

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

  <xsl:key name="result-group" match="result[id = 11]/condition" use="../id"/>

  <xsl:template match="result[id = 11]/condition">
      <xsl:copy>
          <xsl:for-each select="key('result-group', ../id)">
              <xsl:if test="position() > 1">,</xsl:if>
              <xsl:value-of select="."/>
          </xsl:for-each>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

,


在XSLT 2或3中,您可以在键和模板匹配模式中使用一个变量:

因此您已经成功地将标签用于XSLT 1.0的分组技术Muenchian分组,但您不能将任何类似的教程应用于您自己的数据?我做了关于Muenchian分组的研究,并回答了其他类似的问题,我发现这可以通过使用Muenchian分组来实现,但是我无法成功地实现它。因此,我张贴一些帮助。谢谢你的推荐。:)那么,如果您尝试使用Muechian分组,您尝试定义了哪个键?您的XSLT1尝试看起来如何?你得到了哪一个错误的结果?谢谢你的回信。我得到了下面的输出,我已经把它编辑得更小了<代码>“@type:”11“,“$”:“11ABC.new66711ABC.used824”它将项目分组,但将所有内容都作为一个块。我无法理解您的示例数据,输入有两个
结果
元素,其中包含
22
,想要的输出只有一个,没有对输入的条件进行任何分组或合并。结果中的
33
从何而来?因此,您已经成功地将标签用于XSLT 1.0的分组技术Muenchian分组,但您无法将任何类似的教程应用于您自己的数据?我对Muenchian分组进行了研究,并遵循了其他类似的问题,我发现这可以使用Muenchian分组来完成,然而,我无法成功地实现它。因此,我张贴一些帮助。谢谢你的推荐。:)那么,如果您尝试使用Muechian分组,您尝试定义了哪个键?您的XSLT1尝试看起来如何?你得到了哪一个错误的结果?谢谢你的回信。我得到了下面的输出,我已经把它编辑得更小了<代码>“@type:”11“,“$”:“11ABC.new66711ABC.used824”
它将项目分组,但将所有内容都作为一个块。我无法理解您的示例数据,输入有两个
结果
元素,其中包含
22
,想要的输出只有一个,没有对输入的条件进行任何分组或合并。结果中的
33
是从哪里来的?谢谢你的时间@Martin,非常感谢。我尝试了您的解决方案,但它仍然没有添加新的、已使用的
。“Items:{”result:[{”id“:11,“name:“ABC”,“description:”,“condition:[“new”]}我已经更改了
match=“result[id='11']/条件”
获取结果/id中的任何id。非常感谢。我终于能够将其应用到我的用例中,并且它工作得很好。也感谢您的指导。感谢您。感谢您的时间@Martin,非常感谢。我尝试了您的解决方案,但它仍然没有附加
新的、使用过的
项目:{“结果”:[{“id”:11,“name”:“ABC”,“description”:“,“condition”:[“new”]}我已经更改了
match=“result[id='11']/条件”
以获取结果中的任何id/id。非常感谢。我终于能够将其应用到我的用例中,并且工作得非常出色。也感谢您的指导。非常感谢。