1

I have tried every solution I found on here and even a larger Google search but nothing seems to work. Here is my problem... I have data that is in XML that I would like to visualize using the Google Visualization API. I was hoping I could simply use XSL to generate what I need instead of doing anything with data sources (I have my reasons). But the javascript code does not show up in my output. Is this even possible?

My XML

<DocumentElement>
  <QueryResults>
    <Year>2000</Year>
    <Population>100000</Population>
  </QueryResults>
  <QueryResults>
    <Year>2001</Year>
    <Population>105000</Population>
  </QueryResults>
</DocumentElement>

My XSL file

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>     
    <script type="text/javascript">
        google.load('visualization', '1', {packages:['barchart']});
        google.setOnLoadCallback(drawChart);

        function drawChart() {
            var data = new google.visualization.DataTable();

            data.addColumn('string', 'Year');
            data.addColumn('string', 'Population');
            data.addRows(2); // hard-coded for testing

            <xsl:for-each select="DocumentElement/QueryResults">
                data.setValue(0, 0, '<xsl:value-of select="Year"/>');
                data.setValue(0, 1, '<xsl:value-of select="Population"/>');                 
            </xsl:for-each>

            var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
            chart.draw(data, {width: 400, height: 240, is3D: true, title: 'Population'});
        }
    </script>

    <div id="chart_div">&#160;</div>

</xsl:template>
</xsl:stylesheet>

My output

<!-- notice no javascript -->
<div id="chart_div"> </div>

1 Answer 1

2

What does the line in your XML that includes the XSL file look like? I added these two lines to the top of the XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

The result was correct, though the graph didn't actually show the data properly. It was at least called.

Sign up to request clarification or add additional context in comments.

3 Comments

What I pasted is exactly what the XML output I have to work with looks like. Then a 3rd party component renders based on the XML and my XSL... It sounds like I might have to dig into the renderer to find out what is going on. Thanks.
Which 3rd party component, if you don't mind my asking? Like I mentioned, I just copied your inputs to test.xml and test.xsl and added the stylesheet directive, and it works, so you're on the right track. I think it's probably just something with your toolchain.
It is XML spit out from the DNN reports module and I am seeing if I can use the built-in XSL visualization. I have it working using the HTML visualizer but it is ugly. And if you are not familiar with DNN then this probably means nothing to you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.