1

我的 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 个月,姓 Shah。

所以我的输出 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 的值并计算行数。

4

2 回答 2

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 分组方法。

于 2012-08-10T01:37:59.757 回答
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>
于 2012-08-10T01:03:14.160 回答