[英]merge 2 xml file with xslt
I have two xml files persons1 and persons2 and I want to merge them in one xml file using xslt , I m new in xslt so any help will be appreciated : first file persons1: 我有两个xml文件person1和person2,我想使用xslt将它们合并到一个xml文件中,因为我是xslt的新手,所以将不胜感激:第一个文件person1:
<personnes>
<personne>
<name>aaa</name>
<age>10</age>
<adress>aaaaaa</adress>
</personne>
<personne>
<name>bbb</name>
<age>10</age>
<adress>aaaaaa</adress>
</personne>
<personne>
<name>ccc</name>
<age>20</age>
<adress>cccccc</adress>
</personne>
<personne>
<name>ddd</name>
<age>10</age>
<adress>cccccc</adress>
</personne>
</personnes>
second file persons2: 第二档人员2:
<personnes>
<personne>
<id>1111</id>
<quantity>1100</quantity>
</personne>
<personne>
<id>2222</id>
<quantity>2200</quantity>
</personne>
<personne>
<id>3333</id>
<quantity>3300</quantity>
</personne>
<personne>
<id>4444</id>
<quantity>4400</quantity>
</personne>
<personne>
<id>5555</id>
<quantity>5500</quantity>
</personne>
</personnes>
and I want the result in a new xml file like bellow : 我想要结果在新的xml文件中,例如bellow:
<personnes>
<personne>
<id>1111</id>
<name>aaa</name>
<quantity>1100</quantity>
<age>10</age>
<adress>aaaaaa</adress>
</personne>
<personne>
<id>2222</id>
<name>bbb</name>
<quantity>2200</quantity>
<age>10</age>
<adress>aaaaaa</adress>
</personne>
<personne>
<id>3333</id>
<name>ccc</name>
<quantity>3300</quantity>
<age>20</age>
<adress>cccccc</adress>
</personne>
<personne>
<id>4444</id>
<name>ddd</name>
<quantity>4400</quantity>
<age>10</age>
<adress>cccccc</adress>
</personne>
</personnes>
I want to take id and quantity from the file: persons2 and put them in the xml file:persons1 so one by one that mean fisrt with the first , second with second etc... 我想从文件中提取id和数量:person2,并将它们放在xml文件中:persons1,一个接一个地表示第一个,第二个等第二个,等等。
Here is an XSL file that should accomplish what you are looking for out of the merge: 这是一个XSL文件,该文件应该可以完成您要从合并中查找的内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- load the merge file -->
<xsl:variable name="personne2"
select="document('file2.xml')"/>
<xsl:template match="/">
<personnes>
<xsl:for-each select="personnes/personne">
<xsl:variable name="elementposition" select="count(preceding-sibling::*)+1"/>
<!-- copy the child nodes -->
<personne>
<xsl:copy-of select="$personne2/personnes//personne[position() = $elementposition]/id"/>
<xsl:copy-of select="child::name"/>
<xsl:copy-of select="$personne2/personnes//personne[position() = $elementposition]/quantity"/>
<xsl:copy-of select="child::age"/>
<xsl:copy-of select="child::address"/>
</personne>
</xsl:for-each>
</personnes>
<xsl:variable name="doc" select="document('person2.xml')"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="personnes">
<xsl:copy>
<xsl:for-each select="personne">
<xsl:variable name="pos" select="position()"/>
<xsl:copy>
<xsl:if test=".[$pos=$doc/personnes/personne/position()]">
<xsl:copy-of select="$doc/personnes/personne[position() =$pos]/id"/>
</xsl:if>
<xsl:if test="child::name">
<name>
<xsl:value-of select="name"/>
</name>
</xsl:if>
<xsl:if test=".[$pos=$doc/personnes/personne/position()]">
<xsl:copy-of select="$doc/personnes/personne[position() =$pos]/quantity"/>
</xsl:if>
<xsl:if test="child::age">
<age>
<xsl:value-of select="age"/>
</age>
</xsl:if>
<xsl:if test="child::adress">
<adress>
<xsl:value-of select="adress"/>
</adress>
</xsl:if>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
You may also try this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.