1

我需要使用 xslt 1.0 做一些棘手的映射。只需要对datetime元素的日期部分进行分组。但是该元素包含日期和时间。分组后还需要添加小时。

这是输入:

<Records>
   <Record>
      <empid>1</empid>
      <datetime>10/10/2010 11:11:00 AM</datetime>
      <hours>5</hours>
    </Record>
    <Record>            
      <empid>1</empid>
      <datetime>10/10/2010 10:11:00 AM</datetime>
      <hours>5</hours>
    </Record>
    <Record>   
      <empid>1</empid>           
      <datetime>10/11/2010 11:11:00 AM</datetime>
      <hours>5</hours>
    </Record>
    <Record>   
       <empid>2</empid>
       <datetime>10/10/2010 10:11:00 AM</datetime>
       <hours>2</hours>
    </Record>
    <Record> 
      <empid>2</empid>
      <datetime>10/10/2010 9:11:00 AM</datetime>
      <hours>5</hours>  
     </Record>    
  </Records>   

预期输出为:

  <Records>
      <Record>
       <empid>1</empid>
      <detail>     
         <date>10/10/2010</date>
         <hours>10</hours>
      </detail> 
     <detail>
        <date>10/11/2010</date>
        <hours>5</hours>
     </detail>
    </Record>
    <Record>
       <empid>2</empid>
       <detail>
          <date>10/10/2010</date>
          <hours>7</hours>
       </detail>
    </Record>
 </Records>

感谢任何帮助。

4

1 回答 1

1

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kRecById" match="Record" use="empid"/>
 <xsl:key name="kRecByDateId" match="Record"
          use="concat(empid,'+',substring-before(datetime, ' '))"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "Record[generate-id()=generate-id(key('kRecById', empid)[1])]">
  <Record>
   <xsl:apply-templates select="empid"/>
   <xsl:apply-templates mode="inGroup" select=
    "key('kRecById', empid)
                [generate-id()
                =
                 generate-id(key('kRecByDateId',
                             concat(empid,'+',substring-before(datetime, ' ')))[1])]"/>
  </Record>
 </xsl:template>

 <xsl:template match="Record" mode="inGroup">
   <detail>
     <date><xsl:value-of select="substring-before(datetime, ' ')"/></date>
     <hours><xsl:value-of select=
     "sum(key('kRecByDateId',
              concat(empid,'+',substring-before(datetime, ' '))
              )/hours)"/>
     </hours>
   </detail>
 </xsl:template>
 <xsl:template match="Record"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<Records>
    <Record>
        <empid>1</empid>
        <datetime>10/10/2010 11:11:00 AM</datetime>
        <hours>5</hours>
    </Record>
    <Record>
        <empid>1</empid>
        <datetime>10/10/2010 10:11:00 AM</datetime>
        <hours>5</hours>
    </Record>
    <Record>
        <empid>1</empid>
        <datetime>10/11/2010 11:11:00 AM</datetime>
        <hours>5</hours>
    </Record>
    <Record>
        <empid>2</empid>
        <datetime>10/10/2010 10:11:00 AM</datetime>
        <hours>2</hours>
    </Record>
    <Record>
        <empid>2</empid>
        <datetime>10/10/2010 9:11:00 AM</datetime>
        <hours>5</hours>
    </Record>
</Records>

产生想要的正确结果:

<Records>
   <Record>
      <empid>1</empid>
      <detail>
         <date>10/10/2010</date>
         <hours>10</hours>
      </detail>
      <detail>
         <date>10/11/2010</date>
         <hours>5</hours>
      </detail>
   </Record>
   <Record>
      <empid>2</empid>
      <detail>
         <date>10/10/2010</date>
         <hours>7</hours>
      </detail>
   </Record>
</Records>

说明

两个嵌套的Muenchian grouping,内部使用复合键。

于 2012-12-14T23:50:46.643 回答