XSLT用于计算每个月内匹配字段的行数

时间:2012-08-09 22:26:22

标签: xml xslt

我的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2012-08-01T17:43:31" EndDate="2012-08-01T17:43:03" StartDate="2012-08-01T16:43:03" Version="12.0.12 Build(9)">
<Rowset>
    <Columns>
        <Column Description="FirstName" MaxRange="1" MinRange="0" Name="Plant" SQLDataType="12" SourceColumn="Plant"/>
        <Column Description="Lastname" MaxRange="1" MinRange="0" Name="Process" SQLDataType="12" SourceColumn="Process"/>
        <Column Description="BorninMonth" MaxRange="1" MinRange="0" Name="ScheduleStartTimestamp" SQLDataType="93" SourceColumn="ScheduleStartTimestamp"/>
        <Column Description="BorninWeek" MaxRange="1" MinRange="0" Name="PO" SQLDataType="4" SourceColumn="PO"/>
        <Column Description="Phone" MaxRange="1" MinRange="0" Name="EquipmentName" SQLDataType="-1" SourceColumn="EquipmentName"/>
    </Columns>
    <Row>
        <FirstName>Derek</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Michael</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>2</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Yang</FirstName>
        <Lastname>Patel</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Bruke</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Maulik</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>2</BorninMonth>
        <BorninWeek>2</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Montu</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>3</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>CHintu</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>4</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Pintu</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>4</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Puppy</FirstName>
        <Lastname>Patel</Lastname>
        <BorninMonth>4</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
    <Row>
        <FirstName>Tommy</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>5</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
    </Row>
</Rowset>

现在我想知道在第1个月和姓氏Shah出生的人数。同样的方式出生在第2个月和姓氏Shah。出生于第3个月和姓氏沙阿。

所以我的输出XML看起来像:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2012-08-01T17:43:31" EndDate="2012-08-01T17:43:03" StartDate="2012-08-01T16:43:03" Version="12.0.12 Build(9)">
<Rowset>
    <Columns>
        <Column Description="FirstName" MaxRange="1" MinRange="0" Name="Plant" SQLDataType="12" SourceColumn="Plant"/>
        <Column Description="Lastname" MaxRange="1" MinRange="0" Name="Process" SQLDataType="12" SourceColumn="Process"/>
        <Column Description="BorninMonth" MaxRange="1" MinRange="0" Name="ScheduleStartTimestamp" SQLDataType="93" SourceColumn="ScheduleStartTimestamp"/>
        <Column Description="BorninWeek" MaxRange="1" MinRange="0" Name="PO" SQLDataType="4" SourceColumn="PO"/>
        <Column Description="Phone" MaxRange="1" MinRange="0" Name="EquipmentName" SQLDataType="-1" SourceColumn="EquipmentName"/>
    </Columns>
    <Row>
        <FirstName>Derek</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>3</CountMonth>
    </Row>
    <Row>
        <FirstName>Michael</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>2</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>3</CountMonth>
    </Row>
    <Row>
        <FirstName>Yang</FirstName>
        <Lastname>Patel</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>3</CountMonth>
    </Row>
    <Row>
        <FirstName>Bruke</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>1</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>3</CountMonth>
    </Row>
    <Row>
        <FirstName>Maulik</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>2</BorninMonth>
        <BorninWeek>2</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>1</CountMonth>
    </Row>
    <Row>
        <FirstName>Montu</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>3</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>1</CountMonth>
    </Row>
    <Row>
        <FirstName>CHintu</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>4</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>2</CountMonth>
    </Row>
    <Row>
        <FirstName>Pintu</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>4</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>2</CountMonth>
    </Row>
    <Row>
        <FirstName>Puppy</FirstName>
        <Lastname>Patel</Lastname>
        <BorninMonth>4</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>2</CountMonth>
    </Row>
    <Row>
        <FirstName>Tommy</FirstName>
        <Lastname>Shah</Lastname>
        <BorninMonth>5</BorninMonth>
        <BorninWeek>1</BorninWeek>
        <Phone>925</Phone>
        <CountMonth>1</CountMonth>
    </Row>
</Rowset>

所以我需要一些帮助来编写这样的XML,它会增加Month的值并计算行数。

2 个答案:

答案 0 :(得分:1)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kRowByNameMonth" match="Row"
  use="concat(Lastname, '+', BorninMonth)"/>

 <xsl:template match=
  "Row[Lastname='Shah']
      [generate-id()
      = generate-id(key('kRowByNameMonth',concat('Shah+',BorninMonth))[1])
      ]">
   Born in month: <xsl:value-of select="BorninMonth"/>
   <xsl:value-of select="concat(' Count: ',
                                count(key('kRowByNameMonth',
                                          concat('Shah+',BorninMonth)
                                          )
                                       )
                                )"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的XML文档

    <Rowsets DateCreated="2012-08-01T17:43:31" EndDate="2012-08-01T17:43:03" StartDate="2012-08-01T16:43:03" Version="12.0.12 Build(9)">
    <Rowset>
        <Columns>
            <Column Description="FirstName" MaxRange="1" MinRange="0" Name="Plant" SQLDataType="12" SourceColumn="Plant"/>
            <Column Description="Lastname" MaxRange="1" MinRange="0" Name="Process" SQLDataType="12" SourceColumn="Process"/>
            <Column Description="BorninMonth" MaxRange="1" MinRange="0" Name="ScheduleStartTimestamp" SQLDataType="93" SourceColumn="ScheduleStartTimestamp"/>
            <Column Description="BorninWeek" MaxRange="1" MinRange="0" Name="PO" SQLDataType="4" SourceColumn="PO"/>
            <Column Description="Phone" MaxRange="1" MinRange="0" Name="EquipmentName" SQLDataType="-1" SourceColumn="EquipmentName"/>
        </Columns>
        <Row>
            <FirstName>Derek</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>1</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Michael</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>1</BorninMonth>
            <BorninWeek>2</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Yang</FirstName>
            <Lastname>Patel</Lastname>
            <BorninMonth>1</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Bruke</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>1</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Maulik</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>2</BorninMonth>
            <BorninWeek>2</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Montu</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>3</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>CHintu</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>4</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Pintu</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>4</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Puppy</FirstName>
            <Lastname>Patel</Lastname>
            <BorninMonth>4</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
        <Row>
            <FirstName>Tommy</FirstName>
            <Lastname>Shah</Lastname>
            <BorninMonth>5</BorninMonth>
            <BorninWeek>1</BorninWeek>
            <Phone>925</Phone>
        </Row>
    </Rowset>
</Rowsets>

生成想要的正确结果

   Born in month: 1 Count: 3
   Born in month: 2 Count: 1
   Born in month: 3 Count: 1
   Born in month: 4 Count: 2
   Born in month: 5 Count: 1

<强>解释

使用复合键正确使用 Muenchian grouping method

答案 1 :(得分:0)

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

<xsl:template match="Row">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
  <CountMonth>
    <xsl:value-of select="count(../Row
      [BorninMonth = current()/BorninMonth]
      [Lastname    = current()/Lastname   ]    
       )" /> 
  </CountMonth> 
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>