DanielCSD
DanielCSD

Reputation: 73

Grouping in XSLT 2.0 (grouping by text)

I have a problem figuring out this grouping in xslt:

The initial information:

<Application>

  <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8a].UniqueID" Value="bfd0b74d-2888-49d9-a986-df807f08ad8a" />
  <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8a].Filename" Value="Document 1 Test" />
  <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8a].URI" Value="https/.test.pdf" />

  <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8b].UniqueID" Value="bfd0b74d-2888-49d9-a986-df807f08ad8b" />
  <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8b].Filename" Value="Document 2 Test" />
  <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8b].URI" Value="google.com" />

</Application>

The expected result:

<Package>
   <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8a"
      Filename="Document 1 Test"
      URI="https/.test.pdf"/>
   <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8b"
      Filename="Document 2 Test"
      URI="google.com"/>
<Package>

My code: I've done the grouping by using the id from the square brackets.

<xsl:for-each-group select="ApplicationItem[contains(@LayoutPath,'Attachments.Package.Attachment')]" group-by="substring-before(substring-after(@LayoutPath, 'Attachments.Package.Attachment['), ']')">
            <Attachment>
                <xsl:for-each select="current-group()">
                    <xsl:attribute name="UniqueID" select="current-grouping-key()"/>
                    <xsl:attribute name="Filename" select=".[contains(@LayoutPath,'Filename')]/@Value"/>
                    <xsl:attribute name="URI" select=".[contains(@LayoutPath,'URI')]/@Value"/>
                </xsl:for-each>
            <Attachment>
</xsl:for-each-group>

My results:

<Package>
   <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8a"
      Filename=""
      URI="https/.test.pdf"/>
   <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8b"
      Filename=""
      URI="google.com"/>
<Package>

What i need to change in code to use the grouping because for now is not working taking only the last ApplicationItem with the unique @LayoutPath. I think the problem is with the grouping but don't now how to fix it.

Upvotes: 0

Views: 62

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167446

Remove the <xsl:for-each select="current-group()"> and change

                <xsl:attribute name="Filename" select=".[contains(@LayoutPath,'Filename')]/@Value"/>
                <xsl:attribute name="URI" select=".[contains(@LayoutPath,'URI')]/@Value"/>

to

                <xsl:attribute name="Filename" select="current-group()[contains(@LayoutPath,'Filename')]/@Value"/>
                <xsl:attribute name="URI" select="current-group()[contains(@LayoutPath,'URI')]/@Value"/>

Upvotes: 1

Related Questions