按照我之前的问题:Sort complex XML structure by nested attribute using XSLT(设置不当)道歉。
我很好奇你将如何仅列出标准销售的十大地点。
这可以在apply-templates中使用,因为我使用它有困难
<xsl:if test="position() <= 10">
答案 0 :(得分:2)
在谓词中定义它,例如:
<xsl:apply-templates select="item[position() <= 10]" />
答案 1 :(得分:2)
<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() <= 10]"/>
</xsl:copy>
</xsl:template>
答案 2 :(得分: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>
<强>解释强>:
要处理的所需顶级元素数量在global / external参数$pTopN
中指定。这样,当在变换之外设置此参数时,转换可以用于任何想要的数字,而无需任何修改。
关键时刻位于xsl:apply-templates
,其中有xsl:sort
个孩子。这会导致模板应用于已排序的节点列表。
在匹配模板中,有一个条件可以将position()
与$pTopN
参数进行比较,并且仅当当前位置未超过该数量时才执行处理。