根据条件进行分组并将它们放入XSLT转换中的单行中

时间:2014-12-07 19:25:50

标签: xslt xslt-2.0 xslt-grouping

我面临着XML转换的问题。我需要转换csv文件中的输出,如同','分隔。

XML:

<Root>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount1>8</amount1>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>6</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Food"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Travel"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12345</ID>
</Employee>
<Type Descriptor="Other"></Type>
<amount>800</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Phone"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Other"></Type>
<amount>8</amount>
</Employees>
<Employees>
<Employee>
<Co_Code>DEEP1</Co_Code>
<ID>12346</ID>
</Employee>
<Type Descriptor="Food"></Type>
<amount>8</amount>
</Employees>
</Root>

目前正在使用XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

<xsl:param name="sep" select="', '"/>

<xsl:output method="text"/>

<xsl:template match="Root">
  <xsl:value-of select="'Co Code', 'ID', 'type', 'amount'" separator="{$sep}"/>
  <xsl:text>&#10;</xsl:text>
  <xsl:for-each-group select="Employees" group-adjacent="Type/@Descriptor">
    <xsl:if test="position() gt 1"><xsl:text>&#10;</xsl:text></xsl:if>
    <xsl:value-of select="Employee/Co_Code, Employee/ID, current-grouping-key(), sum(current-group()/amount)"
      separator="{$sep}"/>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

目前以下列格式生成输出:

Co Code, ID, type, amount
DEEP1, 12345, Phone, 6
DEEP1, 12345, Food, 8
DEEP1, 12345, Travel, 8
DEEP1, 12345, Other, 800
DEEP1, 12346, Phone, 16
DEEP1, 12346, Other, 8
DEEP1, 12346, Food, 8

但我需要将几行安排到列中。 我试图以下面的格式获得输出。 员工电话和食品总和应写在一个单独的列中,对于其他类型,它应该创建一个单独的行,电话和食品列为空白。

Co Code, ID,  Phone , Food, type,  amount
DEEP1, 12345,   6   ,  8  ,       ,
DEEP1, 12345,       ,     , Travel, 8
DEEP1, 12345,               Other, 800
DEEP1, 12346,  16   ,  8  ,       ,
DEEP1, 12346,       ,     , Other, 8

请给我一些想法来实现这一目标。

感谢Martin Honnen帮助我。

1 个答案:

答案 0 :(得分:1)

您可能希望从员工ID

开始分组
<xsl:for-each-group select="Employees"  
                    group-by="Employee/ID">

然后您有一个特定的行输出&#34;食物&#34;和&#34;电话&#34;值

<xsl:value-of select="Employee/Co_Code, 
                      current-grouping-key(), 
                      sum(current-group()[Type/@Descriptor = 'Phone']/amount), 
                      sum(current-group()[Type/@Descriptor = 'Food']/amount), 
                      '', 
                      ''"  separator="{$sep}"/>

在当前群组中,您可以将非&#34; Food&#34;和&#34;电话&#34;按描述符的条目(只有在可以重复描述符的情况下才需要这样做)

 <xsl:for-each-group select="current-group()[not(Type/@Descriptor = ('Phone', 'Food'))]" 
                     group-by="Type/@Descriptor">

然后,这也是为此输出一条直线的直接案例。

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

<xsl:param name="sep" select="', '"/>

<xsl:output method="text"/>

<xsl:template match="Root">
    <xsl:value-of select="'Co Code', 'ID', 'type', 'amount'" separator="{$sep}"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each-group select="Employees" group-by="Employee/ID">
        <xsl:value-of select="Employee/Co_Code, current-grouping-key(), sum(current-group()[Type/@Descriptor = 'Phone']/amount), sum(current-group()[Type/@Descriptor = 'Food']/amount), '', ''"  separator="{$sep}"/>
        <xsl:text>&#10;</xsl:text>
        <xsl:for-each-group select="current-group()[not(Type/@Descriptor = ('Phone', 'Food'))]" group-by="Type/@Descriptor">
            <xsl:value-of select="Employee/Co_Code, '', '', sum(current-group()/amount), current-group()/Type/@Descriptor"  separator="{$sep}"/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each-group>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>