简体   繁体   English

将数据从一个xml文件复制到另一个xml文件

[英]copying data from one xml file to another xml file

i have a source.xml file 我有一个source.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<Studentdatabase>
    <Student>
        <id>0001</id>
        <Fname>SOMEX</Fname>
        <Mname>Y</Mname>
        <Lname>Z</Lname>
        <DOB>1992-05-26T00:00:00+05:30</DOB>
        <fathersname>XYZ</fathersname>
        <cls>I</cls>
        <gen>M</gen>
        <add>kadapa</add>
    </Student>
    <Student>
        <id>0002</id>
        <Fname>SOMEA</Fname>
        <Mname>B</Mname>
        <Lname>C</Lname>
        <DOB>1991-04-6T00:00:00+05:30</DOB>
        <fathersname>ABC</fathersname>
        <cls>II</cls>
        <gen>F</gen>
        <add>PUNE</add>
    </Student>
    <Student>
        <id>0003</id>
        <Fname>SOMED</Fname>
        <Mname>E</Mname>
        <Lname>F</Lname>
        <DOB>1990-08-2T00:00:00+05:30</DOB>
        <fathersname>DEF</fathersname>
        <cls>III</cls>
        <gen>M</gen>
        <add>JMD</add>
    </Student>
</Studentdatabase>

and a destination.xml file 和destination.xml文件

<?xml version="1.0" standalone="yes"?>
<MyDB>
  <tableName>
    <studentid>1</studentid>
    <Firstname>chaithanya</Firstname>
    <middlename>babu</middlename>
    <lastname>satyala</lastname>
    <Dateofbirth>1991-05-26T00:00:00+05:30</Dateofbirth>
    <fathersname>babu</fathersname>
    <class>I</class>
    <gender>M</gender>
    <address>kadapa</address>
  </tableName>
  <tableName>
    <studentid>2</studentid>
    <Firstname>charan</Firstname>
    <middlename>kumar</middlename>
    <lastname>palla</lastname>
    <Dateofbirth>1990-10-05T00:00:00+05:30</Dateofbirth>
    <fathersname>krishnaiah</fathersname>
    <class>I</class>
    <gender>M</gender>
    <address>hyderabad</address>
  </tableName>
  <tableName>
    <studentid>3</studentid>
    <Firstname>kondaiah</Firstname>
    <middlename />
    <lastname>dasari</lastname>
    <Dateofbirth>1985-06-05T00:00:00+05:30</Dateofbirth>
    <fathersname>dasari</fathersname>
    <class>II</class>
    <gender>M</gender>
    <address>porumamilla</address>
  </tableName>
  <tableName>
    <studentid>4</studentid>
    <Firstname>dheeraj</Firstname>
    <middlename>reddy</middlename>
    <lastname>polimera</lastname>
    <Dateofbirth>1991-05-16T00:00:00+05:30</Dateofbirth>
    <fathersname>krishna reddy</fathersname>
    <class>II</class>
    <gender>M</gender>
    <address>pulivendula</address>
  </tableName>
  <tableName>
    <studentid>5</studentid>
    <Firstname>shabaz</Firstname>
    <middlename>banu</middlename>
    <lastname>noormohammad</lastname>
    <Dateofbirth>1991-06-16T00:00:00+05:30</Dateofbirth>
    <fathersname>noor ahmed</fathersname>
    <class>III</class>
    <gender>F</gender>
    <address>jmd</address>
  </tableName>
  <tableName>
    <studentid>6</studentid>
    <Firstname>khairuna</Firstname>
    <middlename>begum</middlename>
    <lastname>taticherla</lastname>
    <Dateofbirth>2002-02-02T00:00:00+05:30</Dateofbirth>
    <fathersname>kullay</fathersname>
    <class>III</class>
    <gender>F</gender>
    <address>gugudu</address>
  </tableName>
  <tableName>
    <studentid>7</studentid>
    <Firstname>chandrakala</Firstname>
    <middlename />
    <lastname>kummera</lastname>
    <Dateofbirth>1991-06-03T00:00:00+05:30</Dateofbirth>
    <fathersname>lingreddy</fathersname>
    <class>IV</class>
    <gender>F</gender>
    <address>anantapur</address>
  </tableName>
</MyDB>

Now the issue is i need to insert the data from source file to destination file by changing the names of the elements and tags with reference to destination file... ie, for example <Fname> in the source file must be changed to <FirstName> in destination file 现在的问题是我需要通过参考目标文件更改元素和标签的名称来将数据从源文件插入到目标文件...即,例如,源文件中的<Fname>必须更改为<FirstName>在目标文件中

Can any one help me on this? 谁可以帮我这个事?

You probably want to start off by reading about the XSLT Identity Transform . 您可能想开始阅读有关XSLT Identity Transform的文章 This would just copy all elements as-is 这将照原样复制所有元素

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

In your case though, it looks like only the fathersname element is unchanging. 但是在您的情况下,看起来只有fathersname元素不变。 You then need to write templates to match the elements you do want to change, and create new elements in their place. 然后,您需要编写模板以匹配您要更改的元素,并在其位置创建新元素。 For example, to 'rename' the Studentdatabase to MyDB you would do the following: 例如,要将“学生” 数据库 “重命名”为MyDB,您可以执行以下操作:

<xsl:template match="Studentdatabase">
   <MyDB>
     <xsl:apply-templates select="@*|node()"/>
   </MyDB>
</xsl:template>

And to change Student into tableName you would do the following: 并将Student更改为tableName,您可以执行以下操作:

<xsl:template match="Student">
   <tableName>
     <xsl:apply-templates select="@*|node()"/>
   </tableName>
</xsl:template>

If you repeat this for all the elements you want to rename, you should be able to get your expected output. 如果对要重命名的所有元素重复此操作,则应该能够获得预期的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM