Looping Through XSLT
I've got some XML that appears like this
<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 transform is something similar to this:
Foo
- Details
- Blah
Bar
- Details
- BlahBlah
Baz
- Details
- BlahBlahBlahBlahBlah
using XSLT 1.0.
I'm currently accomplishing the grouping by doing something similar to the Muenchian method; but I'm no开发者_StackOverflowt sure how to bring in the data from the tags into my grouping. Any tips?
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 set up a working example for you.
The following grouping solution does not use loops and take care of any other sibling element following the ItemGroup
. Moreover only a small key based on MainItem
is used to identify the groups.
XSLT 1.0 under Saxon 6.5.5
Producing text:
<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>
</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>
</xsl:text>
</xsl:template>
<xsl:template match="*">
<xsl:text>- </xsl:text><xsl:value-of select="."/><xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Applied on your input, produces:
Foo
- Details
- Blah
Bar
- Details
- BlahBlah
Baz
- Details
- BlahBlahBlahBlahBlah
Producing XML output:
<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>
produces:
<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>
Here is a very short and most efficient transformation that uses only templates and keys:
<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('
',.)"/>
<xsl:apply-templates mode="listGroup"
select="key('kNonGroupByGroup',.)"/>
</xsl:template>
<xsl:template match="*" mode="listGroup">
<xsl:value-of select="concat('
 - ', .)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When applied on the provided XML document:
<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 wanted, correct result is produced:
Foo
- Details
- Blah
Bar
- Details
- BlahBlah
Baz
- Details
- BlahBlahBlahBlahBlah
** Explanation**:
Muenchian grouping to get the distinct values of
ItemGroup
.Key used to index all non-
ItemGroup
elements by theirItemGroup
sibling.Empty template matching any text node to prevent the built-in XSLT template to output any text node.
精彩评论