I have an XML document containing items, each having a tokenized ID string.
<?xml version="1.0"?>
<Test>
<Items>
<Item>
<ID>1_A_3</ID>
<Name>foo</Name>
</Item>
<Item>
<ID>1_B_5</ID>
<Name>bar</Name>
</Item>
<Item>
<ID>1_B_7</ID>
<Name>baz</Name>
</Item>
</Items>
</Test>
I need to transform this into another XML document that groups the items according to the middle part of their ID string (the letter in the above example).
<?xml version='1.0' ?>
<GroupedItems>
<Group id="A">
<Item>foo</Item>
</Group>
<Group id="B">
<Item>bar</Item>
<Item>baz</Item>
</Group>
</GroupedItems>
I am finding the groups using the keys functionality:
<xsl:key name="uniqueGroupIDs"
match="Test/Items/Item"
use="substring-before(substring-after(ID,'_'),'_')"/>
<xsl:for-each
select="Test/Items/Item[generate-id() =
generate-id(key('uniqueGroupIDs',
substring-before(substring-after(ID,'_'),'_')))]">
Notice the code duplication of the substring calls in both places. I already have a template that does the same thing:
<xsl:template name="ExtractGroupID">
<xsl:param name="idString"/>
<xsl:value-of
select="substring-before(substring-after($idString, '_'),'_')"/>
</xsl:template>
Is there any way to use that template within the key statements, to avoid the duplication of code?
In XSLT 2.0 I would just define a function to do it, but I'm stuck with XSLT 1.0 due to a tools limitation beyond my control.