<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:output method="xhtml" indent="no"/>

<--xsl:strip-space elements="*"/-->


<xsl:template match="/">
  <html>
    <head>
      <title>Grouping By Adjacent Key Value</title>
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:template match="p">

<xsl:for-each-group select="node()" group-adjacent="self::ul or self::ol">
  <xsl:choose>
    <xsl:when test="current-grouping-key()">
      <xsl:copy-of select="current-group()"/>  
    </xsl:when>
    <xsl:otherwise>
      <p>
	<xsl:copy-of select="current-group()"/>
      </p>
    </xsl:otherwise>  
  </xsl:choose>
</xsl:for-each-group>

</xsl:template>

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>