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!

Making Table row white depending on contents XSL

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I have a table I am populating, and I have the left cell always being populated with the name of a call, and the right cell being filled with files the calls are in. I want to have it so that if there is a filename in the right, the row is white, otherwise it is gray. I have this working with a similar situation, so I used similar code, by the condition for color or white is slightly different. Here is the Data that is in the XML.

Code:
<Function Call="Call1">
  <File>FileB</File> 
  </Function>
<Function Call="Call1">
  <File>FileA</File>
  <File>FileB</File>
  </Function>
<Function Call="Call1">
  <File /> 
  </Function>
<Function Call="Call1">
  <File /> 
  </Function>

And here is the XSL I have now:

Code:
<xsl:template match="Function">
		<xsl:if test="File = n">
			<fo:table-row>
				<fo:table-cell border="1pt solid black" background-color="#CCCCCC">
					<fo:block> <xsl:value-of select="@Call"/></fo:block>
				</fo:table-cell>
				<fo:table-cell border="1pt solid black" background-color="#CCCCCC">
					<xsl:apply-templates select="File" />
				</fo:table-cell>
			</fo:table-row>
		</xsl:if>
		<xsl:if test="not(File = n)">
			<fo:table-row>
				<fo:table-cell border="1pt solid black" background-color="#CCCCCC">
					<fo:block> <xsl:value-of select="@Call"/></fo:block>
				</fo:table-cell>
				<fo:table-cell border="1pt solid black" background-color="#CCCCCC">
					<xsl:apply-templates select="File" />
				</fo:table-cell>
			</fo:table-row>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="File">
		<fo:block>
		 <xsl:value-of select="." />
		</fo:block>
	</xsl:template>

I know the problem is in my If statement, I don't know exactly what it should be
 
Ignore that Both have a background color of gray, even if it is just one set to grey, it doesnt work right
 
I tried googling the solution and I took it to mean that the File node == NULL.
 
I got it, I must not have tried this line correctly the first time, but the if's should be not(string(File)) and string(File) respectively
 
Okay, an alternative, easier to read, coding that I prefer when you need an if-then-else construct:
Code:
      <xsl:choose>
        <xsl:when test="string(File)">
            <fo:table-row>
                <fo:table-cell border="1pt solid black" background-color="#CCCCCC">
                    <fo:block> <xsl:value-of select="@Call"/></fo:block>
                </fo:table-cell>
                <fo:table-cell border="1pt solid black" background-color="#CCCCCC">
                    <xsl:apply-templates select="File" />
                </fo:table-cell>
            </fo:table-row>
        </xsl:when>
        <xsl:otherwise>
            <fo:table-row>
                <fo:table-cell border="1pt solid black" background-color="#CCCCCC">
                    <fo:block> <xsl:value-of select="@Call"/></fo:block>
                </fo:table-cell>
                <fo:table-cell border="1pt solid black" background-color="#CCCCCC">
                    <xsl:apply-templates select="File" />
                </fo:table-cell>
            </fo:table-row>
        </xsl:otherwise>
      </xsl:choose>
Even more readable (one of the few times I might suggest the use of an attribute value template:
Code:
      <xsl:variable name="backColor">
      <xsl:choose>
        <xsl:when test="string(File)">
            <xsl:text>#CCCCCC</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>#CCCCCC</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:variable>
[tt]
[/tt]
      <fo:table-row>
          <fo:table-cell border="1pt solid black" background-color="{$backColor}">
              <fo:block> <xsl:value-of select="@Call"/></fo:block>
          </fo:table-cell>
          <fo:table-cell border="1pt solid black" background-color="{$backColor}">
               <xsl:apply-templates select="File" />
          </fo:table-cell>
      </fo:table-row>
This has the benefit of changing the background color in one place only.

Tom Morrison
 
Thank you. I am new to XSL so that is a very Helpful Tip
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top