Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL- Trying to select the children by index with bounds

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I am trying to take a node and its children and turning it into a table. When the table has too many columns, I want to split the table on the 25th column. I can't seem to find the bug in my XSL transform. Here is what I have so far

Code:
<xsl:template match="Parent">
		<xsl:variable name="numNums" select="count(ColHeaders/ChildNode)"/>
		<xsl:variable name="numPages" select="count(ColHeaders/ChildNode) div 25"/>

		<xsl:call-template name="ParentPage">
			<xsl:with-param name="MaxPages" select="$numPages"/>
			<xsl:with-param name="NumColHeaders" select="$numNums"/>
			<xsl:with-param name="pageIndex" select="0"/>
		</xsl:call-template>

	</xsl:template>

	<xsl:template name="ParentPage">
		<xsl:param name="MaxPages"/>
		<xsl:param name="NumColHeaders"/>
		<xsl:param name="pageIndex"/>

		<xsl:if test="$pageIndex &lt;= $MaxPages">
			
			<xsl:choose>
				<xsl:when test="$pageIndex &lt; $MaxPages">
					<xsl:call-template name="ParentPageFill">
						<xsl:with-param name="max" select="($pageIndex+1)*25"/>
						<xsl:with-param name="start" select="$pageIndex*25"/>

					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="ParentPageFill">
						<xsl:with-param name="max" select="$NumColHeaders mod 25"/>
						<xsl:with-param name="start" select="$pageIndex*25"/>

					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
			

			

			<xsl:call-template name="ParentPage">
				<xsl:with-param name="MaxPages" select="$MaxPages"/>
				<xsl:with-param name="NumColHeaders" select="$NumColHeaders"/>
				<xsl:with-param name="pageIndex" select="$pageIndex+1"/>
			</xsl:call-template>
		</xsl:if>

	</xsl:template>
	
	<xsl:template name="ParentPageFill">
		<xsl:param name="max"/>
		<xsl:param name="start"/>

		
		<fo:block break-before="page" font-size="16pt" font-family="sans-serif" font-weight="bold" space-before="6pt" space-after="6pt">
			My Big Table
		</fo:block>


		<fo:table table-layout="fixed" width="100%" font-size="8pt" font-family="sans-serif" space-before="0pt" space-after="6pt">
			<fo:table-column column-width="1in"/>

			<fo:table-column column-width="proportional-column-width(1)">
				<xsl:attribute name="number-columns-repeated">
					<xsl:value-of select="$max - $start"/>
				</xsl:attribute>
			</fo:table-column>


			<fo:table-header>
				<fo:table-row>

					<xsl:apply-templates select="ColHeaders">
						<xsl:with-param name="NumMax" select="$max"/>
						<xsl:with-param name="NumStart" select="$start"/>
					</xsl:apply-templates>
						
					
				</fo:table-row>
			</fo:table-header>

			<fo:table-body>
				<xsl:apply-templates select="Program">
						<xsl:with-param name="NumMax" select="$max"/>
						<xsl:with-param name="NumStart" select="$start"/>
				</xsl:apply-templates>
			</fo:table-body>
		</fo:table>
	
	</xsl:template>

	<xsl:template match="Parent/ColHeaders">
		<xsl:param name="NumMax"/>
		<xsl:param name="NumStart"/>
		<xsl:variable name="Id" select="@Id" />
				<fo:table-cell  padding="1pt" border="1pt solid black">
					<fo:block font-weight="bold" text-align="center">
						Row Number
					</fo:block>
				</fo:table-cell>
			<xsl:call-template name="ChildNodeHeader">
				<xsl:with-param name="MaxNum" select="$NumMax"/>
				<xsl:with-param name="Start" select="$NumStart"/>
				<xsl:with-param name="Index" select="0"/>
				<xsl:with-param name ="NextChild" select="child::ChildNode"/>
			</xsl:call-template>
	</xsl:template>

	<xsl:template name="ChildNodeHeader">
		<xsl:param name="MaxNum"/>
		<xsl:param name="Start"/>
		<xsl:param name="Index"/>
		<xsl:param name ="NextChild"/>
		<xsl:if test="$Index &lt; $MaxNum">
			<xsl:if test="$Index &gt; $Start">
				<fo:table-cell padding="1pt" border="1pt solid black">
					<fo:instream-foreign-object content-height="3in">
						<svg:svg xmlns:svg="[URL unfurl="true"]http://www.w3.org/2000/svg"[/URL] width="20px" height="210px">
							<svg:text x="10" y="210" font-family="Arial, Helvetica, sans-serif" font-size="8" 
								fill="black" font-weight="bold" text-anchor="start" transform="rotate(-90,8,210)">
								<xsl:value-of select="$NextChild/@SomeAttrib"/> Test
							</svg:text>
						</svg:svg>
					</fo:instream-foreign-object>
					
				</fo:table-cell>
			</xsl:if>
			<xsl:call-template name="ChildNodeHeader">
				<xsl:with-param name="MaxNum" select="$MaxNum"/>
				<xsl:with-param name="Start" select="$Start"/>
				<xsl:with-param name="Index" select="$Index+1"/>
				<xsl:with-param name ="NextChild" select="$NextChild/following-sibling"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="Parent/Program">
		<xsl:param name="NumMax"/>
		<xsl:param name="NumStart"/>
		<xsl:variable name="Id" select="@Id" />
		<fo:table-row>
			<fo:table-cell padding="1pt" border="1pt solid black" >
				<fo:block text-align="center">
					<xsl:value-of select="@Id"/>
				</fo:block>
			</fo:table-cell>
			<xsl:call-template name="Children">
				<xsl:with-param name="MaxNum" select="$NumMax"/>
				<xsl:with-param name="Start" select="$NumStart"/>
				<xsl:with-param name="Index" select="0"/>
				<xsl:with-param name ="NextChild" select="child::ChildNode"/>
			</xsl:call-template>
		</fo:table-row>
	</xsl:template>

	<xsl:template name="Children">
		<xsl:param name="MaxNum"/>
		<xsl:param name="Start"/>
		<xsl:param name="Index"/>
		<xsl:param name ="NextChild"/>
		<xsl:if test="$Index &lt; $MaxNum">
			<xsl:if test="$Index &gt; $Start">
				<fo:table-cell padding="1pt" border="1pt solid black" >
					<xsl:if test="string($NextChild/.)">
						<fo:block text-align="center">
							X
						</fo:block>
					</xsl:if>
				</fo:table-cell>
			</xsl:if>
			<xsl:call-template name="Children">
				<xsl:with-param name="MaxNum" select="$MaxNum"/>
				<xsl:with-param name="Start" select="$Start"/>
				<xsl:with-param name="Index" select="$Index+1"/>
				<xsl:with-param name ="NextChild" select="following-sibling::ChildNode"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

And the original documents partial structure
Code:
<Parent>
    <ColHeader>
      <ChildNode SomeAttrib="A"/>
      <ChildNode SomeAttrib="B"/>
      <ChildNode SomeAttrib="C"/>
      <ChildNode SomeAttrib="D"/>
      <ChildNode SomeAttrib="E"/>
      <ChildNode SomeAttrib="F"/>
     </ColHeader>
    <Program Id="1">
      <ChildNode SomeAttrib="A">
	yes
      </ChildNode>
      <ChildNode SomeAttrib="B"/>
      <ChildNode SomeAttrib="C"/>
      <ChildNode SomeAttrib="D"/>
	yes
      </ChildNode>
      <ChildNode SomeAttrib="E"/>
      <ChildNode SomeAttrib="F"/>
    </Program>
</Parent>
And the output should be
Code:
Row Number     A    B    C     D     E     F
1              X               X

Thanks in advance, if anything is unclear, let me know
 
I got it working by using decendent::ChildNode[position()=$Index], instead of using following sibling and passing nodes through
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top