XSLT在第二级没有分组

时间:2013-11-24 09:57:42

标签: xslt muenchian-grouping

我需要使用xslt转换一些数据。基本上,我需要按客户分组信息。之后,所有日期和时间应按客户级别显示时间值。我必须提到,我不必只显示不同的值。     

<XMLLINE>

<Customer>1</Customer>

<Day> Monday </Day>

<Hour> 10:00 <Hour>

 </XMLLine>

<XMLLINE>

<Customer>2</Customer>

<Day> Monday</Day>

<Hour> 12:00 <Hour>

 </XMLLine>

<XMLLINE>

<Customer>1</Customer>

<Day> Tuesday</Day>

<Hour> 12:00 <Hour>

 </XMLLine>

</XMLines>

输出应该看起来像

<Clients>

<Customer>

<Id> 1 </Id>

<Days>

<Day> Monday </Day>

<Hour> 10:00 <Hour>

<Day> Tuesday</Day>

<Hour> 12:00 <Hour>

</Days>

</Customer>

<Customer>

<Id> 2 </Id>

<Days>

<Day> Monday </Day>

<Hour> 12:00 <Hour>

</Days>

</Customer>

</Clients>

非常感谢你们

1 个答案:

答案 0 :(得分:0)

只需与Muenchian分组合并,定义一个密钥,然后使用key函数查找组中的所有项目:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="c-by-id" match="XMLLINE" use="Customer"/>

<xsl:template match="XMLines">
  <Clients>
    <xsl:apply-templates select="XMLLINE[generate-id() = generate-id(key('c-by-id', Customer)[1])]"/>
  </Clients>
</xsl:template>

<xsl:template match="XMLLINE">
  <Customer>
    <Id><xsl:value-of select="Customer"/></Id>
    <Days>
      <xsl:copy-of select="key('c-by-id', Customer)/*[self::Day | self::Hour]"/>
    </Days>
  </Customer>
</xsl:template>

</xsl:stylesheet>

变换

<XMLines>
<XMLLINE>

<Customer>1</Customer>

<Day> Monday </Day>

<Hour> 10:00 </Hour>

 </XMLLINE>

<XMLLINE>

<Customer>2</Customer>

<Day> Monday</Day>

<Hour> 12:00 </Hour>

 </XMLLINE>

<XMLLINE>

<Customer>1</Customer>

<Day> Tuesday</Day>

<Hour> 12:00 </Hour>

 </XMLLINE>

</XMLines>

<Clients>
   <Customer>
      <Id>1</Id>
      <Days>
         <Day> Monday </Day>
         <Hour> 10:00 </Hour>
         <Day> Tuesday</Day>
         <Hour> 12:00 </Hour>
      </Days>
   </Customer>
   <Customer>
      <Id>2</Id>
      <Days>
         <Day> Monday</Day>
         <Hour> 12:00 </Hour>
      </Days>
   </Customer>
</Clients>