在XSL中计数:FO

时间:2016-11-10 12:58:36

标签: xml xslt xsl-fo

我有这两个表达式,我想将它们放在同一个表中并对它们进行计数,所以我有类似的东西:

1.  Replacement 1
2.  Replacement 2
3.  Replacement 3
4.  Repair 1
5.  Repair 2
6.  Repair 3

但是我得到了这个输出:

1.  Replacement 1
2.  Replacement 2
3.  Replacement 3
1.  Repair 1
2.  Repair 2
3.  Repair 3

XML

<selectedcalculation>
    <CLASSXml>
        <CalcData>
            <RunDesc>NormalCalc</RunDesc>
            <SpareParts>
               <PartDtls>
                  <PartDtl>
                      <RepTyp>E</RepTyp> 
                      <GId>0281</GId> 
                      <PartDesc>Replacement 1</PartDesc> 
                      <PartNo>8W0 807 065 GRU</PartNo> 
                      <Price Cur="HRK">+3002.92</Price> 
                  </PartDtl>
                  <PartDtl>
                      <RepTyp>E</RepTyp> 
                      <GId>0297</GId> 
                      <PartDesc>Replacement 2</PartDesc> 
                      <PartNo>8W0 807 681 B 9B9</PartNo> 
                      <Price Cur="HRK">+193.01</Price> 
                  </PartDtl>
                  <PartDtl>
                      <RepTyp>E</RepTyp> 
                      <GId>0410</GId> 
                      <PartDesc>REŠETKA HLADNJAKA</PartDesc> 
                      <PartNo>8W0 853 651 3FZ</PartNo> 
                      <Price Cur="HRK">+1640.57</Price>
                  </PartDtl>
               </PartDtls>
            </SpareParts>
            <Labor>
               <IDtls>
                  <IDtl>
                      <RepTyp>I</RepTyp> 
                      <GId>0471</GId> 
                      <PartDesc>Repair 1</PartDesc> 
                      <RepDesc>POPRAVAK</RepDesc> 
                  </IDtl>
                  <IDtl>
                      <RepTyp>I</RepTyp> 
                      <GId>0741</GId> 
                      <PartDesc>Repair 2</PartDesc> 
                      <RepDesc>POPRAVAK</RepDesc> 
                  </IDtl>
                  <IDtl>
                      <RepTyp>I</RepTyp> 
                      <GId>0742</GId> 
                      <PartDesc>Repair 3</PartDesc> 
                      <RepDesc>POPRAVAK</RepDesc> 
                  </IDtl>
               </IDtls>
            </Labor>
        </CalcData>
</selectedcalculation>

XSLT

<xsl:template name="standardParts">
  <xsl:for-each select="selectedcalculation//CalcData[RunDesc = 'NormalCalc']//SpareParts[1]//PartDtl">
    <xsl:variable name="detal" select="GId"/>
    <fo:table-row>
      <fo:table-cell>
        <fo:block text-align="left">
          <xsl:value-of select="position()"/>
          <xsl:value-of select="'. '"/>
          <xsl:value-of select="PartDesc"/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
    </xsl:for-each>

    <xsl:for-each select="selectedcalculation//CalcData[RunDesc = 'NormalCalc']//IDtl">
      <xsl:variable name="detal" select="GId"/>
      <fo:table-row>
        <fo:table-cell>
          <fo:block text-align="left" >
            <xsl:value-of select="position()"/>
            <xsl:value-of select="'. '"/>
            <xsl:value-of select="PartDesc"/>  
          </fo:block>
        </fo:table-cell>
      </fo:table-row>
    </xsl:for-each>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

您应该使用xsl:number代替position()position()函数只返回一个等于上下文位置的数字。 xsl:number元素提供了更多功能。

我还建议使用xsl:for-each替换2 xsl:apply-templates并添加一个模板以匹配PartDtlIDtl。< / p>

示例...

XML输入(已修改,因为您的输入格式不正确。CalcDataCLASSXml的孩子的兄弟姐妹吗?我把它作为一个孩子,但写了XSLT所以它可以以任何方式工作。)

