2012-04-02 147 views

回答

2

下面是一個簡單的,完整的,解決方案,這是一單通

<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:param name="pTopN" select="2"/> 

    <xsl:template match="CompanyStats"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates select="CompanyLocation"> 
       <xsl:sort data-type="number" select="UserContent/StandardSales/Sales" order="descending"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="CompanyLocation"> 
     <xsl:if test="not(position() > $pTopN)"> 
     <xsl:copy-of select="."/> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

當這一轉變施加在下面的XML文檔(含有三個CompanyLocation元素):

<Company> 
    <CompanyStats> 
     <CompanyLocation id="London"> 
      <OfficeID>1</OfficeID> 
      <Totalworkers>20</Totalworkers> 
      <NoCleaners>2</NoCleaners> 
      <TopSales> 
       <UserID>4</UserID> 
       <Sales>43</Sales> 
       <Description> Highest sales this quater</Description> 
      </TopSales> 
      <LowestSales> 
       <UserID>12</UserID> 
       <Sales>26</Sales> 
       <Description> Lowest sales this quater</Description> 
      </LowestSales> 
      <UserContent> 
       <ID>4</ID> 
       <FirstName>Jack</FirstName> 
       <Surname>Black</Surname> 
       <StartDate>11/11/2011</StartDate> 
       <StandardSales> 
        <SSID>0</SSID> 
        <Sales>64</Sales> 
        <SalesManager>Steve Hewitt</SalesManager> 
       </StandardSales> 
       <BusinessSales> 
        <BSID>0</BSID> 
        <Sales>64</Sales> 
        <SalesManager>Steve Hewitt</SalesManager> 
       </BusinessSales> 
      </UserContent> 
     </CompanyLocation> 
     <CompanyLocation id="Paris"> 
      <OfficeID>1</OfficeID> 
      <Totalworkers>20</Totalworkers> 
      <NoCleaners>2</NoCleaners> 
      <TopSales> 
       <UserID>4</UserID> 
       <Sales>43</Sales> 
       <Description> Highest sales this quater</Description> 
      </TopSales> 
      <LowestSales> 
       <UserID>12</UserID> 
       <Sales>26</Sales> 
       <Description> Lowest sales this quater</Description> 
      </LowestSales> 
      <UserContent> 
       <ID>4</ID> 
       <FirstName>Jack</FirstName> 
       <Surname>Black</Surname> 
       <StartDate>11/11/2011</StartDate> 
       <StandardSales> 
        <SSID>0</SSID> 
        <Sales>122</Sales> 
        <SalesManager>Steve Hewitt</SalesManager> 
       </StandardSales> 
       <BusinessSales> 
        <BSID>0</BSID> 
        <Sales>64</Sales> 
        <SalesManager>Steve Hewitt</SalesManager> 
       </BusinessSales> 
      </UserContent> 
     </CompanyLocation> 
     <CompanyLocation id="Berlin"> 
      <OfficeID>1</OfficeID> 
      <Totalworkers>20</Totalworkers> 
      <NoCleaners>2</NoCleaners> 
      <TopSales> 
       <UserID>4</UserID> 
       <Sales>43</Sales> 
       <Description> Highest sales this quater</Description> 
      </TopSales> 
      <LowestSales> 
       <UserID>12</UserID> 
       <Sales>26</Sales> 
       <Description> Lowest sales this quater</Description> 
      </LowestSales> 
      <UserContent> 
       <ID>4</ID> 
       <FirstName>Jack</FirstName> 
       <Surname>Black</Surname> 
       <StartDate>11/11/2011</StartDate> 
       <StandardSales> 
        <SSID>0</SSID> 
        <Sales>12</Sales> 
        <SalesManager>Steve Hewitt</SalesManager> 
       </StandardSales> 
       <BusinessSales> 
        <BSID>0</BSID> 
        <Sales>64</Sales> 
        <SalesManager>Steve Hewitt</SalesManager> 
       </BusinessSales> 
      </UserContent> 
     </CompanyLocation> 
    </CompanyStats> 
</Company> 

