0

我有以下简化的 XML 结构:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<DataSet>
    <Tables>
        <Table>
            <Rows>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9991</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9992</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9994</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9995</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue/>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
                </R>
            </Rows>
        </Table>
    </Tables>
</DataSet>
</ExportData>

我需要根据“Qualifierstring”和“revenueCenter”对记录集(R)进行分组。我想出了这个完美运行的 XSLT:

<?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" indent="yes"/>
<xsl:template match="text()|@*"/>

<!-- define the key to define unique elements -->
<xsl:key name="keyTranstypeCcRc" match="R" use="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>

<!-- define which elements are unique -->
<xsl:template match="ExportData/DataSet/Tables/Table/Rows">
    <xsl:variable name="uniqueTransactions" select="R[generate-id()=generate-id(key('keyTranstypeCcRc',concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))[1])]"/>
    <ExportData>
        <xsl:apply-templates select="$uniqueTransactions" mode="group"/>
    </ExportData>
</xsl:template>

<!-- create the unique groups -->
<xsl:template match="R" mode="group">
    <transaction>
        <xsl:attribute name="transactionType"><!-- write the 2 key criteria into this attribute -->
            <xsl:value-of select="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>
        </xsl:attribute>
        <xsl:apply-templates select="key('keyTranstypeCcRc', concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))" mode="item"/>
    </transaction>
</xsl:template>

<!-- write the item content into each group -->
<xsl:template match="R" mode="item">
    <R>
        <xsl:copy-of select="child::*"/> 
    </R>
</xsl:template>

</xsl:stylesheet>

我定义一个键并选择唯一的项目,构建组并处理记录集 (R)。

但是现在我如何定义密钥以便将“revenueCenter”9992 和 9993 组合在一起?我必须设置2个不同的键吗?(但我怎么能在我的模板中使用 2 个键)或者我可以用值扩展现有的键吗?

XML 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<transaction transactionType="Sales|9991">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9991</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9993 Sales|9992">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9992</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9994">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9994</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9995">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9995</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue/>
    </R>
</transaction>
</ExportData>

2012 年 5 月 24 日更新: 我实际上想出了一个 XSLT,它可以满足我的需求,我使用了两个不同的键:

<?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" indent="yes"/>
<xsl:template match="text()|@*"/>


<!-- define the key to define unique elements -->
<!--<xsl:key name="keyTranstypeCcRc" match="R" use="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>-->
<xsl:key name="keyTranstypeCcRc" match="R[not(revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993')]" 
    use="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>

<xsl:key name="keyTranstypeCcRc99923" match="R[revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993']" 
    use="transactionQualifierstring"/>

<!-- define which elements are unique -->
<xsl:template match="ExportData/DataSet/Tables/Table/Rows">
    <xsl:variable name="uniqueTransactions" select="R[not(revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993')]
        [generate-id()=generate-id(key('keyTranstypeCcRc',concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))[1])]"/>
    <xsl:variable name="keyTranstypeCcRc99923" select="R[revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993']
        [generate-id()=generate-id(key('keyTranstypeCcRc99923',transactionQualifierstring)[1])]"/>
    <ExportData>
        <xsl:apply-templates select="$uniqueTransactions" mode="group"/>
        <xsl:apply-templates select="$keyTranstypeCcRc99923" mode="group99923"/>
    </ExportData>
</xsl:template>

<!-- create the unique groups -->
<xsl:template match="R" mode="group">
    <transaction>
        <xsl:attribute name="transactionType"><!-- write the 2 key criteria into this attribute -->
            <xsl:value-of select="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>
        </xsl:attribute>
        <xsl:apply-templates select="key('keyTranstypeCcRc', concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))" mode="item"/>
        
    </transaction>
</xsl:template>

<!-- create the unique groups -->
<xsl:template match="R" mode="group99923">
    <transaction>
        <xsl:attribute name="transactionType"><!-- write the 2 key criteria into this attribute -->
            <xsl:value-of select="concat(transactionQualifierstring,'|','9992','|','9993')"/>
        </xsl:attribute>
        <xsl:apply-templates select="key('keyTranstypeCcRc99923', transactionQualifierstring)" mode="item"/>
        
    </transaction>
</xsl:template>

<!-- write the item content into each group -->
<xsl:template match="R" mode="item">
    <R>
        <xsl:copy-of select="child::*"/> 
    </R>
</xsl:template>

</xsl:stylesheet>

应用到源 XML 我得到这个正确的输出:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<transaction transactionType="Sales|9991">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9991</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9994">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9994</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9995">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9995</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue/>
    </R>
</transaction>
<transaction transactionType="Sales|9992|9993">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9992</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
</transaction>
</ExportData>

该解决方案有效,因为我知道我需要分别排除和分组哪些值。

4

1 回答 1

1

每当您希望以特定方式对事物进行排序和/或分组时,考虑生成“排序键”会有所帮助。在您生成索引的地方

<xsl:key name="keyTranstypeCcRc" match="R" use="makeKey(...)"/>

您需要提供一个从适当的参数生成所需密钥的函数。然后在索引中查找键时使用相同的函数。

在 XSLT 1 中,您可以使用 EXSLTfunc:function以可重用的方式打包逻辑。

如果您熟悉 Java,这有点像定义一个自定义Comparator.

于 2012-05-16T22:05:56.290 回答