<selectedcalculation>
    <CLASSXml>
        <CalcData>
            <RunDesc>NormalCalc</RunDesc>
            <SpareParts>
                <PartDtls>
                    <PartDtl>
                        <RepTyp>E</RepTyp> 
                        <GId>0281</GId> 
                        <PartDesc>Replacement 1</PartDesc> 
                        <PartNo>8W0 807 065 GRU</PartNo> 
                        <Price Cur="HRK">+3002.92</Price> 
                    </PartDtl>
                    <PartDtl>
                        <RepTyp>E</RepTyp> 
                        <GId>0297</GId> 
                        <PartDesc>Replacement 2</PartDesc> 
                        <PartNo>8W0 807 681 B 9B9</PartNo> 
                        <Price Cur="HRK">+193.01</Price> 
                    </PartDtl>
                    <PartDtl>
                        <RepTyp>E</RepTyp> 
                        <GId>0410</GId> 
                        <PartDesc>REŠETKA HLADNJAKA</PartDesc> 
                        <PartNo>8W0 853 651 3FZ</PartNo> 
                        <Price Cur="HRK">+1640.57</Price>
                    </PartDtl>
                </PartDtls>
            </SpareParts>
            <Labor>
                <IDtls>
                    <IDtl>
                        <RepTyp>I</RepTyp> 
                        <GId>0471</GId> 
                        <PartDesc>Repair 1</PartDesc> 
                        <RepDesc>POPRAVAK</RepDesc> 
                    </IDtl>
                    <IDtl>
                        <RepTyp>I</RepTyp> 
                        <GId>0741</GId> 
                        <PartDesc>Repair 2</PartDesc> 
                        <RepDesc>POPRAVAK</RepDesc> 
                    </IDtl>
                    <IDtl>
                        <RepTyp>I</RepTyp> 
                        <GId>0742</GId> 
                        <PartDesc>Repair 3</PartDesc> 
                        <RepDesc>POPRAVAK</RepDesc> 
                    </IDtl>
                </IDtls>
            </Labor>
        </CalcData>
    </CLASSXml>
</selectedcalculation>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
          <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates select="selectedcalculation"/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="selectedcalculation">
    <fo:table>
      <fo:table-body>
        <xsl:apply-templates select=".//CalcData[RunDesc='NormalCalc']//*[self::PartDtl or self::IDtl]"/>
      </fo:table-body>
    </fo:table>
  </xsl:template>

  <xsl:template match="PartDtl|IDtl">
    <fo:table-row>
      <fo:table-cell>
        <fo:block text-align="left">
          <xsl:number count="PartDtl[ancestor::CalcData[RunDesc='NormalCalc']]|
            IDtl[ancestor::CalcData[RunDesc='NormalCalc']]" 
            level="any" format="1. "/>
          <xsl:value-of select="PartDesc"/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>

</xsl:stylesheet>

XSL-FO输出

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align="left">1. Replacement 1</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align="left">2. Replacement 2</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align="left">3. REŠETKA HLADNJAKA</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align="left">4. Repair 1</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align="left">5. Repair 2</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align="left">6. Repair 3</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

呈现PDF (使用FOP 1.1)

enter image description here

另一个例子,这里有两个替换模板,它们创建一个实际的列表而不是一个表。 (请注意,format属性中的空格已被删除。)渲染的PDF与上面的内容相同。

<xsl:template match="selectedcalculation">
  <fo:list-block provisional-distance-between-starts="24pt"
    space-before=".1in" space-after=".1in">
    <xsl:apply-templates select=".//CalcData[RunDesc='NormalCalc']//*[self::PartDtl or self::IDtl]"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="PartDtl|IDtl">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>
        <xsl:number count="PartDtl[ancestor::CalcData[RunDesc='NormalCalc']]|
          IDtl[ancestor::CalcData[RunDesc='NormalCalc']]" 
          level="any" format="1."/>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
        <xsl:value-of select="PartDesc"/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>