title: XSLT -- generate unique id for root element and append that value to the id of child elements
body: <p>I need to:</p>
  
  <p>(1) Generate a unique id attribute for the root element</p>
  
  <p>(2) Append that id to the child elements</p>
  
  <p>(3) Append the name and sequence of any parent element to the id attribute of a child element</p>
  
  <p>**note -- I have an XML editor on my machine that can use XSLT 2.0 but would prefer 1.0 because whenever I run macros with visual basic, I think the Microsoft xml/xslt processor can only handle xslt 1.0. It doesn't seem to like 2.0.</p>
  
  <p>Source XML Example:</p>
  
  <pre><code>&lt;root&gt;
  &lt;segment&gt;
  &lt;para&gt;Text of the segment here.&lt;/para&gt;
  &lt;/segment&gt;
  &lt;segment&gt;
  &lt;para&gt;Text of the segment here.&lt;/para&gt;
  &lt;para&gt;Text of the segment here.&lt;/para&gt;
  &lt;/segment&gt;
  &lt;segment&gt;
  &lt;para&gt;Text of the segment here.&lt;/para&gt;
  &lt;sub_segment&gt;
  &lt;para&gt;Text of the segment here.&lt;/para&gt;
  &lt;/sub_segment&gt;
  &lt;/segment&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Desired Output XML:</p>
  
  <pre><code>&lt;root id="idrootx2x1"&gt;
  &lt;segment id="idrootx2x1.segment.1"&gt;
  &lt;para id="idrootx2x1.segment.1.para.1"&gt;Text of the segment here.&lt;/para&gt;
  &lt;/segment&gt;
  &lt;segment id="idrootx2x1.segment.2"&gt;
  &lt;para id="idrootx2x1.segment.2.para.1"&gt;Text of the segment here.&lt;/para&gt;
  &lt;para id="idrootx2x1.segment.2.para.2"&gt;Text of the segment here.&lt;/para&gt;
  &lt;/segment&gt;
  &lt;segment id="idrootx2x1.segment.3"&gt;
  &lt;para id="idrootx2x1.segment.3.para.1"&gt;Text of the segment here.&lt;/para&gt;
  &lt;sub_segment id="idrootx2x1.segment.3.sub_segment.1"&gt;
  &lt;para id="idrootx2x1.segment.3.sub_segment.1.para.1"&gt;Text of the segment here.&lt;/para&gt;
  &lt;/sub_segment&gt;
  &lt;/segment&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Here is the XSLT I have so far:</p>
  
  <pre><code>&lt;xsl:template match="*|@*|text()"&gt;
  &lt;xsl:copy&gt;
      &lt;xsl:apply-templates select="*|@*|text()"/&gt; 
  &lt;/xsl:copy&gt; 
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="root"&gt;
  &lt;xsl:copy&gt;
      &lt;xsl:attribute name="id"&gt;&lt;xsl:value-of select="generate-id()"/&gt;&lt;/xsl:attribute&gt;
      &lt;xsl:apply-templates select="*|@*|text()"/&gt;
  &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="segment | para | sub_segment"&gt;
  &lt;xsl:copy&gt;
      &lt;xsl:attribute name="id"&gt;
          &lt;xsl:value-of select="name(.)"/&gt;.&lt;xsl:number format="1" level="single"/&gt;
      &lt;/xsl:attribute&gt;
      &lt;xsl:apply-templates select="*|@*|text()"/&gt;
  &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>