On 16/10/2026 23:11, rick@xxxxxxxxxxxxxx wrote:
>
> I have this XML:
>
> <root>
>
> B B B <x>not a child of p</x>
>
> B B B <p>1</p>
>
> B B B <p>2</p>
>
> B B B <p>3</p>
>
> B B B <x>child of p</x>
>
> B B B <p>4</p>
>
> </root>
>
> If an <x> element immediately follows a <p>, I want to make it a child
> of the <p>:
>
> <root>
> B B <x>not a child of p</x>
> B B <p>1</p>
> B B <p>2</p>
> B B <p>
> B B B B B <x>child of p</x>3</p>
> B B B <p>4</p>
> </root>
>
I would be tempted to tackle it with for-each-group group-starting-with
B <xsl:mode on-no-match="shallow-copy"/>
B <xsl:template match="root">
B B <xsl:copy>
B B B <xsl:for-each-group select="*"
group-starting-with="p[following-sibling::*[1][self::x]]">
B B B B <xsl:choose>
B B B B B <xsl:when test="self::p[following-sibling::*[1][self::x]]">
B B B B B B <xsl:copy>
B B B B B B B <xsl:apply-templates select="current-group()[2],
node()"/>
B B B B B B </xsl:copy>
B B B B B B <xsl:apply-templates select="current-group()[position() gt
2]"/>
B B B B B </xsl:when>
B B B B B <xsl:otherwise>
B B B B B B <xsl:apply-templates select="current-group()"/>
B B B B B </xsl:otherwise>
B B B B </xsl:choose>
B B B </xsl:for-each-group>
B B </xsl:copy>
B </xsl:template>
|