使用 xml 和 xsl 生成 javascript [英] generate javascript using xml and xsl

查看:30
本文介绍了使用 xml 和 xsl 生成 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了在这里找到的所有解决方案,甚至是更大的 Google 搜索,但似乎没有任何效果.这是我的问题...我有 XML 格式的数据,我想使用 Google Visualization API 对其进行可视化.我希望我可以简单地使用 XSL 来生成我需要的东西,而不是对数据源做任何事情(我有我的理由).但是 javascript 代码没有显示在我的输出中.这甚至可能吗?

我的 XML

<查询结果><年>2000</年><人口>100000</人口></查询结果><查询结果><年>2001</年><人口>105000</人口></查询结果></DocumentElement>

我的 XSL 文件

<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('可视化', '1', {packages:['barchart']});google.setOnLoadCallback(drawChart);函数 drawChart() {var data = new google.visualization.DataTable();data.addColumn('string', 'Year');data.addColumn('string', '人口');数据.addRows(2);//硬编码用于测试<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'});}<div id="chart_div">&#160;</div></xsl:模板></xsl:stylesheet>

我的输出

<!-- 注意没有 javascript --><div id="chart_div">

解决方案

XML 中包含 XSL 文件的行是什么样的?我将这两行添加到 XML 的顶部:

结果是正确的,但图表实际上并未正确显示数据.它至少被称为.

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>

解决方案

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.

这篇关于使用 xml 和 xsl 生成 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