XSLT 1.0 solution:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="/root/CONTAINER">
<xsl:copy-of select="."/>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/root/STOP">
<STOP>
<new_tag><xsl:value-of select="." /></new_tag>
<xsl:variable name="stopnum">
<xsl:number level="single" count="STOP" />
</xsl:variable>
<xsl:for-each select="following-sibling::PO[count(preceding-sibling::STOP) = $stopnum]">
<xsl:copy-of select="." />
</xsl:for-each>
</STOP>
</xsl:template>
<xsl:template match="text()" />
</xsl:transform>
The key part is in here:
<xsl:variable name="stopnum">
<xsl:number level="single" count="STOP" />
</xsl:variable>
<xsl:for-each select="following-sibling::PO[count(preceding-sibling::STOP) = $stopnum]">
We set a variable that tells us the position of the last STOP node that's been processed, and then select all PO nodes in our for-each
that have that many preceding-sibling
s that are STOP nodes.
Note that I've wrapped your XML in a <root>
and </root>
node to make it valid XML.
Input:
<root>
<CONTAINER>container1</CONTAINER>
<STOP>stop1</STOP>
<PO>po1</PO>
<PO>po2</PO>
<PO>po3</PO>
<CONTAINER>container2</CONTAINER>
<STOP>stop2</STOP>
<PO>po4</PO>
<PO>po5</PO>
<PO>po6</PO>
<STOP>stop3</STOP>
<PO>po7</PO>
<PO>po8</PO>
</root>
Output:
<root>
<CONTAINER>container1</CONTAINER>
<STOP>
<new_tag>stop1</new_tag>
<PO>po1</PO>
<PO>po2</PO>
<PO>po3</PO>
</STOP>
<CONTAINER>container2</CONTAINER>
<STOP>
<new_tag>stop2</new_tag>
<PO>po4</PO>
<PO>po5</PO>
<PO>po6</PO>
</STOP>
<STOP>
<new_tag>stop3</new_tag>
<PO>po7</PO>
<PO>po8</PO>
</STOP>
</root>