xml - Move/Copy node to multiple child nodes with XSLT -


i'm new xslt , need solve 1 of issues. want accomplish this:

i have file looking this:

<transaction>     <date>2010-10-14t12:06:12.164+01:00</date>     <production>no</production>     <document fun:oid="1.9.101106">         <documenttype xmlns="">monthly a</documenttype>         <rangename xmlns="">range name</rangename>         <name xmlns="">equity</name>         <language xmlns="">english</language>         <class xmlns="">a acc</class>         <active xmlns="">yes</active>         <country xmlns="">uk</country>         <country xmlns="">luxembourg</country>         <country xmlns="">denmark</country>         <country xmlns="">malta</country>         <primary fun1:oid="1.9.101106" xmlns="" xmlns:fun1="documentxml.com">               <name>sisf-indian-equity-a-acc-fmr-uken</name>               <filesizeinkb>176784</filesizeinkb>               <filetype>pdf</filetype>               <reportingperiod>september</reportingperiod>               <reportingyear>2010</reportingyear>         </primary>         <primary fun1:oid="1.9.101118" xmlns="" xmlns:fun1="documentxml.com">               <name>sisf-indian-equity-a-acc</name>               <filesizeinkb>176784</filesizeinkb>               <filetype>pdf</filetype>               <reportingperiod>september</reportingperiod>               <reportingyear>2010</reportingyear>         </primary>     </document> </transaction> 

and want keep current file, except want copy country nodes in in primary node. this:

<transaction>     <date>2010-10-14t12:06:12.164+01:00</date>     <production>no</production>     <document fun:oid="1.9.101106">         <documenttype xmlns="">monthly a</documenttype>         <rangename xmlns="">range name</rangename>         <name xmlns="">equity</name>         <language xmlns="">english</language>         <class xmlns="">a acc</class>         <active xmlns="">yes</active>         <country xmlns="">uk</country>         <country xmlns="">luxembourg</country>         <country xmlns="">denmark</country>         <country xmlns="">malta</country>         <primary fun1:oid="1.9.101106" xmlns="" xmlns:fun1="documentxml.com">               <name>sisf-indian-equity-a-acc-fmr-uken</name>               <filesizeinkb>176784</filesizeinkb>               <filetype>pdf</filetype>               <reportingperiod>september</reportingperiod>               <reportingyear>2010</reportingyear>               <country xmlns="">uk</country>               <country xmlns="">luxembourg</country>               <country xmlns="">denmark</country>               <country xmlns="">malta</country>         </primary>         <primary fun1:oid="1.9.101118" xmlns="" xmlns:fun1="documentxml.com">               <name>sisf-indian-equity-a-acc</name>               <filesizeinkb>176784</filesizeinkb>               <filetype>pdf</filetype>               <reportingperiod>september</reportingperiod>               <reportingyear>2010</reportingyear>               <country xmlns="">uk</country>               <country xmlns="">luxembourg</country>               <country xmlns="">denmark</country>               <country xmlns="">malta</country>         </primary>     </document> </transaction> 

what best approach accomplish that? need copy of whole document first , copy individual countries, or can in 1 go somehow?

the trick use identity template copy whole document still able modify parts want:

with (slightly modified) input:

<?xml version="1.0" encoding="utf-8"?> <transaction xmlns:fun1="documentxml.com">   <date>2010-10-14t12:06:12.164+01:00</date>   <production>no</production>   <document fun1:oid="1.9.101106">     <documenttype xmlns="">monthly a</documenttype>     <rangename xmlns="">range name</rangename>     <name xmlns="">equity</name>     <language xmlns="">english</language>     <class xmlns="">a acc</class>     <active xmlns="">yes</active>     <country xmlns="">uk</country>     <country xmlns="">luxembourg</country>     <country xmlns="">denmark</country>     <country xmlns="">malta</country>     <primary fun1:oid="1.9.101106" xmlns="" xmlns:fun1="documentxml.com">       <name>sisf-indian-equity-a-acc-fmr-uken</name>       <filesizeinkb>176784</filesizeinkb>       <filetype>pdf</filetype>       <reportingperiod>september</reportingperiod>       <reportingyear>2010</reportingyear>     </primary>     <primary fun1:oid="1.9.101118" xmlns="" xmlns:fun1="documentxml.com">       <name>sisf-indian-equity-a-acc</name>       <filesizeinkb>176784</filesizeinkb>       <filetype>pdf</filetype>       <reportingperiod>september</reportingperiod>       <reportingyear>2010</reportingyear>     </primary>   </document> </transaction> 

explanation changes in input xml: supplied input xml isn't valid because use double quotes every attribute. also, fun prefix isn't declared document node. i've changed fun prefix fun1 , bound prefix @ root level.


and stylesheet:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet    xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">    <xsl:output indent="yes"/>    <!-- identity template copies document -->   <xsl:template match="node() | @*">     <xsl:copy>       <xsl:apply-templates select="node() | @*"/>     </xsl:copy>   </xsl:template>    <!-- template match 'primary' nodes        , modify them way want. -->   <xsl:template match="primary">     <xsl:copy>       <xsl:apply-templates select="node() | @*"/>       <!-- can see, difference            between identity template , specific            template. -->       <xsl:copy-of select="../country"/>     </xsl:copy>   </xsl:template>  </xsl:stylesheet> 

will give identical document country copied primary:

<?xml version="1.0" encoding="utf-8"?> <transaction xmlns:fun1="documentxml.com">   <date>2010-10-14t12:06:12.164+01:00</date>   <production>no</production>   <document fun1:oid="1.9.101106">       <documenttype>monthly a</documenttype>       <rangename>range name</rangename>       <name>equity</name>       <language>english</language>       <class>a acc</class>       <active>yes</active>       <country>uk</country>       <country>luxembourg</country>       <country>denmark</country>       <country>malta</country>       <primary fun1:oid="1.9.101106">          <name>sisf-indian-equity-a-acc-fmr-uken</name>          <filesizeinkb>176784</filesizeinkb>          <filetype>pdf</filetype>          <reportingperiod>september</reportingperiod>          <reportingyear>2010</reportingyear>          <country>uk</country>          <country>luxembourg</country>          <country>denmark</country>          <country>malta</country>       </primary>       <primary fun1:oid="1.9.101118">          <name>sisf-indian-equity-a-acc</name>          <filesizeinkb>176784</filesizeinkb>          <filetype>pdf</filetype>          <reportingperiod>september</reportingperiod>          <reportingyear>2010</reportingyear>          <country>uk</country>          <country>luxembourg</country>          <country>denmark</country>          <country>malta</country>       </primary>   </document> </transaction> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -