根据current-group()

时间:2016-08-27 19:36:24

标签: xml xslt

更改xmls以使其更接近所需的实际转换。

请考虑下面的xml:

<?xml version="1.0"?>
<ns0:MyFile xmlns:ns0="http://myfile.com">
  <RECORDS>
     <Detail>
        <Groupby>Group1</Groupby>
        <Cond>10</Cond>
        <AddMe>200</AddMe>
    </Detail>
    <Detail>
        <Groupby>Group1</Groupby>
        <Cond>12</Cond>
         <AddMe>1200</AddMe>
    </Detail>
     <Detail>
        <Groupby>Group1</Groupby>
        <Cond>22</Cond>
         <AddMe>1200</AddMe>
    </Detail>
    <Detail>
        <Groupby>Group1</Groupby>
         <Cond>11</Cond>
         <AddMe>200</AddMe>
    </Detail>
    <Detail>
         <Groupby>Group2</Groupby>
         <Cond>11</Cond>
        <AddMe>230</AddMe>
    </Detail>
     <Detail>
        <Groupby>Group2</Groupby>
        <Cond>12</Cond>
        <AddMe>1200</AddMe>
    </Detail>
    <Detail>
         <Groupby>Group2</Groupby>
         <Cond>10</Cond>
        <AddMe>200</AddMe>
     </Detail>
     <Detail>
        <Groupby>Group2</Groupby>
        <Cond>22</Cond>
        <AddMe>1300</AddMe>
     </Detail>
  </RECORDS>
</ns0:MyFile>

期望的结果:

<?xml version="1.0"?>
<ns0:ResultFile xmlns:ns0="http://myfile.com">
 <Results>
    <Detail>    <!--Based on ((Cond = 10) or (Cond = 11)) for Group1 -->
        <Constant0>RIDE</Constant0> 
        <Group>Group1</Group>
        <Added>400</Added>
        <Constant1>BIKE</Constant1>
    </Detail>
    <Detail>        <!--Based on (OTHERS, any OTHER Cond) for Group1 -->
        <Constant0>RIDE</Constant0>
        <Group>Group1</Group>
        <Added>2400</Added>
        <Constant1>CAR</Constant1>
    </Detail>
    <Detail>    <!--Based on ((Cond = 10) or (Cond = 11)) for Group2 -->
        <Constant0>RIDE</Constant0> 
        <Group>Group1</Group>
        <Added>430</Added>
        <Constant1>BIKE</Constant1>
    </Detail>
    <Detail>        <!--Based on (OTHERS, any OTHER Cond) for Group2 -->
        <Constant0>RIDE</Constant0>
        <Group>Group1</Group>
        <Added>2500</Added>
        <Constant1>CAR</Constant1>
    </Detail>
 </Results>

按照迈克尔提供的上一个解决方案,我使用了以下代码:

<?xml version='1.0' ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://myfile.com">
<xsl:template match="/">
    <ns0:ResultFile xmlns:ns0="http://myfile.com">
        <Results>
            <xsl:choose>
                <xsl:when test="((//Cond = 11) or (//Cond = 10))">
                    <xsl:for-each-group select="ns0:MyFile/RECORDS/Detail" group-by="concat(Groupby, '|', ((Cond = 11) or (Cond = 10 )))">

                <xsl:value-of select="(current-group())"/>
                        <Detail>
                            <Constant0>RIDE</Constant0>
                            <xsl:copy-of select="Groupby"/>
                    <AddMe>
                        <xsl:value-of select="sum(current-group()/AddMe)"/>
                    </AddMe>
                            <Constant1>BIKE</Constant1>
                        </Detail>
                    </xsl:for-each-group>
                </xsl:when>
                <xsl:otherwise>

                <xsl:value-of select="name(.)"/>
                        <Detail>
                            <Constant0>RIDE</Constant0>
                            <Constant1>CAR</Constant1>
                        </Detail>
                </xsl:otherwise>
            </xsl:choose>
        </Results>
    </ns0:ResultFile>
</xsl:template>

结果我得到的总和很好,但后来我不能在组段中有两个单独的常量。结果如下:

<?xml version="1.0"?>
<ns0:ResultFile xmlns:ns0="http://myfile.com">
<Results>
    <Detail>
        <Constant0>RIDE</Constant0>
        <Groupby>Group1</Groupby>
        <AddMe>400</AddMe>
        <Constant1>BIKE</Constant1>
    </Detail>
     <Detail>
        <Constant0>RIDE</Constant0>
        <Groupby>Group1</Groupby>
        <AddMe>2400</AddMe>
        <Constant1>BIKE</Constant1>
    </Detail>
    <Detail>
        <Constant0>RIDE</Constant0>
        <Groupby>Group2</Groupby>
        <AddMe>430</AddMe>
        <Constant1>BIKE</Constant1>
    </Detail>
    <Detail>
        <Constant0>RIDE</Constant0>
        <Groupby>Group2</Groupby>
        <AddMe>2500</AddMe>
        <Constant1>BIKE</Constant1>
    </Detail>
</Results>

1 个答案:

答案 0 :(得分:0)

  

无法将变量传递给xslt函数来执行求和   另一个为每个人。

当你可以做的时候,我不明白为什么你需要(或想要)做这样的事情:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://myfile.com">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/ns0:MyFile">
    <ns0:ResultFile>
        <Results>
            <xsl:for-each-group select="RECORDS/Detail" group-by="concat(Gorupby, '|' , Cond)">
                <xsl:copy>
                    <xsl:copy-of select="Gorupby"/>
                    <AddMe>
                        <xsl:value-of select="sum(current-group()/AddMe)" />
                    </AddMe>
                    <xsl:copy-of select="Cond"/>
                </xsl:copy>
            </xsl:for-each-group>
        </Results>
    </ns0:ResultFile>
</xsl:template>

</xsl:stylesheet>

加了:

要根据您的新要求进行调整,请更改:

<xsl:for-each-group select="RECORDS/Detail" group-by="concat(Gorupby, '|' , Cond)">

为:

<xsl:for-each-group select="RECORDS/Detail" group-by="string-join((Groupby, string(Cond=10 or Cond=11)), '|')">