是否可以用XSLT将此数据汇总?我有以下数据集
<?xml version="1.0" encoding="utf-8"?>
<data>
<recordType>EXP</recordType>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>658.82</LdgCost>
<LabCostIn>0.0</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>0.0</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
<recordType>ADD</recordType>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>932.5</LdgCost>
<LabCostIn>104.64</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>260.36</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
<recordType>ADD</recordType>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>932.5</LdgCost>
<LabCostIn>104.64</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>260.36</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
<recordType>ADD</recordType>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>932.5</LdgCost>
<LabCostIn>104.64</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>260.36</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
<recordType>EXP</recordType>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>0.0</LdgCost>
<LabCostIn>322.95</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>0.0</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
</data>
我试图将RecordType ='exp'下的值汇总到一个总数,而RecordType ='Add'下的值将其值汇总到另一个总数。这是可能的还是我需要将XML格式更改为这样的东西?
<?xml version="1.0" encoding="utf-8"?>
<data>
<recordType value='EXP'>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>658.82</LdgCost>
<LabCostIn>0.0</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>0.0</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
</recordType>
<recordType value='ADD'>
<AppContribAmt>0.0</AppContribAmt>
<LdgCost>932.5</LdgCost>
<LabCostIn>104.64</LabCostIn>
<LabCostOut>0.0</LabCostOut>
<ServiceCost>0.0</ServiceCost>
<MatCostIn>260.36</MatCostIn>
<MatCostOut>0.0</MatCostOut>
<ToolCostIn>0.0</ToolCostIn>
<ToolCostOut>0.0</ToolCostOut>
</recordType>
</data>
这是另一个XSLT 1.0选项,它更复杂,但如果您有较大的数据集,则应该更有效。
*我用萨克森(Saxon)9.6进行了测试,并重复了您的数据,因此文件为10,353行,此样式表的运行量约为150毫秒,而其他答案约为1500ms(基于" -t"开关的输出)。结果可能随实际数据和不同的处理器而变化,因此您可能需要测试。
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="recordTypes" match="recordType" use="."/>
<xsl:key name="byRecordType" match="*[not(self::recordType)]"
use="preceding-sibling::recordType[1]"/>
<xsl:template match="/*">
<results>
<xsl:for-each select="recordType[count(.|key('recordTypes',.)[1])=1]">
<xsl:apply-templates select="."/>
</xsl:for-each>
</results>
</xsl:template>
<xsl:template match="recordType">
<sum type="{.}">
<xsl:value-of select="sum(key('byRecordType',.))"/>
</sum>
</xsl:template>
</xsl:stylesheet>
输出
<results>
<sum type="EXP">981.77</sum>
<sum type="ADD">3892.5</sum>
</results
我会在第一个XML上使用类似的东西:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="k" match="recordType" use="."/>
<xsl:template match="recordType[generate-id() = generate-id(key('k', .))]">
<total name="{.}">
<xsl:value-of select="sum(key('k', .)
/following-sibling::*[not(self::recordType)][preceding-sibling::recordType[1] = current()])"/>
</total>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
输出:
<total name="EXP">981.77</total>
<total name="ADD">3892.5</total>