title: XSLT multiple document output for combined subelements
body: <p>I have a command contained with a message and body that need to be duplicated. Like so:</p>
  
  <pre><code>&lt;message&gt;
   &lt;body&gt;
    &lt;command name="1"/&gt;
    &lt;command name="2"/&gt;
   &lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>What I would like is:</p>
  
  <pre><code> &lt;message&gt;
   &lt;body&gt;
    &lt;command name="1"/&gt;
   &lt;/body&gt;
  &lt;/message&gt;
  
  &lt;message&gt;
   &lt;body&gt;
     &lt;command name="2"/&gt;
   &lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>I'm using the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"&gt;
  &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;xsl:variable name="items" select="/message/body/*"/&gt;
      &lt;xsl:for-each select="$items"&gt;
          &lt;xsl:result-document href="section{position()}.xml"&gt;
              &lt;xsl:copy-of select="../../.[position()]"/&gt;
          &lt;/xsl:result-document&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>A few clarifications: the name attribute is not a position attribute so it can vary. The command name is not known and can vary. Commands are nodes themselves and can contain elements.</p>
  
  <p>Based on Dabbler's code I was able to produce this which successfully parses the following:</p>
  
  <pre><code>&lt;message&gt;
  &lt;body&gt;
   &lt;commandA num="1"/&gt;
   &lt;commandB num="5"/&gt;
   &lt;commandC num="25"&gt;
      &lt;subCommandT /&gt;
   &lt;/commandC&gt;
  &lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>into </p>
  
  <pre><code>&lt;message&gt;
   &lt;body&gt;
    &lt;commandA num="1"/&gt;
   &lt;/body&gt; 
  &lt;/message&gt;
  
  &lt;message&gt;
   &lt;body&gt;
    &lt;commandB num="5"/&gt;
   &lt;/body&gt;
  &lt;/message&gt;
  
  &lt;message&gt;
  &lt;body&gt;
  &lt;commandC num="25"&gt;
        &lt;subCommandT /&gt;
   &lt;/commandC&gt;
  &lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" &gt;
  &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;xsl:for-each select="/message/body/*"&gt;
          &lt;xsl:apply-templates select="/*"&gt;
              &lt;xsl:with-param name="CmdId" select="generate-id(.)" tunnel="yes"/&gt;
              &lt;xsl:with-param name="CmdPosition" select="position()" tunnel="yes"/&gt;
          &lt;/xsl:apply-templates&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="node()|@*"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="message"&gt;
      &lt;xsl:param name="CmdId" as="xs:string" tunnel="yes"/&gt;
      &lt;xsl:param name="CmdPosition" as="xs:integer" tunnel="yes"/&gt;
      &lt;xsl:result-document href="section{$CmdPosition}.xml"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:result-document&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="body/*"&gt;
      &lt;xsl:param name="CmdId" as="xs:string" tunnel="yes"/&gt;
      &lt;xsl:if test="generate-id(.) = $CmdId"&gt;
          &lt;xsl:copy-of select="."/&gt;
      &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>Thank you Dabbler!</p>
  
  <p>Here is a sample of the original command:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tst:Message xmlns:tst="http://www.someschema.com/v1.4" xmlns:pc="http://www.someotherschema.com/v1.5"&gt;
  &lt;tst:Body tst:hostId="1" tst:machineId="77.D2" tst:dateTimeSent="2012-07-11T15:30:46"&gt;
      &lt;tst:commandOption tst:deviceId="1" tst:dateTime="2012-07-11T15:30:46" tst:commandId="1" tst:sessionType="response" tst:sessionId="28" &gt;
          &lt;tst:commandOptionAck tst:transactionId="12" tst:configurationId="3148"/&gt;
      &lt;/tst:commandOption&gt;
      &lt;tst:commandOption tst:deviceId="1" tst:dateTime="2012-07-11T15:30:48" tst:commandId="2" tst:sessionType="response" tst:sessionId="29" &gt;
          &lt;tst:commandOptionAck tst:transactionId="13" tst:configurationId="3149"/&gt;
      &lt;/tst:commandOption&gt;
      &lt;tst:commandOption tst:deviceId="1" tst:dateTime="2012-07-11T15:30:50" tst:commandId="3" tst:sessionType="response" tst:sessionId="30" &gt;
          &lt;tst:commandOptionAck tst:transactionId="14" /&gt;
      &lt;/tst:commandOption&gt;
      &lt;pc:pContext pc:deviceId="1" pc:dateTime="2012-07-11T15:24:17" pc:commandId="4" pc:sessionId="47146"&gt;
          &lt;pc:Exit pc:transactionId="3116380"/&gt;
      &lt;/pc:pContext&gt;
  &lt;/tst:Body&gt;
  </code></pre>
  
  <p></p>