I'm just starting out with XSLT, so bear with me. I am processing a fairly complex document with a structure similar to the one below. It is divided into two sections, data and meta. For each data/item I need to look up the "actual" class in the corresponding meta/item.
<root>
<data>
<item id="i1">
<h3 id="p2">akkakk</h3>
<p id="p3">osijaoid</p>
<p id="p4">jqidqjwd</p>
<item>
</data>
<meta>
<item ref="i1">
<p ref="p2" class="heading"/>
<p ref="p3" class="heading"/>
<p ref="p4" class="body"/>
</item>
</meta>
</root>
I need to group adjacent p elements in meta on their class attribute. I thought a helper function could make this a bit cleaner:
<xsl:function name="fn:node-groups" as="node()*">
<xsl:param name="parent"/>
<xsl:variable name="nodeRefs" select="root($parent)//meta/item[@ref = $parent/@id]/p"/>
<xsl:for-each-group select="$nodeRefs" group-adjacent="@class">
<group class="{@class}">
<xsl:for-each select="current-group()">
<node ref="{@ref}"/>
</xsl:for-each>
</group>
</xsl:for-each-group>
</xsl:function>
And then use it when processing the data nodes, like so. My problem is that I can not select further from the nodeset returned by the function.
<xsl:template match="//data/item">
<!-- works -->
<xsl:variable name="test1" select="fn:node-groups(.)"/>
<!-- works -->
<xsl:variable name="test2" select="fn:node-groups(.)/*"/>
<!-- does not work -->
<xsl:variable name="test3" select="fn:node-groups(.)/group[@class = 'heading']"/>
</xsl:template>
I can write a star as in test2, and that gives me all node nodes. But anything else just gives me an empty nodeset. Or at least it looks that way, but at this point I really don't know anymore.