如何基于唯一元素联接两个节点集?

时间:2019-02-04 19:49:24

标签: xml join xslt-1.0 nodes

我有一个带有不同节点集的大xml文件,我需要根据两个节点都包含的唯一元素值使用xsl对其进行组合。

以下是需要转换的xml文件的示例:

<root>
  <node1>
    <funds>
      <fund>
        <FundId>a</FundId>
        <FundName>fund a</FundName>
        <SomeInfo>some info</SomeInfo>
      </fund>
      <fund>
        <FundId>b</FundId>
        <FundName>fund b</FundName>
        <SomeInfo>some info</SomeInfo>
      </fund>
      <fund>
        <FundId>c</FundId>
        <FundName>fund c</FundName>
        <SomeInfo>some info</SomeInfo>
      </fund>
    </funds>
  </node1>
  <node2>
    <funds>
      <fund>
        <FundId>a</FundId>
        <MaxInvestmentAmount>200</MaxInvestmentAmount>
        <MinInvestmentAmount>1</MinInvestmentAmount>
      </fund>
      <fund>
        <FundId>b</FundId>
        <MaxInvestmentAmount>100</MaxInvestmentAmount>
        <MinInvestmentAmount>5</MinInvestmentAmount>
      </fund>
      <fund>
        <FundId>c</FundId>
        <MaxInvestmentAmount>50</MaxInvestmentAmount>
        <MinInvestmentAmount>20</MinInvestmentAmount>
      </fund>
    </funds>
  </node2>
</root>

这是所需的输出:

<node>
    <funds>
        <fund>
            <FundId>a<FundId/>
            <FundName>fund a</FundName>
            <SomeInfo>some info</SomeInfo>
            <MaxInvestmentAmount>200</MaxInvestmentAmount>
            <MinInvestmentAmount>1</MinInvestmentAmount>
        </fund>
        <fund>
            <FundId>b<FundId/>
            <FundName>fund b</FundName>
            <SomeInfo>some info</SomeInfo>
            <MaxInvestmentAmount>100</MaxInvestmentAmount>
            <MinInvestmentAmount>5</MinInvestmentAmount>
        </fund>
        <fund>
            <FundId>c<FundId/>
            <FundName>fund c</FundName>
            <SomeInfo>some info</SomeInfo>
            <MaxInvestmentAmount>50</MaxInvestmentAmount>
            <MinInvestmentAmount>20</MinInvestmentAmount>
        </fund>
    </funds>
</node>

我已经尝试过模板匹配,但是这似乎不像我尝试过的那样工作,因为两个节点具有相同的内部节点名称,因此它们会彼此覆盖。

1 个答案:

答案 0 :(得分:1)

实现此目标的可靠方法是在xsl:key元素上使用<fund>作为关键字的FundId元素上使用FundId。此方法的一个相关限制是仅合并第一个子元素的<node1>键(此处为<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:key name="keyFund" match="fund" use="FundId" /> <xsl:template match="/root"> <node> <funds> <xsl:for-each select="*[1]/funds/fund"> <fund> <xsl:copy-of select="FundId" /> <xsl:for-each select="key('keyFund',FundId)"> <xsl:copy-of select="current()/*[not(self::FundId)]" /> </xsl:for-each> </fund> </xsl:for-each> </funds> </node> </xsl:template> </xsl:stylesheet> )。如果其他子元素包含更多值,则此方法将无法正常工作。

这是 XSLT-1.0 样式表:

<?xml version="1.0"?>
<node>
    <funds>
        <fund>
            <FundId>a</FundId>
            <FundName>fund a</FundName>
            <SomeInfo>some info</SomeInfo>
            <MaxInvestmentAmount>200</MaxInvestmentAmount>
            <MinInvestmentAmount>1</MinInvestmentAmount>
        </fund>
        <fund>
            <FundId>b</FundId>
            <FundName>fund b</FundName>
            <SomeInfo>some info</SomeInfo>
            <MaxInvestmentAmount>100</MaxInvestmentAmount>
            <MinInvestmentAmount>5</MinInvestmentAmount>
        </fund>
        <fund>
            <FundId>c</FundId>
            <FundName>fund c</FundName>
            <SomeInfo>some info</SomeInfo>
            <MaxInvestmentAmount>50</MaxInvestmentAmount>
            <MinInvestmentAmount>20</MinInvestmentAmount>
        </fund>
    </funds>
</node>

其输出为:

{{1}}