2016-11-10 5 views
0

Ich habe diese beiden Ausdrücke, und ich möchte, dass sie in der gleichen Tisch legen und sie zu zählen, damit ich so etwas wie dieses:Zählen in XSL: FO

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

Aber ich bin immer diese Ausgabe:

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

Geben Sie den Eingang ein. –

+0

Die Eingabe erfolgt über die GUI bei der Auswahl von Ersatz- und Reparaturteilen. Ich kann XML nur dort veröffentlichen, wo die Daten extrahiert werden: –

+0

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

Antwort

0

sollten Sie xsl:number anstelle von position(). Die Funktion position() gibt nur eine Zahl zurück, die der Kontextposition entspricht. Das xsl:number Element bietet viel mehr Funktionalität.

Ich würde auch vorschlagen, die genannte Vorlage mit dem 2 xsl:for-each mit einem xsl:apply-templates und das Hinzufügen einer einzigen Vorlage ersetzt sowohl PartDtl und IDtl anzupassen.

Beispiel ...

XML-Eingabe (Modified, weil Sie Ihre Eingabe ist nicht wohlgeformt ist. Ist CalcData ein Kind Geschwister von CLASSXml? Ich machte es ein Kind, sondern schrieb das XSLT so würde es entweder arbeiten Art und Weise.)

<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 Ausgang

<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> 

Rendered PDF (Gebraucht FOP 1,1)

enter image description here

Für ein weiteres Beispiel, hier sind zwei Ersatz-Vorlagen, die eine aktuelle Liste anstelle einer Tabelle erstellen . (Beachten Sie, dass das Leerzeichen im Attribut format entfernt wird.) Das gerenderte PDF sieht genauso aus wie oben.

<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>