title: XSL-T For All Attributes in All Child Elements When Duplicates in Attribute Names
body: <p>I have a XSL-T which is looping through all the child elements of an XML, and pushing those attributes into a single outputted element. However, there are duplicate attributes in these child elements (same attribute in multiple children), and the XSL is only providing one result for each (only one of the values in that attribute in the family). I also do not believe it is looking for all of the children of the children, and properly pushing these into the resulting attribute set.</p>
  
  <p>I have tried using  a single dynamic XSL-T, and also by using N# XSL templates, with each specifically focusing on a single child element, and then hard-coding the child element's name into the outputted attribute -- the second approach is only giving results for the first template, so did not solve my problem. If my second approach can be fixed to work, that is fine. If there is a way to programatically do this in a dynamic manner (the first option), then really cool! XSL is powerful, so hopefully it can do this, and you all can assist!</p>
  
  <p>Below is an example shaped like my XML document:</p>
  
  <pre><code>&lt;JSON offset="0" total_rows="1337" millis="987"&gt;
  &lt;rows Attribute1="" Attribute2="" oid="0000001"&gt;
  &lt;Name Attribute1="FirstName" Attribute2="LastName"&gt;
  &lt;_id oid="1337"/&gt;
  &lt;/Name&gt;
  &lt;Occupation oid="12345"/&gt;
  &lt;/rows&gt;
  &lt;/JSON&gt;
  </code></pre>
  
  <p>Note that there are duplicate attributes between the parent, and the child elements. I can only get one set of these attributes using my dynamic code or hard-coded approach. Also, I am not sure if either approach is successfully trying to get the child of the child of rows-- since they are not working, I have not been able to confirm that.</p>
  
  <p>My two XSL approaches:</p>
  
  <p>Dynamic (Only returns one value for the attributes which are duplicated):</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:template match="JSON"&gt;
  &lt;DATAPARENT&gt;
  &lt;xsl:apply-templates/&gt;
  &lt;/DATAPARENT&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="//rows"&gt;
  
          &lt;xsl:element name="DataRow"&gt;
          &lt;xsl:for-each select="*"&gt;                   
  
                      &lt;xsl:for-each select="@*"&gt;  
  
                      &lt;xsl:attribute name="{name()}"&gt;  
  
                          &lt;xsl:value-of select="." /&gt;  
  
                      &lt;/xsl:attribute&gt;
  
                      &lt;/xsl:for-each&gt; 
  
          &lt;/xsl:for-each&gt;
          &lt;/xsl:element&gt;
          &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
  
  
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Hard-Coded (Less flexible, which is fine, but also not giving me anything but the first set of results with the rows_{name()}):</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:template match="JSON"&gt;
  &lt;DATAPARENT&gt;
  &lt;xsl:apply-templates/&gt;
  &lt;/DATAPARENT&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/rows"&gt;
  
          &lt;xsl:element name="DataRow"&gt;            
  
                      &lt;xsl:for-each select="@*"&gt;  
  
                      &lt;xsl:attribute name="rows_{name()}"&gt;  
  
                          &lt;xsl:value-of select="." /&gt;  
  
  
                      &lt;/xsl:attribute&gt;
  
                      &lt;/xsl:for-each&gt; 
  
          &lt;/xsl:element&gt;  
  
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="//Name"&gt;
  
          &lt;xsl:element name="DataRow"&gt;            
  
                      &lt;xsl:for-each select="@*"&gt;  
  
                      &lt;xsl:attribute name="Name_{name()}"&gt;  
  
                          &lt;xsl:value-of select="." /&gt;  
  
  
                      &lt;/xsl:attribute&gt;
  
                      &lt;/xsl:for-each&gt; 
  
          &lt;/xsl:element&gt;  
  
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="//Occupation"&gt;
  
          &lt;xsl:element name="DataRow"&gt;            
  
                      &lt;xsl:for-each select="@*"&gt;  
  
                      &lt;xsl:attribute name="Occupation_{name()}"&gt;  
  
                          &lt;xsl:value-of select="." /&gt;  
  
  
                      &lt;/xsl:attribute&gt;
  
                      &lt;/xsl:for-each&gt; 
  
          &lt;/xsl:element&gt;  
  
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Thank you all for your suggestions and insights that can be provided. Please look at the XSL that I have already provided before suggesting basic approaches which just do element attribute looping; I am already using them, and it is the duplicate attribute issue I haven't resolved. Thanks!!</p>
  
  <p>**EDIT: The desired output would be:</p>
  
  <pre><code>&lt;DATAPARENT&gt;
  &lt;DataRow rows_Attribute1="" rows_Attribute2="" rows_oid="0000001" Name_Attribute1="FirstName" Name_Attribute2="LastName" Name__id_oid="1337" Occupation_oid="12345" /&gt;
  &lt;DataRow rows_Attribute1="1" rows_Attribute2="2" rows_oid="0000002" Name_Attribute1="FirstName" Name_Attribute2="LastName" Name__id_oid="1254" Occupation_oid="99231" /&gt;
  &lt;/DATAPARENT&gt;**
  </code></pre>
  
  <p>I am aware that I can't have duplicate named attributes in XML, but I don't know how to programatically create the attribute name with a unique combination referencing the elements in the source. </p>