首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xsl 1.0对相似项进行分组

使用xsl 1.0对相似项进行分组
EN

Stack Overflow用户
提问于 2018-12-24 19:02:37
回答 1查看 35关注 0票数 0

我需要根据OPERATION_CODE对记录进行分组。每条记录在组合标记中可以有多个Allowance。如果多个记录有相同的operation_code,我需要所有的余量。我使用的是xsl 1.0。使用这个xsl,我每条记录只有一个Allowance (在示例输入中,第一个记录中的第一个允许值和第二个记录中的第一个允许值)。预期输出是第一条记录和第二条记录中的所有余量,因为operation_code是相同的。

代码语言:javascript
复制
  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>

预期输出:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
 <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>
EN

回答 1

Stack Overflow用户

发布于 2018-12-25 17:24:38

用这种方式试试?

XSLT 1.0

代码语言:javascript
复制
<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>

您现在所拥有的:

代码语言:javascript
复制
<xsl:for-each select="key('opcode' ,OPERATION_CODE)">

选择当前组中的两个record节点,并为每个节点创建一个Allowance节点。在这些节点中,从每个record中的first allowance检索值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53912560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档