Question

I have inherited some xslt code that isn't working correctly. I have examined it and looked at various questions here and elsewhere, but still can't isolate the issue. There are numerous links through the code which attempt to use the xslt variable link_value. Everywhere it is referenced it is in the format '{$link_value}', and here is where confusion starts. In some answers here, it is stated that the variable must be in braces, and in others, the braces are not there. Some say no quotes around it and some show quotes. link_value is a string and needs the quotes when it is referenced. The value that is being placed in link_value is in the corresponding XML. And then there is the scoping issue, for which I have seen numerous answers, all contradictory. Can someone tell me why link_value wont expand properly?

link_value references //companyheader/title which is also referenced on line 195 and shows up correctly on the web page.

Also, there is a $("#roatitle").click(function() entry (at line 176) which also uses link_value. this entry gives me a pointer on the web page, but when I click on it nothing happens. The entry on line 180 has the same issue. Any help on this one?

I compressed the XML to keep it small and only show the revelant data. chartsdatareport.XML:

<chartsdatareport>
  <privateroaroeratiosdata>
    <datapoint></datapoint>
  </privateroaroeratiosdata>
  <privateincomeratiosdata>
    <datapoint></datapoint>
  </privateincomeratiosdata>
  <companyheader>
    <id>300902</id>
    <title>Bank of America NA</title>
    <address>100 N Tryon St, Ste 170                 </address>
    <citystatezip>Charlotte, NC 28202</citystatezip>
    <phone>(980) 335-3561</phone>
    <fax>(704) 386-0981</fax>
    <internetaddress>www.bankofamerica.com</internetaddress>
  </companyheader>
  <financial>
  </financial>
  <bankinsightrating>
  </bankinsightrating>
  <financialsummary>
  </financialsummary>
  <loancomposition>
  </loancomposition>
  <depositscomposition>
  </depositscomposition>
</chartsdatareport>

I wanted to include all of the code for scoping purposes, but had to remove a bunch of style sheet code to keep it small. PrivateChartsDataReport.xslt:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="yes"/>
      <xsl:template match="/chartsdatareport">
      <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html></xsl:text>
          <html>
             <head>
          <title>Company Tearsheet</title>
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
                <script type="text/javascript">
                  var roeValues = [];
                  var roaValues = [];
                  var netIncomeValues = [];
                  var netInterestIncomeValues = [];
                  var roaroequarters = [];
                  var incomequarters = [];
                  var tier1riskbasedcapital = [];
                  var totalriskbasedcapital = [];
                  var toteqtytotassets = [];
                  var corecaploanlossres = [];
                  var IntGrowthRateEqtyCap = [];
                  var leverage = [];
                  var tangcommequity = [];
                  var totalinterestincome = [];
                  var totalinterestexpense = [];
                  var noninterestincome = [];
                  var overheadexpense = [];
                  var nplgrossloans = [];
                  var npatotalassets = [];
                  var ncoavgloans = [];
                  var reservesgrossloans = [];
                  var totalloansnet = [];
                  var avgearnasst = [];
                  var nonintbearingdeposits = [];
                  var totalnonearningassets = [];
                  var date = [];
                  var ratingTA=<xsl:value-of select="//bankinsightrating/ratingta"/>;
                  var rating = [<xsl:value-of select="//bankinsightrating/peers"/>, <xsl:value-of select="//bankinsightrating/regional"/>, <xsl:value-of select="//bankinsightrating/national"/>];
                  var revertrating = [<xsl:value-of select="100-//bankinsightrating/peers"/>, <xsl:value-of select="100-//bankinsightrating/regional"/>, <xsl:value-of select="100-//bankinsightrating/national"/>];
                  <xsl:apply-templates select="privateroaroeratiosdata"/>
                  <xsl:apply-templates select="privateincomeratiosdata"/>
            var entitytype="<xsl:value-of select="//companyheader/entitytype"/>";
            var chartroaroe;
            var chartnetincome;
            var loan;
            var deposits;
            var capitalAdequacy1;
            var capitalAdequacy2;
            var capitalAdequacyCU1;
            var capitalAdequacyCU2;
            var profitabilityRatios1;
        var profitabilityRatios2;
        var assetQuality1;
        var assetQuality2;
        var liquidity1;
        var liquidity2;
    <xsl:text disable-output-escaping="yes">        
    <![CDATA[       
            function addCommas(str) {
                var amount = new String(str);
                amount = amount.split("").reverse();

                var output = "";
                for ( var i = 0; i <= amount.length-1; i++ ){
                    output = amount[i] + output;
                    if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;
                }
                return output;
            }
            function formatRating(val)  {
              if (val == 0 && ratingTA == 0 && entitytype=='Holding Company'){
                      return '<span style="font-size:20px">N/A</span>';
                } else {
                      return '<span style="color:white">'+val+'</span>';
                }
            }
    ]]>   
    </xsl:text>   
            $(document).ready(function() {
                  chartrating = new Highcharts.Chart({
                     chart: {
                        renderTo: 'rating',
                        type: 'column',
                        backgroundColor: '#666666',
                        margin: 0,
                        spacingBottom: 5,
                        spacingLeft: 0,
                        spacingRight: 0
                     },
                     credits: {
                      enabled: false
                     },
                     exporting: {
                      buttons: {
                        exportButton: {
                          enabled: false
                        },
                        printButton: {
                          enabled: false
                        }
                      }
                     },
                     title: {
                      text: 'Banking Insight Rating',
                        style: {
                          display: 'none'
                        }
                 },
                 xAxis: {
                    categories: rating,
                  gridLineWidth: 0,
                  minorGridLineWidth: 0,
                  lineColor: 'transparent',
                  lineWidth: 0,
                  tickLength: 0,
                  minorTickLength: 0,
                  labels: {
                    enabled: true,
                    style: {
                      font: 'bold 26px Verdana',
                      color: '#FFFFFF'
                    },
                    y: -35,
                    formatter: function(){
                      return formatRating(this.value);
                    }
                  }
                 },
                 yAxis: {
                  gridLineWidth: 0,
                  lineWidth: 0,
                  min: 0,
                  max: 100,
                  labels: {
                    enabled: false
                  },
                  title: {
                    text: '',
                    margin: 0
                  }
                 },
                 plotOptions: {
                  column: {
                    stacking: 'normal',
                    borderWidth: 0,
                    point: {
                      events: {
                        mouseOut: function(){
                          $("#ratingtooltip").hide();
                        },
                        mouseOver: function(){
                          $("#ratingtooltip").show();
                        }
                      }
                    }
                  },
                  series: {
                    pointWidth: 50
                  }
                 },
                 tooltip: {
                  enabled: false
                 },
                 legend:{
                  enabled: false
                 },
                 series: [{
                  color: '#005A84',
                    data: revertrating
                 },{
                  color: '#FF9100',
                    data: rating
                 }]
              });

            $("#roatitle").click(function(){
              parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/><xsl:text disable-output-escaping="yes"><![CDATA[&report=DYRPT%3A%3A118]]></xsl:text>','{$link_value}');
            });

            $("#incometitle").click(function(){
              parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/><xsl:text disable-output-escaping="yes"><![CDATA[&report=EXCEL%3A%3A621]]></xsl:text>','{$link_value}');
            });

          });
               </script>
         </head>
         <body>
      <script src="/bats/highcharts/highcharts.js"></script>
      <script src="/bats/highcharts/modules/exporting.js"></script>
      <script src="/bats/js/uiUtilBats.js"></script>
      <table cellpadding="0" cellspacing="0" border="0" width="100%" class="bg">
        <tr>
          <td class="companyheader" vAlign="top">
            <div class="companytitle"><xsl:value-of select="//companyheader/title"/></div>
            <div class="top5"><xsl:value-of select="//companyheader/address"/></div>
            <div><xsl:value-of select="//companyheader/citystatezip"/></div>
            <div>Phone: <xsl:value-of select="//companyheader/phone"/></div>
            <div>Fax: <xsl:value-of select="//companyheader/fax"/></div>
            <div>
              <a target="_blank">
                <xsl:attribute name="href">http://<xsl:value-of select="//companyheader/internetaddress"/></xsl:attribute>
                <xsl:value-of select="//companyheader/internetaddress"/>
              </a>  
            </div>
          </td>
          <td>
          <div id="zero1">($000)</div>
          </td>
          <td class="rating">
            <table cellpadding="0" cellspacing="0" border="0" width="100%">
              <tr>
                <td class="ratingtitle">
                  <a href="javascript:void(0)">
                    <xsl:attribute name="onclick">parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/>&amp;report=EXCEL%3A%3A401','{$link_value}')</xsl:attribute>                       
                Bank Insight Rating</a></td>
              </tr>
              <tr>
               <td class="ratinglink">
                  <a href="#" onclick="parent.getHighlineTop().newTab('/banking/pages/bpages/ratingmethod.html', 'BankInsight Rating Methodology')">
                    Rating Methodology</a></td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td colspan="3" class="menu">
            <table align="center" border="0" cellpadding="0" cellspacing="0" width="87%" >
              <tr>

                <xsl:choose>
                  <xsl:when test="//companyheader/entitytype != 'Credit Union'">
                    <td align="left" width="33%">
                  <a href="javascript:void(0)">
                  <xsl:attribute name="onclick">parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/>&amp;report=5','{$link_value}')</xsl:attribute>
                    M&amp;A Activity</a></td>
                  </xsl:when>
                  <xsl:otherwise>
                    <td align="left" width="33%">
                  <a href="javascript:void(0)">
                  <xsl:attribute name="onclick">parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/>&amp;report=DYRPT%3A%3A20','{$link_value}')</xsl:attribute>
                    Peer Group Data</a></td>
                  </xsl:otherwise>
                </xsl:choose>
                <td align="center" width="33%">
                  <a href="javascript:void(0)">
                  <xsl:attribute name="onclick">parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/>&amp;report=1000','{$link_value}')</xsl:attribute>
                    Market Share Data</a></td>
                <xsl:choose>
                  <xsl:when test="//companyheader/entitytype != 'Credit Union'">
                    <td align="right" width="33%">
                  <a href="javascript:void(0)">
                  <xsl:attribute name="onclick">parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/>&amp;report=41','{$link_value}')</xsl:attribute>
                    Company Hierarchy</a></td>
                  </xsl:when>
                  <xsl:otherwise>
                    <td align="right" width="33%">
                  <a href="javascript:void(0)">
                  <xsl:attribute name="onclick">parent.getHighlineTop().newTab('/bats/company/PopupReport.do?entityID=<xsl:value-of select="//companyheader/id"/>&amp;report=EXCEL%3A%3A365','{$link_value}')</xsl:attribute>
                    Income Analysis</a></td>
                  </xsl:otherwise>
                </xsl:choose>
              </tr>
            </table></td>
        </tr>
      </table>
         </body>
      </html>
  </xsl:template>
  <xsl:variable name="apos">'</xsl:variable>
  <xsl:variable name="escapos">\'</xsl:variable>

  <xsl:variable name="link_value">
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="//companyheader/title"/>
          <xsl:with-param name="replace" select="$apos"/>
          <xsl:with-param name="by" select="$escapos"/>
        </xsl:call-template>
  </xsl:variable>
  <xsl:template match="privateroaroeratiosdata">
    <xsl:for-each select="./datapoint">
            roeValues.push(<xsl:value-of select="./roevalue"/>);
            roaValues.push(<xsl:value-of select="./roavalue"/>);
            roaroequarters.push('<xsl:value-of select="./quarter"/>');
            tier1riskbasedcapital.push(<xsl:value-of select="./tier1riskbasedcapital"/>);
            totalriskbasedcapital.push(<xsl:value-of select="./totalriskbasedcapital"/>);
            leverage.push(<xsl:value-of select="./leverage"/>);
            tangcommequity.push(<xsl:value-of select="./tangcommequity"/>);
        toteqtytotassets.push(<xsl:value-of select="./toteqtytotassets"/>);
            corecaploanlossres.push(<xsl:value-of select="./corecaploanlossres"/>);
      IntGrowthRateEqtyCap.push(<xsl:value-of select="./IntGrowthRateEqtyCap"/>);
            totalinterestincome.push(<xsl:value-of select="./totalinterestincome"/>);
            totalinterestexpense.push(<xsl:value-of select="./totalinterestexpense"/>);
            noninterestincome.push(<xsl:value-of select="./noninterestincome"/>);
            overheadexpense.push(<xsl:value-of select="./overheadexpense"/>);
            nplgrossloans.push(<xsl:value-of select="./nplgrossloans"/>);
            npatotalassets.push(<xsl:value-of select="./npatotalassets"/>);
            ncoavgloans.push(<xsl:value-of select="./ncoavgloans"/>);
            reservesgrossloans.push(<xsl:value-of select="./reservesgrossloans"/>);
            totalloansnet.push(<xsl:value-of select="./totalloansnet"/>);
            avgearnasst.push(<xsl:value-of select="./avgearnasst"/>);
            nonintbearingdeposits.push(<xsl:value-of select="./nonintbearingdeposits"/>);
            totalnonearningassets.push(<xsl:value-of select="./totalnonearningassets"/>);
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="privateincomeratiosdata">
    <xsl:for-each select="./datapoint">
            netIncomeValues.push(<xsl:value-of select="./netincome"/>);
            netInterestIncomeValues.push(<xsl:value-of select="./netinterestincome"/>);
            incomequarters.push('<xsl:value-of select="./quarter"/>');
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="string-replace-all">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$by"/>
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text"
            select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Here is snippet from another of our codes where this works fine. But I think due to all of the code being together:

     <tr class="evenrow"><td>Top Holding Company:</td>
       <td>
         <xsl:variable name="apos">'</xsl:variable>
         <xsl:variable name="escapos">\'</xsl:variable>
         <xsl:variable name="link_value">
         <xsl:call-template name="string-replace-all">
           <xsl:with-param name="text" select="TOP_HC_NAME" />
           <xsl:with-param name="replace" select="$apos"/>
           <xsl:with-param name="by" select="$escapos"/>
         </xsl:call-template>
         </xsl:variable>
         <xsl:choose>
           <xsl:when test="TOP_HC!=''">
             <a href="javascript:void(0)" onclick="topHC('{TOP_HC}','{$link_value}')"><xsl:value-of select="TOP_HC_NAME" /></a>
           </xsl:when>
           <xsl:otherwise>
             <xsl:value-of select="TOP_HC_NAME" />
           </xsl:otherwise>
         </xsl:choose>
       </td>
     </tr>

thanks in advance for your assistance.

Was it helpful?

Solution

The XPath expression to extract the value of a variable is $variableName with no quotes, but the question is which places in the stylesheet are interpreted as XPath expressions and which aren't.

In attribute values on literal result elements you can include XPath expressions in braces and they will be evaluated, this is called "attribute value template" syntax. This is the situation with something like

<a onclick="topHC('{TOP_HC}','{$link_value}')">

where TOP_HC and $link_value are XPath expressions, and the single quotes surrounding these will come through literally in the output and will form part of the resulting JavaScript expression.

But in situations like

<xsl:attribute name="onclick">parent.getHighlineTop().newTab('...','{$link_value}')</xsl:attribute>

you are not in an attribute value template, and the {$link_value} is output literally rather than being treated as an XPath expression. To evaluate XPaths in this situation you need to use value-of. It may be clearer to wrap the literal segments in xsl:text so you can break the lines in the XSLT without introducing newlines into the result:

<xsl:attribute name="onclick">
  <xsl:text>parent.getHighlineTop().newTab('...','</xsl:text>
  <xsl:value-of select="$link_value"/>
  <xsl:text>')</xsl:text>
</xsl:attribute>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top