1

我正在尝试使用 XSLT 合并两个 XML 文件。我需要匹配tradeId两个文件中必须存在的节点值,并将所有内容复制到file1.

欢迎任何帮助。所有类似的示例都基于属性和相同的 XML,这对我不起作用。

文件1.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>

文件2.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>
4

1 回答 1

0

If you apply this stylesheet to file1.xml it will explicitly include the corresponding information from file2.xml using the document function. There is no need to declare the namespace S in the stylesheet as the relevant elements have no namespace. 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>
于 2013-05-16T18:33:43.273 回答