我需要根据OPERATION_CODE对记录进行分组。每条记录在组合标记中可以有多个Allowance。如果多个记录有相同的operation_code,我需要所有的余量。我使用的是xsl 1.0。使用这个xsl,我每条记录只有一个Allowance (在示例输入中,第一个记录中的第一个允许值和第二个记录中的第一个允许值)。预期输出是第一条记录和第二条记录中的所有余量,因为operation_code是相同的。
sample input:
<root xmlns="">
<records>
<record>
<OPERATION_CODE>123456</OPERATION_CODE>
<Combinations>
<allowance>
<WMI_CODE>MR0</WMI_CODE>
<VDS_CODE>1</VDS_CODE>
</allowance>
<allowance>
<WMI_CODE>MR1</WMI_CODE>
<VDS_CODE>2</VDS_CODE>
</allowance>
</Combinations>
</record>
<record>
<OPERATION_CODE>123456</OPERATION_CODE>
<Combinations>
<allowance>
<WMI_CODE>MR2</WMI_CODE>
<VDS_CODE>3</VDS_CODE>
</allowance>
</Combinations>
</record>
</records>
</root>预期输出:
<LaborOperationsDetail>
<LaborOperationID>123456</LaborOperationID>
<Combinations>
<Allowance>
<GroupID>MR0</star:GroupID>
<VID>1</star:VID>
</Allowance>
<Allowance>
<GroupID>MR1</star:GroupID>
<VID>2</star:VID>
</Allowance>
<Allowance>
<GroupID>MR2</star:GroupID>
<VID>3</star:VID>
</Allowance>
</Combinations>
<LaborOperationsDetail>使用的xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:key name="opcode" match="record" use="OPERATION_CODE" />
<xsl:template match="root/records">
<xsl:for-each select="record[generate-id() = generate-id(key('opcode', OPERATION_CODE)[1])]">
<LaborOperationsDetail>
<LaborOperationID><xsl:value-of select="OPERATION_CODE"/></LaborOperationID>
<Combinations>
<xsl:for-each select="key('opcode' ,OPERATION_CODE)">
<Allowance>
<GroupID><xsl:value-of select="Combinations/allowance/WMI_CODE" /></GroupID>
<VID><xsl:value-of select="Combinations/allowance/VDS_CODE" /></VID>
</Allowance>
</xsl:for-each >
</Combinations>
</LaborOperationsDetail>
</xsl:for-each >
</xsl:template>
</xsl:stylesheet>发布于 2018-12-25 17:24:38
用这种方式试试?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="opcode" match="record" use="OPERATION_CODE"/>
<xsl:template match="/root">
<xsl:for-each select="records/record[generate-id() = generate-id(key('opcode', OPERATION_CODE)[1])]">
<LaborOperationsDetail>
<LaborOperationID>
<xsl:value-of select="OPERATION_CODE"/>
</LaborOperationID>
<Combinations>
<xsl:for-each select="key('opcode' ,OPERATION_CODE)/Combinations/allowance">
<Allowance>
<GroupID>
<xsl:value-of select="WMI_CODE"/>
</GroupID>
<VID>
<xsl:value-of select="VDS_CODE"/>
</VID>
</Allowance>
</xsl:for-each>
</Combinations>
</LaborOperationsDetail>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>您现在所拥有的:
<xsl:for-each select="key('opcode' ,OPERATION_CODE)">选择当前组中的两个record节点,并为每个节点创建一个Allowance节点。在这些节点中,从每个record中的first allowance检索值。
https://stackoverflow.com/questions/53912560
复制相似问题