xsl-list
[Top] [All Lists]

xsl:apply-templates with variable

2004-09-21 13:12:27
Hello All,

I have the following XML file:

<CUSTOMERS>     
        <CUSTOMER>
                <CUST_ID>2</CUST_ID>
                <FIRST_NAME>test</FIRST_NAME>
                <LAST_NAME>test2</LAST_NAME>            
                <CUST_ACCUCAST_DATA>
                        <CAD_CUST_ID>2</CAD_CUST_ID>
                        <MSG_CONTENT>T</MSG_CONTENT>
                        <BAD_EMAIL> </BAD_EMAIL>
                        <USER_AGENT>Mozilla/4.0</USER_AGENT>
                        <EMAIL_AGENT> </EMAIL_AGENT>
                </CUST_ACCUCAST_DATA>
                <DEPARTMENT>
                        <D_ID>2</D_ID>
                        <D_NAME>Sales</D_NAME>
                </DEPARTMENT>
                <ORDERSS>
                        <ORDERS>
                                <O_ID>2</O_ID>
                        </ORDERS>
                </ORDERSS>
        </CUSTOMER>
</CUSTOMERS>


---------------------
with following xslt:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
                xmlns:fo="http://www.w3.org/1999/XSL/Format";>
<xsl:param name="ROOT"/>

  <xsl:template match="CUSTOMERS">
    <html>
      <body>
        <table cellpadding="5" cellspacing="2" border="0" width="100%">
        
          <xsl:call-template name="printHeaders"/>
          <xsl:apply-templates select="CUSTOMER"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="CUSTOMER">
    <xsl:call-template name="printNode"/>
  </xsl:template>

  <xsl:template name="printHeaders">
    <tr bgcolor="#4D5D97">
      <xsl:variable name="spanA"
select="count(CUSTOMER[1]/child::*[count(child::*)=0])"/>
      <td colspan="{$spanA}">
        <xsl:text> </xsl:text>
      </td>
      <xsl:for-each select="CUSTOMER[1]/child::*[count(child::*) &gt;
0]">
        <xsl:for-each select="child::*[1]">
          <xsl:variable name="spanB" select="count(child::*)"/>
          <td colspan="{$spanB}" valign="top">
            <font face="Arial" size="3" color="white">
              <b><xsl:value-of select="name()"/></b>
            </font>
          </td>
        </xsl:for-each>
      </xsl:for-each>
    </tr>
    <tr bgcolor="#4D5D97">
      <xsl:for-each select="CUSTOMER[1]/child::*[count(child::*)=0]">
        <td valign="top">
          <font face="Arial" size="3" color="white">
            <b><xsl:value-of select="name()"/></b>
          </font>
        </td>
      </xsl:for-each>
      <xsl:for-each select="CUSTOMER[1]/child::*[count(child::*) &gt;
0]">
        <xsl:for-each select="child::*[1]">
          <xsl:for-each select="child::*">
            <td valign="top">
              <font face="Arial" size="3" color="white">
                <b><xsl:value-of select="name()"/></b>
              </font>
            </td>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </tr>
  </xsl:template>

  <xsl:template name="printValues">
    <xsl:for-each select="child::*">
      <xsl:if test="count(child::*)=0">
        <td>
          <font face="Arial" size="3" color="black">
            <xsl:value-of select="."/>
           </font>
        </td>
      </xsl:if>
      <xsl:if test="count(child::*) &gt; 0">
        <xsl:variable name="span" select="count(descendant::*)-1"/>
        <td colspan="{$span}" valign="top">
          <table cellpadding="5" cellspacing="2" border="0"
width="100%">
            <xsl:for-each select="child::*">
              <tr>  
                <xsl:if test="count(child::*) = 0">
                  <td><xsl:text> </xsl:text></td>
                </xsl:if>
                <xsl:for-each select="child::*">
                  <td>
                    <font face="Arial" size="3" color="black">
                      <xsl:value-of select="."/>
                     </font>
                  </td>
                </xsl:for-each>
              </tr>
            </xsl:for-each>
          </table>
        </td>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="printNode">
    <tr bgcolor="#FFFFFF">  
      <xsl:if test="position() mod 2 = 0">
        <xsl:attribute name="bgcolor">#E7ECF0</xsl:attribute>
      </xsl:if>
      <xsl:call-template name="printValues"/>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>



