简体   繁体   中英

Stuck on grouping in XSLT 2.0

I have an input file like this

<items>
    <item>
        <id>5</id>
        <name>Harry</name>
        <age>18</age>
        <color>blue</color>
    </item>
    <item>
        <id>5</id>
        <name>Harry</name>
        <age>18</age>
        <fav_food>pizza</fav_food>
    </item>
    <item>
        <id>5</id>
        <name>Harry</name>
        <age>18</age>
        <is_single>true</is_single>
    </item>
</items>

I'd like to group up the elements so that the file looks like this

<items>
<item>
    <id>5</id>
    <name>Harry</name>
    <age>18</age>
    <color>blue</color>
    <fav_food>pizza</fav_food>
    <is_single>true</is_single>
</item>
</items>

EDIT - Made an XSLT transform following this link ( XSLT Consolidating data when ID is the same ) but this just doesn't print anything.

Here is my transform (using XSLT 2.0) -

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="item">
    <xsl:apply-templates select="@*" />
    <xsl:for-each-group select="item" group-by="id">
    <item>
        <id><xsl:value-of select="current-grouping-key()"/></id>
        <xsl:apply-templates select="current-group()" />
    </item>
    </xsl:for-each-group>
</xsl:template>

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

<xsl:template match="*">
    <xsl:element name="{local-name()}"> 
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

The main problem you have is you have a template matching the item element:

<xsl:template match="item">

But within this, you have an xsl:for-each-group that also looks for item elements

<xsl:for-each-group select="item" group-by="id">

What this means is that you are looking for item elements that are children of other item elements, of which there are none in the XML. I think you need to combine the first two templates into one here:

<xsl:template match="items">
    <xsl:for-each-group select="item" group-by="id">
        <item>
           ....

One thing that is not clear is what elements within item elements will be repeated. Will it always be the id, name and age? It's probably best to be flexible here though, and assume only id will be the common element. I will also assume if two elements are repeated (like name ) it will always have the same value.

So, to get all the other distinct non-id elements, you could some more grouping on element name

<xsl:for-each-group select="current-group()/*[not(self::id)]" group-by="local-name()">

Then, within this loop you can just output the element.

Try this XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

<xsl:template match="items">
  <xsl:for-each-group select="item" group-by="id">
    <item>
      <id><xsl:value-of select="current-grouping-key()"/></id>
      <xsl:for-each-group select="current-group()/*[not(self::id)]" group-by="local-name()">
         <xsl:apply-templates select="self::*" />
      </xsl:for-each-group>
    </item>
  </xsl:for-each-group>
</xsl:template>

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

<xsl:template match="*">
    <xsl:element name="{local-name()}"> 
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

When applied to your XML, the following is output

<item>
   <id>5</id>
   <name>Harry</name>
   <age>18</age>
   <color>blue</color>
   <fav_food>pizza</fav_food>
   <is_single>true</is_single>
</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM