简体   繁体   English

使用XSLT根据节点值合并两个不同的XML文件

[英]Merge two different XML files based on node value using XSLT

I am trying to merge two XML files using XSLT. 我正在尝试使用XSLT合并两个XML文件。 I need to match the tradeId node value which must present in both files, and just copy all the content to file1 . 我需要匹配两个文件中都必须存在的tradeId节点值,然后将所有内容复制到file1

Any help would be welcome. 任何帮助都将受到欢迎。 All similar examples are based on attributes and same XML which does not work for me. 所有类似的示例均基于属性和对我不起作用的相同XML。

File1.xml File1.xml

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">

  <S:Body>

    <trade>
      <tradeId>1</tradeId>
      <createdBy>1</createdBy>
      <createdOnDate>2</createdOnDate>
      <Payment>
        <indicator>3</indicator>
        <updateBy>4</updateBy>
      </Payment>
      <Parties>
        <partyId>5</partyId>
        <partyRoleTypeCode>6</partyRoleTypeCode>
      </Parties>
      <Product>
        <term>7</term>
        <shortDescription>8</shortDescription>
      </Product>
    </trade>

    <trade>
      <tradeId>2</tradeId>
      <createdBy>10</createdBy>
      <createdOnDate>20</createdOnDate>
      <Payment>
        <indicator>30</indicator>
        <updateBy>40</updateBy>
      </Payment>
      <Parties>
        <partyId>50</partyId>
        <partyRoleTypeCode>60</partyRoleTypeCode>
      </Parties>
      <Product>
        <term>70</term>
        <shortDescription>80</shortDescription>
      </Product>
    </trade>

  </S:Body>

</S:Envelope>

File2.xml File2.xml

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">

  <S:Body>

    <financialexpectation>
      <tradeId>1</tradeId>
      <stateCode>TBD</stateCode>
      <methodCode>TBD</methodCode>
      <TypeCode>NONE</TypeCode>
    </financialexpectation>

    <financialexpectation>
      <tradeId>2</tradeId>
      <stateCode>TBD</stateCode>
      <methodCode>TBD</methodCode>
      <TypeCode>NONE</TypeCode>
    </financialexpectation>

    <financialexpectation>
      <tradeId>3</tradeId>
      <stateCode>TBD</stateCode>
      <methodCode>TBD</methodCode>
      <TypeCode>NONE</TypeCode>
    </financialexpectation>

  </S:Body>

</S:Envelope>

Expected output 预期产量

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">

  <S:Body>

    <trade>
      <tradeId>1</tradeId>
      <createdBy>1</createdBy>
      <createdOnDate>2</createdOnDate>
      <Payment>
        <indicator>3</indicator>
        <updateBy>4</updateBy>
      </Payment>
      <Parties>
        <partyId>5</partyId>
        <partyRoleTypeCode>6</partyRoleTypeCode>
      </Parties>
      <Product>
        <term>7</term>
        <shortDescription>8</shortDescription>
      </Product>
      <financialexpectation>
        <tradeId>1</tradeId>
        <stateCode>TBD</stateCode>
        <methodCode>TBD</methodCode>
        <TypeCode>NONE</TypeCode>
      </financialexpectation>
    </trade>

    <trade>
      <tradeId>2</tradeId>
      <createdBy>10</createdBy>
      <createdOnDate>20</createdOnDate>
      <Payment>
        <indicator>30</indicator>
        <updateBy>40</updateBy>
      </Payment>
      <Parties>
        <partyId>50</partyId>
        <partyRoleTypeCode>60</partyRoleTypeCode>
      </Parties>
      <Product>
        <term>70</term>
        <shortDescription>80</shortDescription>
      </Product>
      <financialexpectation>
        <tradeId>2</tradeId>
        <stateCode>TBD</stateCode>
        <methodCode>TBD</methodCode>
        <TypeCode>NONE</TypeCode>
      </financialexpectation>
    </trade>

  </S:Body>

</S:Envelope>

If you apply this stylesheet to file1.xml it will explicitly include the corresponding information from file2.xml using the document function. 如果将此样式表应用于file1.xml ,它将使用document函数显式包括来自file2.xml的相应信息。 There is no need to declare the namespace S in the stylesheet as the relevant elements have no namespace. 不需要在样式表中声明名称空间S ,因为相关元素没有名称空间。 I hope this helps. 我希望这有帮助。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

  <xsl:template match="trade">
    <xsl:copy>
      <xsl:apply-templates/>
      <xsl:copy-of select="document('file2.xml')//financialexpectation[tradeId=current()/tradeId]"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

output 输出

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <trade>
         <tradeId>1</tradeId>
         <createdBy>1</createdBy>
         <createdOnDate>2</createdOnDate>
         <Payment>
            <indicator>3</indicator>
            <updateBy>4</updateBy>
         </Payment>
         <Parties>
            <partyId>5</partyId>
            <partyRoleTypeCode>6</partyRoleTypeCode>
         </Parties>
         <Product>
            <term>7</term>
            <shortDescription>8</shortDescription>
         </Product>
         <financialexpectation>
            <tradeId>1</tradeId>
            <stateCode>TBD</stateCode>
            <methodCode>TBD</methodCode>
            <TypeCode>NONE</TypeCode>
         </financialexpectation>
      </trade>
      <trade>
         <tradeId>2</tradeId>
         <createdBy>10</createdBy>
         <createdOnDate>20</createdOnDate>
         <Payment>
            <indicator>30</indicator>
            <updateBy>40</updateBy>
         </Payment>
         <Parties>
            <partyId>50</partyId>
            <partyRoleTypeCode>60</partyRoleTypeCode>
         </Parties>
         <Product>
            <term>70</term>
            <shortDescription>80</shortDescription>
         </Product>
         <financialexpectation>
            <tradeId>2</tradeId>
            <stateCode>TBD</stateCode>
            <methodCode>TBD</methodCode>
            <TypeCode>NONE</TypeCode>
         </financialexpectation>
      </trade>
   </S:Body>
</S:Envelope>

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

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