2016-11-10 64 views
0

我有这样的两种表达,我想将它们放在同一个表,并指望他们让我有这样的事情:计数在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

选择汽车零件进行更换和修理时,输入是在GUI上完成的。我只能从提取数据的位置发布XML: –

+0

http://stackoverflow.com/questions/5552811/how-to-use-xsl-number-to-count-elements –

回答

0

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

我也建议用xsl:apply-templates替换指定的模板,然后添加一个模板来匹配PartDtlIDtl

例...

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>