[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Finding Only Initial Following Siblings That Meet
Grouping should liberate you from looking ahead or behind. So instead of
matching the first <ph outputclass="x">, you'd match <p> (or more
generally '*[ph[@outputclass]]') and do the group-adjacent grouping for
the child nodes, like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:template match="*[ph[@outputclass]]"> <xsl:copy> <xsl:apply-templates select="@*" mode="#current"/> <xsl:for-each-group select="node()" group-adjacent="string(self::ph/@outputclass)"> <xsl:choose> <xsl:when test="current-grouping-key()"> <xsl:element name="{current-grouping-key()}"> <xsl:value-of select="current-group()" separator=""/> </xsl:element> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="current-group()" mode="#current"/> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:copy> </xsl:template> <xsl:mode on-no-match="shallow-copy"/> </xsl:stylesheet> This is not shorter in terms of lines of code than what you suggested. In terms of performance, it could be a bit more efficient than your solution, depending on the cost of identifying the first ph[@output-class] and its following siblings, compared to the cost of identifying a parent of ph[@output-class] and selecting its children. But as I wanted to say above, in terms of idiomatic XSLT 2+ purity, I'd always prefer a solution that doesn't look along the preceding/following axes, even when it is done just once for selecting the for-each-group population. Gerrit On 05.02.2020 23:29, Eliot Kimber ekimber@xxxxxxxxxxxx wrote: In my XML I can have adjacent elements that should be processed as a unit, where the adjacent elements all have the same value for a given attribute. Other elements with the same attribute could be following siblings but separated by other elements or text nodes, i.e.:
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|