xml - Change the name of the node using xsl:copy-of -


i using xslt denormalized xml document.

the problem same name of node used in different levels, , results in duplicates in result file.

e.g.

the xml source file looks this:

<?xml version="1.0" encoding="utf-8"?> <customers>     <customer>         <id>1</id>         <name>john madsen</name>         <accounts>             <account>                 <id>111</id>                 <name>aaa</name>                 <value>11234</value>                             </account>             <account>                 <id>222</id>                 <name>bbb</name>                 <value>64</value>             </account>         </accounts>             </customer>      <customer>         <id>2</id>         <name>dona m. graduate</name>         <accounts>             <account>                 <id>333</id>                 <name>ccc</name>                 <value>5215</value>                             </account>             <account>                 <id>555</id>                 <name>fff</name>                 <value>6325</value>             </account>         </accounts>             </customer> </customers> 

i want transform flat list of accounts, repeating information regarding customers, this:

<?xml version="1.0" encoding="utf-8"?> <accounts>     <account>         <customerid>1</customerid>         <customername>john madsen</customername>         <id>111</id>         <name>aaa</name>         <value>11234</value>                     </account>     <account>         <customerid>1</customerid>         <customername>john madsen</customername>         <id>222</id>         <name>bbb</name>         <value>64</value>     </account>     <account>         <customerid>2</customerid>         <customername>dona m. graduate</customername>         <id>333</id>         <name>ccc</name>         <value>5215</value>                     </account>     <account>         <customerid>2</customerid>         <customername>dona m. graduate</customername>         <id>555</id>         <name>fff</name>         <value>6325</value>     </account> </accounts> 

i using following xls code:

<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="/">     <accounts>         <xsl:for-each select="customers/customer/accounts/account">             <account>                 <xsl:copy-of select="ancestor::customer/*[not(*)] "/>                 <xsl:copy-of select="*" />             </account>                                         </xsl:for-each>     </accounts> </xsl:template>  </xsl:stylesheet>  

and getting result, note how account has 2 id nodes , name nodes, 1 if account id , name , other customer id , name.

<?xml version="1.0" encoding="utf-8"?> <accounts>    <account>       <id>1</id>       <name>john madsen</name>       <id>111</id>       <name>aaa</name>       <value>11234</value>    </account>    <account>       <id>1</id>       <name>john madsen</name>       <id>222</id>       <name>bbb</name>       <value>64</value>    </account>    <account>       <id>2</id>       <name>dona m. graduate</name>       <id>333</id>       <name>ccc</name>       <value>5215</value>    </account>    <account>       <id>2</id>       <name>dona m. graduate</name>       <id>555</id>       <name>fff</name>       <value>6325</value>    </account> </accounts> 

how can add prefix copied id , name customer node?

regards rafi

the copy-of instruction copies selected nodeset , cannot modify it, therefore recommend using apply-templates instead , perform operation in separate template. following code job you:

<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="/">     <accounts>         <xsl:for-each select="customers/customer/accounts/account">             <account>                 <xsl:apply-templates select="ancestor::customer/*[not(*)]"/>                 <xsl:copy-of select="*" />             </account>                                         </xsl:for-each>     </accounts> </xsl:template>  <xsl:template match="customer/*[not(*)]">     <xsl:element name="{concat('customer', name())}">         <xsl:copy-of select="@*|node()"/>     </xsl:element> </xsl:template>  </xsl:stylesheet>  

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -