Group-adjacent nodes using xslt



Questions concerning the group-adjacent function have already been asked, I know. For some reason though, I can't get this to work.


I inherited project at work that uses xslt and my boss doesn't appreciate how steep the learning curve is for learning xslt and how dissimilar it is from procedural programming.


I'm nonetheless determined to learn it as well as I can, but I still have to work with it while I'm learning. Any help in the solving the below problem would be enormously appreciated.


I have an xml document that's formatted as such:



<document>
<section/>
<TOCChapter>
<TOCAuthor>
<TOCChapter>
<TOCAuthor>
<TOCChapter>
<TOCAuthor>
<TOCChapter>
<TOCAuthor>
<section/>
</document>


I'm trying to group all TOC elements and nest them within a containing element:



<document>
<section/>
<wrapper>
<TOCChap>
<TOCAuth>
<TOCChap>
<TOCAuth>
<TOCChap>
<TOCAuth>
<TOCChap>
<TOCAuth>
</wrapper>
<section/>
</document>


This is proving more difficult than I had anticipated. I've tried many different versions of the below code, and I've pasted in the one that seems the least wrong. I realize it's still pretty bad:



<xsl:template match="*">
<xsl:for-each-group select="TOCChap | TOCAuth" group-adjacent="self::TOCChap | self::TOCAuth">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<wrapper>
<xsl:apply-templates select="current-group()"/>

</wrapper>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>


Thank you in advance.


No comments:

Post a Comment