基于 xsl 1.0 中的条件分组 [英] Grouping based on condition in xsl 1.0
问题描述
我需要根据 OperationID 进行分组,然后再次根据 OperationID 中的 COMBINATION_CODE 进行分组.但是 COMBINATION_CODE 也可能有空标签.仅当 COMBINATION_CODE 中存在值时,下面的样式表才按预期对 COMBINATION_CODE 进行分组.如果存在空标记,则不管 OperationID 是什么,它将所有空 COMBINATION_CODE 记录组合在一起.请找到示例输入:
I need to group based on OperationID and then again group based on COMBINATION_CODE within the OperationID. But COMBINATION_CODE may also have empty tag. The below style sheet is grouping COMBINATION_CODE as expected only if the value is present in COMBINATION_CODE. If empty tag is present, irrespective of OperationID, it is grouping all the empty COMBINATION_CODE records together. Please find the sample input:
<root>
<records>
<record>
<OperationID>13</OperationID>
<COMBINATION_CODE>c</COMBINATION_CODE>
<GroupID>00</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456789</ID>
<DocumentID>ShowOperationCode20181206071249</DocumentID>
<AllGroupID>JTH</AllGroupID>
<AllID>B21B1</AllID>
</record>
<record>
<OperationID>13</OperationID>
<COMBINATION_CODE>c</COMBINATION_CODE>
<GroupID>00</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456789</ID>
<DocumentID>ShowOperationCode20181206071249</DocumentID>
<AllGroupID>JTT</AllGroupID>
<AllID>B21FB</AllID>
</record>
<record>
<OperationID>13</OperationID>
<COMBINATION_CODE/>
<GroupID>00</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456789</ID>
<DocumentID>ShowOperationCode20181206071249</DocumentID>
<AllGroupID>JTT</AllGroupID>
<AllID>B21FC</AllID>
</record>
<record>
<OperationID>14</OperationID>
<COMBINATION_CODE/>
<GroupID>01</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456788</ID>
<DocumentID>ShowOperationCode20181206071250</DocumentID>
<AllGroupID>KTH</AllGroupID>
<AllID>BFFHT</AllID>
</record>
</records>
</root>
预期输出:
<?xml version="1.0" encoding="iso-8859-1"?>
<Show releaseID="5.4.4" xmlns:star="http://www.starstandard.org/STAR/5">
<DataArea>
<LOperations>
<LOperationsDetail>
<LOperationID>13</LOperationID>
<Combinations>
<combinationCode>c</combinationCode><!-- combinationCode is grouped and each record is present inside VLaborAllowance -->
<VLaborAllowance xmlns:star="http://www.starstandard.org/STAR/5" >
<VIGroup>
<GID>JTH</GID>
<VID>B21B1</VID>
</VIGroup>
</VLaborAllowance>
<VLaborAllowance xmlns:star="http://www.starstandard.org/STAR/5" >
<VIGroup>
<GID>JTT</GID>
<VID>B21FB</VID>
</VIGroup>
</VLaborAllowance>
</Combinations>
<Combinations>
<combinationCode/><!--empty tag should present in separate combination-->
<VLaborAllowance xmlns:star="http://www.starstandard.org/STAR/5" >
<VIGroup>
<GID>JTT</GID>
<VID>B21FC</VID>
</VIGroup>
</VLaborAllowance>
</Combinations>
</LOperationsDetail>
<LOperationsDetail>
<LOperationID>KTH</LOperationID>
<Combinations>
<combinationCode/>
<VLaborAllowance xmlns:star="http://www.starstandard.org/STAR/5" >
<VIGroup>
<GID>KTH</GID>
<VID>BFFHT</VID>
</VIGroup>
</VLaborAllowance>
</Combinations>
</LOperationsDetail>
</star:LOperations>
</star:DataArea>
</star:Show>
使用的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<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:strip-space elements="*"/>
<xsl:key name="opcode" match="record" use="OperationID" />
<xsl:key name="combination" match="record" use="COMBINATION_CODE" />
<xsl:template match="root/records">
<Show releaseID="5.4.4" xmlns:star="http://www.starstandard.org/STAR/5">
<DataArea>
<LOperations>
<xsl:for-each select="record[count(. | key('opcode', OperationID)[1]) = 1]" >
<LOperationsDetail>
<LOperationID><xsl:value-of select="OperationID"/></LOperationID>
<xsl:for-each select="key('opcode',OperationID)[count(. | key('combination', COMBINATION_CODE)[1]) = 1]" >
<Combinations>
<combinationCode><xsl:value-of select="COMBINATION_CODE"/></combinationCode>
<xsl:for-each select="key('combination', COMBINATION_CODE)">
<VLaborAllowance xmlns:star="http://www.starstandard.org/STAR/5" >
<VIGroup>
<GID><xsl:value-of select="AllGroupID" /></GID>
<VID><xsl:value-of select="AllID" /></VID>
</VIGroup>
</VLaborAllowance>
</xsl:for-each>
</Combinations>
</xsl:for-each>
</LOperationsDetail>
</xsl:for-each>
</LOperations>
</DataArea>
</Show>
</xsl:template>
</xsl:stylesheet>
电流输出:
<?xml version="1.0" encoding="UTF-8"?>
<Show xmlns:star="http://www.starstandard.org/STAR/5" releaseID="5.4.4">
<DataArea>
<LOperations>
<LOperationsDetail>
<LOperationID>13</LOperationID>
<Combinations>
<combinationCode>c</combinationCode>
<VLaborAllowance>
<VIGroup>
<GID>JTH</GID>
<VID>B21B1</VID>
</VIGroup>
</VLaborAllowance>
<VLaborAllowance>
<VIGroup>
<GID>JTT</GID>
<VID>B21FB</VID>
</VIGroup>
</VLaborAllowance>
</Combinations>
<Combinations>
<combinationCode/>
<VLaborAllowance>
<VIGroup>
<GID>JTT</GID>
<VID>B21FC</VID>
</VIGroup>
</VLaborAllowance>
<VLaborAllowance>
<VIGroup>
<GID>KTH</GID>
<VID>BFFHT</VID>
</VIGroup>
</VLaborAllowance>
</Combinations>
</LOperationsDetail>
<LOperationsDetail>
<LOperationID>14</LOperationID>
</LOperationsDetail>
</LOperations>
</DataArea>
</Show>
推荐答案
为了在基于OperationID
的当前组内创建基于COMBINATION_CODE
的子组,您必须在子组的键定义中包含 OperationID
:
In order to create a sub-group based on COMBINATION_CODE
within the current group based on OperationID
, you must include OperationID
in the sub-group's key definition:
<xsl:key name="combination" match="record" use="concat(COMBINATION_CODE, '|', OperationID)" />
以及调用密钥时:
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:strip-space elements="*"/>
<xsl:key name="opcode" match="record" use="OperationID" />
<xsl:key name="combination" match="record" use="concat(COMBINATION_CODE, '|', OperationID)" />
<xsl:template match="/root">
<Show releaseID="5.4.4" xmlns:star="http://www.starstandard.org/STAR/5">
<DataArea>
<LOperations>
<!-- for each distinct OperationID -->
<xsl:for-each select="records/record[count(. | key('opcode', OperationID)[1]) = 1]" >
<LOperationsDetail>
<LOperationID><xsl:value-of select="OperationID"/></LOperationID>
<!-- for each distinct COMBINATION_CODE in the current group -->
<xsl:for-each select="key('opcode', OperationID)[count(. | key('combination', concat(COMBINATION_CODE, '|', OperationID))[1]) = 1]" >
<Combinations>
<combinationCode><xsl:value-of select="COMBINATION_CODE"/></combinationCode>
<!-- get current sub-group -->
<xsl:for-each select="key('combination', concat(COMBINATION_CODE, '|', OperationID))">
<VLaborAllowance xmlns:star="http://www.starstandard.org/STAR/5" >
<VIGroup>
<GID><xsl:value-of select="AllGroupID" /></GID>
<VID><xsl:value-of select="AllID" /></VID>
</VIGroup>
</VLaborAllowance>
</xsl:for-each>
</Combinations>
</xsl:for-each>
</LOperationsDetail>
</xsl:for-each>
</LOperations>
</DataArea>
</Show>
</xsl:template>
</xsl:stylesheet>
这篇关于基于 xsl 1.0 中的条件分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!