0
votes

I am trying to use XSLT to transform an XML to an HTML where the bgcolor attribute of a table cell is taken from the input XML for a specific column in the table.

My XML:

<?xml version="1.0"?>
<ROWSET>
 <ROW>
  <PROJECTS>EPM Critical Patches – December 11, 2019</PROJECTS>
  <STATUS>lime</STATUS>
 </ROW>
 <ROW>
  <PROJECTS>Archive Project</PROJECTS>
  <STATUS>red</STATUS>
 </ROW>
 <ROW>
  <PROJECTS>12.1.9 ERP upgrade</PROJECTS>
  <STATUS>lime</STATUS>
 </ROW>
</ROWSET>

My XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
<HTML>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
 <xsl:template match="/*">
<TABLE BORDER="0">
<TR><TD><CENTER><font face="verdana" color="Blue" size="4">Go Live</font></CENTER></TD></TR>
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD style="font-family:verdana;font-size: 10px; " bgcolor="red">
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

<xsl:template match="/*/*">

<TR>
    <xsl:apply-templates/>
</TR>

</xsl:template>

<xsl:template match="/*/*/*">
       <xsl:choose>
         <xsl:when test="name() = 'STATUS'">
         <TD style="font-family:verdana;font-size: 10px; " bgcolor ="{//STATUS}">  </TD>
         </xsl:when>
         <xsl:otherwise>
      <TD style="font-family:verdana;font-size: 10px; "> <xsl:value-of select="."/> </TD>
         </xsl:otherwise>
       </xsl:choose>
</xsl:template>
</xsl:stylesheet>

The output I get:

<HTML>
   <BODY>
      <TABLE BORDER="0">
         <TR>
            <TD>
               <CENTER>
                  <font face="verdana" color="Blue" size="4">Go Live</font>
               </CENTER>
            </TD>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="red">PROJECTS</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="red">STATUS</TD>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; ">EPM Critical Patches – December 11, 2019</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="lime"/>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; ">Archive Project</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="lime"/>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; ">12.1.9 ERP upgrade</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="lime"/>
         </TR>
      </TABLE>
   </BODY>
</HTML>

The output I want:

<HTML>
   <BODY>
      <TABLE BORDER="0">
         <TR>
            <TD>
               <CENTER>
                  <font face="verdana" color="Blue" size="4">Go Live</font>
               </CENTER>
            </TD>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="red">PROJECTS</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="red">STATUS</TD>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; ">EPM Critical Patches – December 11, 2019</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="lime"/>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; ">Archive Project</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="red"/>
         </TR>
         <TR>
            <TD style="font-family:verdana;font-size: 10px; ">12.1.9 ERP upgrade</TD>
            <TD style="font-family:verdana;font-size: 10px; " bgcolor="lime"/>
         </TR>
      </TABLE>
   </BODY>
</HTML>

The bgcolor of the second cell in each row should have the value of the STATUS text node for that row in the input XML. Instead, it repeats the value of the first rown throughout the rest. I also tried putting <xsl:value-of select="."/> in the bgcolor attribute but then it complained "The value of attribute "bgcolor" associated with an element type "TD" must not contain the '<' character.".

I also need a solution that is XSL 1.0 compliant as the vendor refuses to upgrade their parser in the technology stack.

1

1 Answers

0
votes

it repeats the value of the first rown throughout the rest.

Of course it does:

bgcolor ="{//STATUS}"

takes all the STATUS elements in the entire document and (in XSLT 1.0) returns the value of the first one.

Since you are already in the context of STATUS and you want the current value, try:

bgcolor ="{.}"