-------------------

I have added the <xsl:param name="ROOT"/> and passing in this value from
my Java code.  This value will be a dynamic variable in my xml file. For
now I am passing in CUSTOMER as the value. I print my $ROOT to make sure
I have passed in the correct value. 

When I replace 'CUSTOMER' with ($ROOT) within my xslt file I get the
following error:

avax.xml.transform.TransformerConfigurationException:
javax.xml.transform.TransformerConfigurationException:
javax.xml.transform.TransformerException:
javax.xml.transform.TransformerException: A node test that matches
either NCName:* or QName was expected.





---------------------
This is my xslt file after I replace the 'CUSTOMER' with ($ROOT)
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
                xmlns:fo="http://www.w3.org/1999/XSL/Format";>
<xsl:param name="ROOT">CUSTOMER</xsl:param>

  <xsl:template match="CUSTOMERS">
    <html>
      <body>
        <table cellpadding="5" cellspacing="2" border="0" width="100%">
        
          <xsl:call-template name="printHeaders"/>
          <xsl:apply-templates select="($ROOT)"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="$ROOT">
    <xsl:call-template name="printNode"/>
  </xsl:template>

  <xsl:template name="printHeaders">
    <tr bgcolor="#4D5D97">
      <xsl:variable name="spanA"
select="count(($ROOT)[1]/child::*[count(child::*)=0])"/>
      <td colspan="{$spanA}">
        <xsl:text> </xsl:text>
      </td>
      <xsl:for-each select="($ROOT)[1]/child::*[count(child::*) &gt;
0]">
        <xsl:for-each select="child::*[1]">
          <xsl:variable name="spanB" select="count(child::*)"/>
          <td colspan="{$spanB}" valign="top">
            <font face="Arial" size="3" color="white">
              <b><xsl:value-of select="name()"/></b>
            </font>
          </td>
        </xsl:for-each>
      </xsl:for-each>
    </tr>
    <tr bgcolor="#4D5D97">
      <xsl:for-each select="($ROOT)[1]/child::*[count(child::*)=0]">
        <td valign="top">
          <font face="Arial" size="3" color="white">
            <b><xsl:value-of select="name()"/></b>
          </font>
        </td>
      </xsl:for-each>
      <xsl:for-each select="($ROOT)[1]/child::*[count(child::*) &gt;
0]">
        <xsl:for-each select="child::*[1]">
          <xsl:for-each select="child::*">
            <td valign="top">
              <font face="Arial" size="3" color="white">
                <b><xsl:value-of select="name()"/></b>
              </font>
            </td>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </tr>
  </xsl:template>

  <xsl:template name="printValues">
    <xsl:for-each select="child::*">
      <xsl:if test="count(child::*)=0">
        <td>
          <font face="Arial" size="3" color="black">
            <xsl:value-of select="."/>
           </font>
        </td>
      </xsl:if>
      <xsl:if test="count(child::*) &gt; 0">
        <xsl:variable name="span" select="count(descendant::*)-1"/>
        <td colspan="{$span}" valign="top">
          <table cellpadding="5" cellspacing="2" border="0"
width="100%">
            <xsl:for-each select="child::*">
              <tr>  
                <xsl:if test="count(child::*) = 0">
                  <td><xsl:text> </xsl:text></td>
                </xsl:if>
                <xsl:for-each select="child::*">
                  <td>
                    <font face="Arial" size="3" color="black">
                      <xsl:value-of select="."/>
                     </font>
                  </td>
                </xsl:for-each>
              </tr>
            </xsl:for-each>
          </table>
        </td>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="printNode">
    <tr bgcolor="#FFFFFF">  
      <xsl:if test="position() mod 2 = 0">
        <xsl:attribute name="bgcolor">#E7ECF0</xsl:attribute>
      </xsl:if>
      <xsl:call-template name="printValues"/>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>




I'll appreciate any help or ideas.


Many thanks in advance,
Paria









<Prev in Thread] Current Thread [Next in Thread>