頂部(在排序之後)其中的兩個被產生到輸出

<CompanyStats> 
    <CompanyLocation id="Paris"> 
     <OfficeID>1</OfficeID> 
     <Totalworkers>20</Totalworkers> 
     <NoCleaners>2</NoCleaners> 
     <TopSales> 
     <UserID>4</UserID> 
     <Sales>43</Sales> 
     <Description> Highest sales this quater</Description> 
     </TopSales> 
     <LowestSales> 
     <UserID>12</UserID> 
     <Sales>26</Sales> 
     <Description> Lowest sales this quater</Description> 
     </LowestSales> 
     <UserContent> 
     <ID>4</ID> 
     <FirstName>Jack</FirstName> 
     <Surname>Black</Surname> 
     <StartDate>11/11/2011</StartDate> 
     <StandardSales> 
      <SSID>0</SSID> 
      <Sales>122</Sales> 
      <SalesManager>Steve Hewitt</SalesManager> 
     </StandardSales> 
     <BusinessSales> 
      <BSID>0</BSID> 
      <Sales>64</Sales> 
      <SalesManager>Steve Hewitt</SalesManager> 
     </BusinessSales> 
     </UserContent> 
    </CompanyLocation> 
    <CompanyLocation id="London"> 
     <OfficeID>1</OfficeID> 
     <Totalworkers>20</Totalworkers> 
     <NoCleaners>2</NoCleaners> 
     <TopSales> 
     <UserID>4</UserID> 
     <Sales>43</Sales> 
     <Description> Highest sales this quater</Description> 
     </TopSales> 
     <LowestSales> 
     <UserID>12</UserID> 
     <Sales>26</Sales> 
     <Description> Lowest sales this quater</Description> 
     </LowestSales> 
     <UserContent> 
     <ID>4</ID> 
     <FirstName>Jack</FirstName> 
     <Surname>Black</Surname> 
     <StartDate>11/11/2011</StartDate> 
     <StandardSales> 
      <SSID>0</SSID> 
      <Sales>64</Sales> 
      <SalesManager>Steve Hewitt</SalesManager> 
     </StandardSales> 
     <BusinessSales> 
      <BSID>0</BSID> 
      <Sales>64</Sales> 
      <SalesManager>Steve Hewitt</SalesManager> 
     </BusinessSales> 
     </UserContent> 
    </CompanyLocation> 
</CompanyStats> 

說明

  1. 頂端元件對過程的所需數量在全球指定/外部參數$pTopN。通過這種方式,當此參數設置在轉換之外時,轉換可以用於任何想要的數字,無需任何修改。

  2. 關鍵時刻是在xsl:apply-templates,其中有一個xsl:sort孩子。這會導致模板應用於已排序的節點列表。

  3. 在匹配模板中,有一個條件將position()$pTopN參數進行比較,只有當前位置未超過該數字時才執行處理。

2

有困難的謂詞定義這個,比如:

​​
+1

完美優雅的答案謝謝。當我可以 – Nerz 2012-04-02 14:31:10

+0

@Nerz,Ok :) +1然後給你投票。 – 2012-04-02 14:35:24

2

好,normaly我會做這兩個步驟。但在這種簡單的情況下,您可以使用變量作爲臨時步驟。改變你的第一個問題以這種方式得到的模板:

<xsl:template match="CompanyStats"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <!-- sorting of the CompanyLocation in a temporary variable --> 
     <xsl:variable name="CompanyLocationSorted"> 
      <xsl:apply-templates select="CompanyLocation"> 
       <xsl:sort data-type="number" select="UserContent/StandardSales/Sales" order="descending"/> 
      </xsl:apply-templates> 
     </xsl:variable> 
     <!-- copies just the first 10 of the sorted CompanyLocation--> 
     <xsl:copy-of select="$CompanyLocationSorted/CompanyLocation[position() &lt;= 10]"/> 
    </xsl:copy> 
</xsl:template> 
+0

那麼,這節省了我如何實現它來整合排序列表。謝謝! – Nerz 2012-04-02 14:33:34