【问题标题】:Merge two different XML files based on node value using XSLT使用 XSLT 基于节点值合并两个不同的 XML 文件
【发布时间】:2013-05-16 17:40:49
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

    标签: xml xslt merge


    【解决方案1】:

    如果您将此样式表应用于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>
    

    【讨论】:

    • 非常感谢鲍罗丁。但是你介意一点解释它是如何工作的吗?我确实为每个标签和其他标签尝试了一些 xsl,但没有完成它。它如何将正确的期望值放置在交易标签中?如果我需要在文档中的其他位置放置我应该怎么做。如果你能解释一下就太好了……
    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2013-10-02
    • 1970-01-01
    相关资源
    最近更新 更多