XSLT 1.0分组

时间:2013-06-11 13:36:06

标签: xslt xslt-grouping

我需要将XSLT 1.0转换应用于以下XML:

<ProfitLossFinancials>
  <ProfitLossFinancial>
    <Year>2013</Year>
    <Turnover>13</Turnover>
    <Profit>13</Profit>
  </ProfitLossFinancial>
  <ProfitLossFinancial>
    <Year>2012</Year>
    <Turnover>12</Turnover>
    <Profit>12</Profit>
  </ProfitLossFinancial>
</ProfitLossFinancials>
<BalanceSheetFinancials>
  <BalanceSheetFinancial>
    <Year>2013</Year>
    <FixedAssets>13</FixedAssets>
  </BalanceSheetFinancial>
  <BalanceSheetFinancial>
    <Year>2011</Year>
    <FixedAssets>11</FixedAssets>
  </BalanceSheetFinancial>
</BalanceSheetFinancials>

有以下输出:

<Financials>
  <Financial>
    <Year>2013</Year>
    <Turnover>13</Turnover>
    <Profit>13</Profit>
    <FixedAssets>13</FixedAssets>
  </Financial>
  <Financial>
    <Year>2012</Year>
    <Turnover>12</Turnover>
    <Profit>12</Profit>
    <FixedAssets/>
  </Financial>
  <Financial>
    <Year>2011</Year>
    <Turnover/>
    <Profit/>
    <FixedAssets>11</FixedAssets>
  </Financial>
</Financials>

怎么做?

3 个答案:

答案 0 :(得分:2)

请试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kFinancialByYear"
           match="*[self::ProfitLossFinancial or self::BalanceSheetFinancial]"
           use="Year" />
  <xsl:variable name="af" 
                select="//*[self::ProfitLossFinancial or 
                            self::BalanceSheetFinancial]"/>

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

  <xsl:template match="/*">
    <Financials>
      <xsl:apply-templates select="$af[generate-id() = 
                                      generate-id(key('kFinancialByYear', Year)[1])]"
                           mode="group" />
    </Financials>
  </xsl:template>

  <xsl:template match="*" mode="group">
    <xsl:variable name="thisYearValues" select="key('kFinancialByYear', Year)" />
    <Financial>
      <xsl:apply-templates select="Year" />
      <Turnover>
        <xsl:value-of select="$thisYearValues/Turnover"/>
      </Turnover>
      <Profit>
        <xsl:value-of select="$thisYearValues/Profit"/>
      </Profit>
      <FixedAssets>
        <xsl:value-of select="$thisYearValues/FixedAssets"/>
      </FixedAssets>
    </Financial>
  </xsl:template>
</xsl:stylesheet>

在此输入上运行时:

<n>
  <ProfitLossFinancials>
    <ProfitLossFinancial>
      <Year>2013</Year>
      <Turnover>13</Turnover>
      <Profit>13</Profit>
    </ProfitLossFinancial>
    <ProfitLossFinancial>
      <Year>2012</Year>
      <Turnover>12</Turnover>
      <Profit>12</Profit>
    </ProfitLossFinancial>
  </ProfitLossFinancials>
  <BalanceSheetFinancials>
    <BalanceSheetFinancial>
      <Year>2013</Year>
      <FixedAssets>13</FixedAssets>
    </BalanceSheetFinancial>
    <BalanceSheetFinancial>
      <Year>2011</Year>
      <FixedAssets>11</FixedAssets>
    </BalanceSheetFinancial>
  </BalanceSheetFinancials>
</n>

结果是:

<Financials>
  <Financial>
    <Year>2013</Year>
    <Turnover>13</Turnover>
    <Profit>13</Profit>
    <FixedAssets>13</FixedAssets>
  </Financial>
  <Financial>
    <Year>2012</Year>
    <Turnover>12</Turnover>
    <Profit>12</Profit>
    <FixedAssets></FixedAssets>
  </Financial>
  <Financial>
    <Year>2011</Year>
    <Turnover></Turnover>
    <Profit></Profit>
    <FixedAssets>11</FixedAssets>
  </Financial>
</Financials>

答案 1 :(得分:1)

以下是使用for-each和key的更直接的解决方案:

<?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 match="Year" name="kyears" use="."/>

    <xsl:template match="/*">
        <Financials>
            <xsl:for-each select="//Year[generate-id() = 
                                    generate-id(key('kyears', .)[1])]"  >
                <xsl:variable name="year" select="." />
                <xsl:variable name="yearData" select="key('kyears', $year)/.." />
                <Financial>
                    <xsl:copy-of select="$year"/>
                    <Turnover>
                        <xsl:value-of select="$yearData/Turnover"/>
                    </Turnover>
                    <Profit>
                        <xsl:value-of select="$yearData/Profit"/>
                    </Profit>
                    <FixedAssets>
                        <xsl:value-of select="$yearData/FixedAssets"/>
                    </FixedAssets>
                </Financial>
            </xsl:for-each>
        </Financials>
    </xsl:template>

</xsl:stylesheet>

生成以下输出:

<?xml version="1.0"?>
<Financials>
  <Financial>
    <Year>2013</Year>
    <Turnover>13</Turnover>
    <Profit>13</Profit>
    <FixedAssets>13</FixedAssets>
  </Financial>
  <Financial>
    <Year>2012</Year>
    <Turnover>12</Turnover>
    <Profit>12</Profit>
    <FixedAssets/>
  </Financial>
  <Financial>
    <Year>2011</Year>
    <Turnover/>
    <Profit/>
    <FixedAssets>11</FixedAssets>
  </Financial>
</Financials>

答案 2 :(得分:-2)

<xsl:template match="ProfitLossFinancials">
    <Financials>
        <xsl:apply-templates select="ProfitLossFinancial"/>
    </Financials>
</xsl:template>

<xsl:template match="ProfitLossFinancial">
    <Financial>
        <xsl:copy-of select="./*"/>
        <xsl:copy-of select="../../BalanceSheetFinancials/BalanceSheetFinancial[Year = current()/Year]/FixedAssets"/>
    </Financial>
</xsl:template>