XSLT2.0基于三个字段进行分组

时间:2013-07-15 14:58:27

标签: xslt-1.0 xslt-2.0

 <passengergroup>
     <passengerList>
<passDetails>
    <route>LONDON</route>
    <lastname>RAY</lastname>
</passDetails>
<seatDetails>
    <SeatNo>1A</SeatNo>
</seatDetails>
<customervalue>AB</customervalue>
   </passengerList
     <passengerList>
<passDetails>
    <route>LONDON</route>
    <lastname>RAY</lastname>
</passDetails>
<seatDetails>
    <SeatNo>1B</SeatNo>
</seatDetails>
<customervalue>good</customervalue>

     </passengerList
<passengerList>
  <passDetails>
          <route>DELHI</route>
   <lastname>RAY</lastname>
  </passDetails>
        <seatDetails>
     <SeatNo>2C</SeatNo>
   </seatDetails>
   <customervalue>BC</customervalue>
         </passengerList>
         <passengerList>
    <passDetails>
    <route>DELHI</route>
     <lastname>RAY</lastname>
    </passDetails>
    <seatDetails>
        <SeatNo>2D</SeatNo>
    </seatDetails>
    <customervalue>okey</customervalue>

       </passengerList>
   </passengergroup>


  <xsl:for-each select="passengergroup/passengerList">
<xsl:if test="customervalue='good'
    <xsl:value-of select="route"/><xsl:text> </xsl:text>
    <xsl:value-of select="customervalue"/><xsl:text> </xsl:text>
    <xsl:value-of select="seatDetails/SeatNo"/>
  </for-each>

  <xsl:for-each select="passengergroup/passengerList">
        <xsl:if test="customervalue='ok'
<xsl:value-of select="route"/><xsl:text> </xsl:text>
<xsl:value-of select="customervalue"/><xsl:text> </xsl:text>
<xsl:value-of select="seatDetails/SeatNo"/>
   </for-each>

  Output
   It will produce output like this

   LONDON good 1A
   LONDON good 1B
   DELHI okey 2C
   DELHI okey 2D

      But i need the output like this 
  LONDON good 1A 1B
  DELHI okey 2C 2D

如果'LONDON good'重复多次,则必须只打印一次。但是我们必须重复使用'1A 1B 1C 1D 1F 2G等'的座位,使用xslt2.0和MY输出类型是文本。没有必要多次显示项目 我尝试了很多..无法找出解决方案,请帮助我。

1 个答案:

答案 0 :(得分:0)

IMO你的输入xml与所需的输出不对应(例如,只有一个LONDON,客户价值=好。但可能我不太了解你需要什么。但是跟随xslt可以找到一份工作。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text" />

    <xsl:template match="/">
            <xsl:apply-templates select="passengergroup" />
    </xsl:template>

    <xsl:template match="passengergroup">
        <xsl:for-each-group select="passengerList" group-by="concat(passDetails/route, ' ', customervalue)">
            <xsl:value-of select="current-grouping-key()" />
            <xsl:text> </xsl:text>
            <xsl:value-of select="current-group()/seatDetails/SeatNo" separator=" " />
            <xsl:value-of select="'&#10;'" />
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

输入

<?xml version="1.0" encoding="UTF-8"?>
<passengergroup>
    <passengerList>
        <passDetails>
            <route>LONDON</route>
            <lastname>RAY</lastname>
        </passDetails>
        <seatDetails>
            <SeatNo>1A</SeatNo>
        </seatDetails>
        <customervalue>good</customervalue>
    </passengerList>
    <passengerList>
        <passDetails>
            <route>LONDON</route>
            <lastname>RAY</lastname>
        </passDetails>
        <seatDetails>
            <SeatNo>1B</SeatNo>
        </seatDetails>
        <customervalue>good</customervalue>
    </passengerList>
    <passengerList>
        <passDetails>
            <route>DELHI</route>
            <lastname>RAY</lastname>
        </passDetails>
        <seatDetails>
            <SeatNo>2C</SeatNo>
        </seatDetails>
        <customervalue>BC</customervalue>
    </passengerList>
    <passengerList>
        <passDetails>
            <route>DELHI</route>
            <lastname>RAY</lastname>
        </passDetails>
        <seatDetails>
            <SeatNo>2D</SeatNo>
        </seatDetails>
        <customervalue>okey</customervalue>
    </passengerList>
</passengergroup>

它产生以下输出

LONDON good 1A 1B
DELHI BC 2C
DELHI okey 2D