使用分组和匹配节点进行总和时,该组的第一个元素中不存在结果。
。XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="EmployeeDetailStylesheet.xsl" ?>
<PayDetailsExportResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PayDetailExport>
<PayrollBatches>
<PayrollBatch>
<PayBatchEmployees>
<PayrollBatchEmployee>
<FirstName>Stacey</FirstName>
<NationalId>999999999</NationalId>
<PayrollBatchDetails>
<PayrollBatchDetail>
<DeductionAdjustmentsNotToInclude>
<DeductionAdjustment>
<TakenAmount>55.00</TakenAmount>
<DeductionType>401(k)</DeductionType>
</DeductionAdjustment>
</DeductionAdjustmentsNotToInclude>
</PayrollBatchDetail>
</PayrollBatchDetails>
</PayrollBatchEmployee>
</PayBatchEmployees>
</PayrollBatch>
<PayrollBatch>
<PayBatchEmployees>
<PayrollBatchEmployee>
<FirstName>Stacey</FirstName>
<NationalId>111111111</NationalId>
<PayrollBatchDetails>
<PayrollBatchDetail>
<DeductionAdjustmentss>
<DeductionAdjustment>
<TakenAmount>70.00</TakenAmount>
<DeductionType>401(k) - Catch up</DeductionType>
</DeductionAdjustment>
</DeductionAdjustmentss>
</PayrollBatchDetail>
<PayrollBatchDetail>
<DeductionAdjustments>
<DeductionAdjustment>
<TakenAmount>20.00</TakenAmount>
<DeductionType>401(k)</DeductionType>
</DeductionAdjustment>
</DeductionAdjustments>
</PayrollBatchDetail>
</PayrollBatchDetails>
</PayrollBatchEmployee>
</PayBatchEmployees>
</PayrollBatch>
</PayrollBatches>
</PayDetailExport>
</PayDetailsExportResult>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:pdi="http://www.profdata.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- strip-space removes whitespace nodes such as tabs and newlines that occur between elements.-->
<xsl:strip-space elements="*"/>
<!-- Set output method to indicate a text file (not XML) -->
<xsl:output method="text" encoding="utf-8" indent="yes"/>
<xsl:key name="payrollBatchEmpGroup" match="PayrollBatchEmployee" use="NationalId" />
<xsl:key name="deductionAdjustmentGroup" match="DeductionAdjustments/DeductionAdjustment" use="concat(../../../../NationalId, '+', DeductionType)" />
<xsl:template match="PayDetailsExportResult">
<!-- Output the CSV data row -->
<xsl:for-each select="PayDetailExport/PayrollBatches/PayrollBatch">
<xsl:for-each select="PayBatchEmployees/PayrollBatchEmployee[count(. | key('payrollBatchEmpGroup', NationalId)[1]) = 1]">
<xsl:variable name="ssn" select="NationalId" />
<xsl:value-of select="FirstName"/>
<text>--</text>
<xsl:for-each select="PayrollBatchDetails/PayrollBatchDetail[DeductionAdjustments]">
<xsl:for-each select="DeductionAdjustments/DeductionAdjustment[DeductionType='401(k)'][count(. | key('deductionAdjustmentGroup', concat($ssn, '+', DeductionType))[1]) = 1]">
<xsl:value-of select="sum(key('deductionAdjustmentGroup', concat($ssn , '+' , DeductionType))/TakenAmount)" />
</xsl:for-each>
</xsl:for-each>
<!--End of row-->
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
结果
Stacey-
预期结果
Stacey- 20
注意: - 当我们更改节点 DEXACTADADJUSTMENTNOTNOTTOINCLUDE 到 DEXACTIONADADJUSTMENTS (该组的第一个元素具有匹配节点)结果(即 - stacey-- 75)时,。
首先在XML中,您需要纠正" DextuctionAdjustmentss"错字。这样做之后,这有效:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:pdi="http://www.profdata.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- strip-space removes whitespace nodes such as tabs and newlines that occur between elements.-->
<xsl:strip-space elements="*"/>
<!-- Set output method to indicate a text file (not XML) -->
<xsl:output method="text" encoding="utf-8" indent="yes"/>
<xsl:key name="payrollBatchEmpGroup" match="PayrollBatchEmployee" use="NationalId" />
<xsl:key name="deductionAdjustmentGroup" match="DeductionAdjustments/DeductionAdjustment" use="concat(../../../../NationalId, '+', DeductionType)" />
<xsl:template match="PayDetailsExportResult">
<!-- Output the CSV data row -->
<xsl:for-each select="PayDetailExport/PayrollBatches/PayrollBatch/PayBatchEmployees/PayrollBatchEmployee/PayrollBatchDetails/PayrollBatchDetail/DeductionAdjustments/DeductionAdjustment
[DeductionType='401(k)' and count(. | key('deductionAdjustmentGroup', concat(../../../../NationalId, '+', DeductionType))[1]) = 1]">
<xsl:value-of select="key('payrollBatchEmpGroup', ../../../../NationalId)/FirstName"/>
<text>--</text>
<xsl:value-of select="sum(key('deductionAdjustmentGroup', concat(../../../../NationalId, '+' , DeductionType))/TakenAmount)" />
<!--End of row-->
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>