Looping Through XSLT

I have an XML that looks like

<Data>

<MainItem>
<ItemGroup>Foo</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>

<MainItem>
<ItemGroup>Bar</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>

<MainItem>
<ItemGroup>Baz</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>

<OtherData>
<ItemGroup>Foo</ItemGroup>
<OtherDataDetails>Blah</OtherDataDetails>
</OtherData>

<OtherData>
<ItemGroup>Bar</ItemGroup>
<OtherDataDetails>BlahBlah</OtherDataDetails>
</OtherData>

<OtherData>
<ItemGroup>Baz</ItemGroup>
<OtherDataDetails>BlahBlahBlahBlahBlah</OtherDataDetails>
</OtherData>

</Data>

What I'm trying to convert looks like this:

Foo
- Details
- Blah

Bar
- Details
- BlahBlah

Baz
- Details
- BlahBlahBlahBlahBlah

using XSLT 1.0.

I am currently doing a grouping, doing something similar to the Muenchian method ; but I'm not sure how to enter tag data into my group. Any tips?

+3
source share
3 answers

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />

    <xsl:key name="groups" match="//ItemGroup" use="text()" />

    <xsl:template match="/">
        <Data>
            <xsl:apply-templates
                select="//ItemGroup[count( . | key('groups', text())[1]) = 1]" />
        </Data>
    </xsl:template>

    <xsl:template match="ItemGroup">
        <xsl:variable name="text" select="text()" />
        <Group><xsl:value-of select="$text" /></Group>
        <xsl:for-each select="/Data/*[ItemGroup = $text]/*[contains(name(), 'Details')]">
            <Detail>- <xsl:value-of select="." /></Detail>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

I have created a working example for you .

+2
source

The following grouping solution does not use loops and does not care about any other sibling element after ItemGroup. In addition, only a small key based on is used to identify groups MainItem.


XSLT 1.0 Saxon 6.5.5

:

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

    <xsl:key name="main" match="MainItem/ItemGroup" use="text()"/>

    <xsl:template match="/Data">
        <xsl:apply-templates select="MainItem"/>
    </xsl:template>

    <xsl:template match="MainItem">
        <xsl:value-of select="ItemGroup"/><xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates select="ItemGroup[generate-id(.)=generate-id(key('main', current()/ItemGroup)[1])]"/>
    </xsl:template>

    <xsl:template match="ItemGroup">
        <xsl:apply-templates select="/Data/*[ItemGroup = current()]/*/following-sibling::*"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="*">
        <xsl:text>- </xsl:text><xsl:value-of select="."/><xsl:text>&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

, :

 Foo
- Details
- Blah

Bar
- Details
- BlahBlah

Baz
- Details
- BlahBlahBlahBlahBlah

XML-:

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

    <xsl:key name="main" match="MainItem/ItemGroup" use="text()"/>

    <xsl:template match="/Data">
        <xsl:copy>
            <xsl:apply-templates select="MainItem"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="MainItem">
        <xsl:variable name="id" select="ItemGroup"/>
        <Group id="{$id}">
            <xsl:apply-templates select="ItemGroup[generate-id(.)=generate-id(key('main', current()/ItemGroup)[1])]"/>
        </Group>
    </xsl:template>

    <xsl:template match="ItemGroup">
        <xsl:copy-of select="/Data/*[ItemGroup = current()]/*/following-sibling::*"/>
    </xsl:template>

</xsl:stylesheet>

:

<Data>
   <Group id="Foo">
      <ItemDetails>Details</ItemDetails>
      <OtherDataDetails>Blah</OtherDataDetails>
   </Group>
   <Group id="Bar">
      <ItemDetails>Details</ItemDetails>
      <OtherDataDetails>BlahBlah</OtherDataDetails>
   </Group>
   <Group id="Baz">
      <ItemDetails>Details</ItemDetails>
      <OtherDataDetails>BlahBlahBlahBlahBlah</OtherDataDetails>
   </Group>
</Data>
+2

, :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:key name="kGroupByVal" match="ItemGroup"
  use="."/>
 <xsl:key name="kNonGroupByGroup"
  match="*[not(self::ItemGroup)]" use="../ItemGroup"/>

 <xsl:template match=
  "ItemGroup[generate-id()
            =
             generate-id(key('kGroupByVal',.)[1])
            ]
  ">
 <xsl:value-of select="concat('&#xA;',.)"/>

 <xsl:apply-templates mode="listGroup"
  select="key('kNonGroupByGroup',.)"/>
 </xsl:template>

 <xsl:template match="*" mode="listGroup">
  <xsl:value-of select="concat('&#xA; - ', .)"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

XML-:

<Data>
    <MainItem>
        <ItemGroup>Foo</ItemGroup>
        <ItemDetails>Details</ItemDetails>
    </MainItem>
    <MainItem>
        <ItemGroup>Bar</ItemGroup>
        <ItemDetails>Details</ItemDetails>
    </MainItem>
    <MainItem>
        <ItemGroup>Baz</ItemGroup>
        <ItemDetails>Details</ItemDetails>
    </MainItem>
    <OtherData>
        <ItemGroup>Foo</ItemGroup>
        <OtherDataDetails>Blah</OtherDataDetails>
    </OtherData>
    <OtherData>
        <ItemGroup>Bar</ItemGroup>
        <OtherDataDetails>BlahBlah</OtherDataDetails>
    </OtherData>
    <OtherData>
        <ItemGroup>Baz</ItemGroup>
        <OtherDataDetails>BlahBlahBlahBlahBlah</OtherDataDetails>
    </OtherData>
</Data>

the desired, correct result is output:

Foo
 - Details
 - Blah
Bar
 - Details
 - BlahBlah
Baz
 - Details
 - BlahBlahBlahBlahBlah

** Explanation **:

+1
source

All Articles