第一篇文章後,看了很多來自社區的真棒建議。嵌套爲每個組呼叫增加員
我在XSLT 2.0中有三個字段,都在同一級別(肩膀,膝蓋和腳趾)。我需要根據肩膀和膝蓋的獨特組合輸出腳趾總數,所以我創建了兩個嵌套的for-each-groups。在每個輸出上,我還需要輸出一個從1增加到肩膀和膝蓋的獨特組合的數量。
這個增量是我遇到的問題。我最近來的是通過調用position(),但如果我在最內層的組中調用它,計數器會在每個獨特的肩膀處重置。如果我在最外面的組中稱呼它,每一個獨特肩膀內的膝蓋都會獲得相同的價值,然後它會重置在每個獨特的肩膀上。如果我完全將它稱爲組外,它永遠不會超過1.我也嘗試過使用xsl:number,keys等等,但無濟於事。在這些情況下,正確的行數仍在打印,但增量值正在查看單個非分組值。
我讀了一個關於模板之間的「隧道」值的建議,但我一直沒有能夠得到這個工作,主要是因爲我不認爲我正確調用模板(這些字段是相同的 - 級別而不是父母子女)。有關使這項工作適用於每組或其他方面的想法?提前謝謝了。
示例XML:
<bodies>
<parts>
<shoulders>shoulders1</shoulders>
<knees>knees1</knees>
<toes>1</toes>
</parts>
<parts>
<shoulders>shoulders2</shoulders>
<knees>knees2</knees>
<toes>2</toes>
</parts>
<parts>
<shoulders>shoulders1</shoulders>
<knees>knees2</knees>
<toes>10</toes>
</parts>
<parts>
<shoulders>shoulders2</shoulders>
<knees>knees1</knees>
<toes>10</toes>
</parts>
<parts>
<shoulders>shoulders1</shoulders>
<knees>knees1</knees>
<toes>9</toes>
</parts>
<parts>
<shoulders>shoulders2</shoulders>
<knees>knees2</knees>
<toes>8</toes>
</parts>
</bodies>
樣品XSLT:
<xsl:stylesheet exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:this="urn:this-stylesheet" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:for-each-group select="bodies/parts" group-by="shoulders">
<xsl:for-each-group select="current-group()" group-by="knees">
<xsl:value-of select="shoulders"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="knees"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="sum(current-group()/toes)"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>. </xsl:text>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
結果輸出:
shoulders1,knees1,10,1 shoulders1,knees2,10,2 shoulders2,knees2 ,10,1。肩膀2,膝蓋1,10,2。
所需輸出:
shoulders1,knees1,10,1 shoulders1,knees2,10,2 shoulders2,knees2,10,3 shoulders2,knees1,10,4