簡體   English   中英

使用XSLT根據節點值合並兩個不同的XML文件

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

我正在嘗試使用XSLT合並兩個XML文件。 我需要匹配兩個文件中都必須存在的tradeId節點值,然后將所有內容復制到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

<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>

預期產量

<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>

如果將此樣式表應用於file1.xml ,它將使用document函數顯式包括來自file2.xml的相應信息。 不需要在樣式表中聲明名稱空間S ,因為相關元素沒有名稱空間。 我希望這有幫助。

<?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>

輸出

<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