簡體   English   中英

用xslt合並2個xml文件

[英]merge 2 xml file with xslt

我有兩個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>

第二檔人員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>

我想要結果在新的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>

我想從文件中提取id和數量:person2,並將它們放在xml文件中:persons1,一個接一個地表示第一個,第二個等第二個,等等。

這是一個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM