XSLT Generate Dynamic Columns for Apache FOP

佐手、 提交于 2019-12-12 06:49:46

问题


I need to make a dynamic table with 3 columns zones

Is better with an example: I don't know if you are gonna understand this, but the "----" is only to format the table in the post

|--Number--|--Doc--|--Status---------|--Number--|--Doc--|--Status---------|--Number--|--Doc--|--Status--| |-----11------|1111- |-- _____________---------|--22------- |2222 --|______________----------|---- 33----- |3333- | _______________|

|-----44----- |4444- |-- _____________ -------- |------------|----------|----------|---------|--------------|---------|------------|

My XML:

    <Details>
      <Detail>
        <Number>11</Number> 
        <Doc>1111</Doc>
      </Detail>
      <Detail>
        <Number>22</Number> 
        <Doc>2222</Doc> 
      </Detail>
      <Detail>
        <Number>33</Number> 
        <Doc>3333</Doc> 
      </Detail>
      <Detail>
        <Number>44</Number> 
        <Doc>4444</v> 
      </Detail>
    </Details>

I tried to do like following post but I couldn't. XSLT Generate Dynamic Rows and Columns for Apache FOP


回答1:


Here's one way with recursion, I did not add "FO" namespaces but you should be able to get there using this. You could also add a test to fill empty cells if you are inclined.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="Details">
   <table>
    <xsl:call-template name="rows">
        <xsl:with-param name="Details" select="*"/>
    </xsl:call-template>
   </table>
</xsl:template>
<xsl:template name="rows">
    <xsl:param name="Details"/>
    <row>
        <xsl:apply-templates select='$Details[position() &lt; 4]/*'/>
    </row>
    <xsl:if test="$Details[4]">
        <xsl:call-template name="rows">
            <xsl:with-param name="Details" select="$Details[position() &gt; 3]"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
<xsl:template match="Number | Doc">
    <cell>
        <xsl:value-of select="."/>
    </cell>
</xsl:template>
</xsl:stylesheet>

The output is this using your XML above (I added a few more Detail elements to make sure all was working):

<?xml version="1.0" encoding="utf-8"?>
<table>
  <row>
      <cell>11</cell>
      <cell>1111</cell>
      <cell>22</cell>
      <cell>2222</cell>
      <cell>33</cell>
      <cell>3333</cell>
   </row>
   <row>
      <cell>44</cell>
      <cell>4444</cell>
      <cell>5</cell>
      <cell>55</cell>
      <cell>6</cell>
      <cell>66</cell>
   </row>
   <row>
      <cell>7</cell>
      <cell>777</cell>
   </row>
 </table>


来源:https://stackoverflow.com/questions/17179352/xslt-generate-dynamic-columns-for-apache-fop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!