Domanda

Al lavoro mi è stato dato il compito divertente di generare PDF con XSL. La struttura XML con cui sto lavorando è simile a

<records>
    <topLevel>
        <topLevelID></topLevelID>
        <secondLevel>
            <secondLevelID></secondLevelID>
            <thirdLevel>
            </thirdLevel>
            <thirdLevel>
            </thirdLevel>
        </secondLevel>
    </topLevel>
    <topLevel>
        <topLevelID></topLevelID>
        <secondLevel>
            <secondLevelID></secondLevelID>
            <thirdLevel>
            </thirdLevel>
            <thirdLevel>
            </thirdLevel>
        </secondLevel>
    </topLevel>
</records>

Proverei a dare un esempio più significativo dell'XML, ma non mi sento di avvicinarmi ai confini legali che potrebbero esistere. Con quella struttura XML, devo generare un blocco di testo nel PDF per ogni thirdLevel nodo. L'XSL che ho finora è come

<xsl:for-each select ="topLevel">          
    <xsl:variable name="topID" select="topLevelID"/>
    <xsl:for-each select ="secondLevel">
        <xsl:variable name="secondID" select="secondLevelID"/>
        <xsl:for-each select="thirdLevel">            
            <fo:block-container position="absolute" height="12.8pt" width="220.8pt" left="160pt" display-align="auto">
                <xsl:attribute name="top">
                    <xsl:value-of select="concat(193 + [whatshouldgohere]), 'pt')"/>
                </xsl:attribute>
                <fo:block font-size="7pt">                          
                    <xsl:call-template name="insertThirdLevel"/>
                </fo:block>
            </fo:block-container>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

Fondamentalmente, devo aggiungere un valore all'attributo in alto per far apparire il testo per ciascun nodo position() sulla sua riga. Ho provato a usare combinazioni di aggiunta / moltiplicazione per l'ID (inizia da 1 e aumenta di 1 per ogni set) e <=>, ma non riesco a farlo bene.

È stato utile?

Soluzione

Penso che dovresti davvero esaminare <xsl:apply-templates>, può farti risparmiare un sacco di battitura.

Versione semplificata:

<xsl:variable name="line-height" select="10" />

<xsl:template match="/records">
  <xsl:apply-templates select="//thirdLevel" />
</xsl:template>

<xsl:template match="thirdLevel">
  <xsl:variable name="top" select="193 + position() * $line-height" />
  <fo:block-container top="{concat($top , 'pt')}">
    <fo:block font-size="7pt">                          
      <xsl:call-template name="insertThirdLevel"/>
    </fo:block>
  </fo:block-container>
</xsl:template>

<xsl:template name="insertThirdLevel">
  Third Level!
</xsl:template>

Output semplificato (" fo " spazio dei nomi escluso):

<fo:block-container top="203pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="213pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="223pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="233pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top