XSLT Convert attribute to element and copy value from an other elemen -


i have xml data this

<abc version="1.0">   <xyz>     <rowset rows="00001">       <rowdef>         <column id="acct_id" len="016" null="y"/>         <column id="agnt_id" len="004" null="y"/>         <column id="cust_extr_id" len="024" null="y"/>         <column id="pi_id" len="019" null="y"/>         <column id="prin_id" len="004" null="y"/>         <column id="sys_id" len="004" null="y"/>       </rowdef>       <row>         <c>6369921501000060</c>         <c>0000</c>         <c>c13093102141063422034238</c>         <c>6369921501000060   </c>         <c>1500</c>         <c>9008</c>       </row>       <row>         <c>6369921501000061</c>         <c>0001</c>         <c>c13093102141063422034231</c>         <c>6369921501000060   </c>         <c>1501</c>         <c>9001</c>       </row>     </rowset>   </xyz> </abc> 

and i'm trying convert into

<?xml version="1.0" encoding="utf-8"?> <abc version="1.0">   <xyz rc="0067">     <rowset rows="00001">       <rowdef>         <acct_id>6369921501000060</acct_id>         <agnt_id>0000</agnt_id>         <cust_extr_id>c13093102141063422034238</cust_extr_id>         <pi_id>6369921501000060</pi_id>         <prin_id>1500</prin_id>         <sys_id>9008</sys_id>       </rowdef>       <rowdef>         <acct_id>6369921501000061</acct_id>         <agnt_id>0001</agnt_id>         <cust_extr_id>c13093102141063422034231</cust_extr_id>         <pi_id>6369921501000060</pi_id>         <prin_id>1501</prin_id>         <sys_id>9001</sys_id>       </rowdef>     </rowset>   </xyz> </abc> 

i have looked around , tried few things not working. can me this. below xslt. in advance.

<?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/xsl/transform">    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>    <xsl:template match="@* | node()">     <xsl:copy>       <xsl:apply-templates select="@* | node()" />     </xsl:copy>   </xsl:template>    <xsl:template match="column">     <xsl:element name="{@id}">       <xsl:copy>         <xsl:apply-templates select="node()"></xsl:apply-templates>       </xsl:copy>       <xsl:apply-templates />       <!--<xsl:call-template name="value"></xsl:call-template>-->     </xsl:element>   </xsl:template>    <!--   <xsl:template match="row" name="value">     <xsl:copy>       <xsl:apply-templates select="node()"></xsl:apply-templates>     </xsl:copy>   </xsl:template>   -->  </xsl:stylesheet> 

this stylesheet want, cannot fathom how generate attribute <xyz rc="0067">.

for each rowset comes across saves rowdef element in variable, copies attribute nodes, , processes each row element. position of each c element in row caclulated, , column in corresponding position within stored rowdef element used fetch element name id attribute.

<?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/xsl/transform">    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>   <xsl:strip-space elements="*"/>    <xsl:template match="@* | node()">     <xsl:copy>       <xsl:apply-templates select="@* | node()" />     </xsl:copy>   </xsl:template>    <xsl:template match="rowset">      <xsl:variable name="columns" select="rowdef/column"/>      <xsl:copy>        <xsl:apply-templates select="@*" />        <xsl:for-each select="row">         <rowdef>           <xsl:for-each select="c">             <xsl:variable name="pos" select="position()"/>             <xsl:element name="{$columns[$pos]/@id}">               <xsl:value-of select="."/>             </xsl:element>           </xsl:for-each>         </rowdef>       </xsl:for-each>      </xsl:copy>    </xsl:template>  </xsl:stylesheet> 

output

<?xml version="1.0" encoding="utf-8"?> <abc version="1.0">    <xyz>       <rowset rows="00001">          <rowdef>             <acct_id>6369921501000060</acct_id>             <agnt_id>0000</agnt_id>             <cust_extr_id>c13093102141063422034238</cust_extr_id>             <pi_id>6369921501000060   </pi_id>             <prin_id>1500</prin_id>             <sys_id>9008</sys_id>          </rowdef>          <rowdef>             <acct_id>6369921501000061</acct_id>             <agnt_id>0001</agnt_id>             <cust_extr_id>c13093102141063422034231</cust_extr_id>             <pi_id>6369921501000060   </pi_id>             <prin_id>1501</prin_id>             <sys_id>9001</sys_id>          </rowdef>       </rowset>    </xyz> </abc> 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -