2011-01-09 68 views
2

我想寫入xslt來獲取輸入xml的響應。提前致謝。我想將產出分組,以便每個員工詳細信息不重複月份組合。按月份和年份組合的組響應

輸入XML:

<resultset> 
    <row> 
     <column> 
      <name>Month</name> 
      <value>2</value> 
     </column> 
     <column> 
      <name>Year</name> 
      <value>2010</value> 
     </column> 
     <column> 
      <name>EmpName</name> 
      <value>Anu</value> 
     </column> 
     <column> 
      <name>Age</name> 
      <value>24</value> 
     </column> 
    </row> 
    <row> 
     <column> 
      <name>Month</name> 
      <value>2</value> 
     </column> 
     <column> 
      <name>Year</name> 
      <value>2010</value> 
     </column> 
     <column> 
      <name>EmpName</name> 
      <value>Nancy</value> 
     </column> 
     <column> 
      <name>Age</name> 
      <value>26</value> 
     </column> 
    </row> 
    <row> 
     <column> 
      <name>Month</name> 
      <value>3</value> 
     </column> 
     <column> 
      <name>Year</name> 
      <value>2010</value> 
     </column> 
     <column> 
      <name>EmpName</name> 
      <value>Ned</value> 
     </column> 
     <column> 
      <name>Age</name> 
      <value>25</value> 
     </column> 
    </row> 
</resultset> 

輸出預計:

<Response> 
    <PeriodInfo> 
     <Month>2</Month> 
     <Year>2010</Year> 
     <EmployeeDetails> 
      <Name>Anu</Name> 
      <Age>24</Age> 
     </EmployeeDetails> 
     <EmployeeDetails> 
      <Name>Nancy</Name> 
      <Age>26</Age> 
     </EmployeeDetails> 
    </PeriodInfo> 
    <PeriodInfo> 
     <Month>3</Month> 
     <Year>2010</Year> 
     <EmployeeDetails> 
      <Name>Ned</Name> 
      <Age>25</Age> 
     </EmployeeDetails> 
    </PeriodInfo> 
</Response> 

回答

2

定義鍵 「行到月」,其按年份和月份索引行。

然後使用Muenchian分組在輸入中選擇唯一的年份組合。 「行」模板每個應用一次。

要生成「EmployeeDetails」輸出,請使用key()選擇年份與當前摘要行相同的所有行。

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

    <xsl:output method="xml" indent="yes" /> 

    <xsl:key name="row-by-month" match="row" 
      use="concat(column[name='Year']/value,'-', 
         column[name='Month']/value)" /> 

    <xsl:template match="resultset"> 
     <Response> 
      <xsl:apply-templates 
       select="row[generate-id(.) = 
          generate-id(key('row-by-month', 
              concat(column 
                 [name='Year'] 
                 /value, 
                '-', 
                column 
                 [name='Month'] 
                 /value))[1])]"/> 
     </Response> 
    </xsl:template> 

    <xsl:template match="row"> 
     <PeriodInfo> 
      <Year> 
       <xsl:value-of select="column[name='Year']/value"/> 
      </Year> 
      <Month> 
       <xsl:value-of select="column[name='Month']/value"/> 
      </Month> 
      <xsl:apply-templates select="key('row-by-month', 
              concat(column 
                 [name='Year'] 
                 /value, 
                '-', 
                column 
                 [name='Month'] 
                 /value))" 
           mode="details"/> 
     </PeriodInfo> 
    </xsl:template> 

    <xsl:template match="row" mode="details"> 
     <EmployeeeDetails> 
      <Name> 
       <xsl:value-of select="column[name='EmpName']/value"/> 
      </Name> 
      <Age> 
       <xsl:value-of select="column[name='Age']/value"/> 
      </Age> 
     </EmployeeeDetails> 
    </xsl:template> 


</xsl:stylesheet> 
+0

+1好答案。 – 2011-01-09 22:59:16

0

這裏是@基本上更可讀的變體拉克倫-羅氏溶液

<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="kRowByDate" match="row" 
    use="concat(column[name='Year']/value, 
      '+', 
      column[name='Month']/value)"/> 

<xsl:template match="/*"> 
    <Response> 
    <xsl:apply-templates/> 
    </Response> 
</xsl:template> 

<xsl:template match= 
    "row[generate-id() 
     = 
     generate-id(key('kRowByDate', 
         concat(column[name='Year']/value, 
           '+', 
           column[name='Month']/value) 
         )[1] 
        ) 
     ] 
    "> 
    <xsl:variable name="vMonth" select="column[name='Month']/value"/> 
    <xsl:variable name="vYear" select="column[name='Year']/value"/> 
    <PeriodInfo> 
    <Month><xsl:value-of select="$vMonth"/></Month> 
    <Year><xsl:value-of select="$vYear"/></Year> 
    <xsl:apply-templates mode="data" 
     select="key('kRowByDate', concat($vYear,'+',$vMonth))"/> 
    </PeriodInfo> 
</xsl:template> 

<xsl:template match="row"/> 

<xsl:template match="row" mode="data"> 
    <EmployeeDetails> 
    <Name><xsl:value-of select="column[name='EmpName']/value"/></Name> 
    <Age><xsl:value-of select="column[name='Age']/value"/></Age> 
    </EmployeeDetails> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔施加:

<resultset> 
    <row> 
     <column> 
      <name>Month</name> 
      <value>2</value> 
     </column> 
     <column> 
      <name>Year</name> 
      <value>2010</value> 
     </column> 
     <column> 
      <name>EmpName</name> 
      <value>Anu</value> 
     </column> 
     <column> 
      <name>Age</name> 
      <value>24</value> 
     </column> 
    </row> 
    <row> 
     <column> 
      <name>Month</name> 
      <value>2</value> 
     </column> 
     <column> 
      <name>Year</name> 
      <value>2010</value> 
     </column> 
     <column> 
      <name>EmpName</name> 
      <value>Nancy</value> 
     </column> 
     <column> 
      <name>Age</name> 
      <value>26</value> 
     </column> 
    </row> 
    <row> 
     <column> 
      <name>Month</name> 
      <value>3</value> 
     </column> 
     <column> 
      <name>Year</name> 
      <value>2010</value> 
     </column> 
     <column> 
      <name>EmpName</name> 
      <value>Ned</value> 
     </column> 
     <column> 
      <name>Age</name> 
      <value>25</value> 
     </column> 
    </row> 
</resultset> 

想要的,正確的結果產生

<Response> 
    <PeriodInfo> 
     <Month>2</Month> 
     <Year>2010</Year> 
     <EmployeeDetails> 
     <Name>Anu</Name> 
     <Age>24</Age> 
     </EmployeeDetails> 
     <EmployeeDetails> 
     <Name>Nancy</Name> 
     <Age>26</Age> 
     </EmployeeDetails> 
    </PeriodInfo> 
    <PeriodInfo> 
     <Month>3</Month> 
     <Year>2010</Year> 
     <EmployeeDetails> 
     <Name>Ned</Name> 
     <Age>25</Age> 
     </EmployeeDetails> 
    </PeriodInfo> 
</Response>