我需要根据操作ID进行分组,然后根据操作ID中的COMBINATION_CODE再次分组。但COMBINATION_CODE也可能有空标签。仅当值存在于COMBINATION_CODE中时,下面的样式表才会按预期COMBINATION_CODE分组。如果存在空标记,则无论操作 ID 如何,它都会将所有空COMBINATION_CODE记录分组在一起。请找到示例输入:
<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
:
<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>