【问题标题】:Is there a way to create dynamic html from fixed xml file有没有办法从固定的 xml 文件创建动态 html
【发布时间】:2011-06-17 15:47:23
【问题描述】:

我尝试使用jqGrid,但它只映射 xml 节点文本。我在下面给出了我的 xml,我需要使用 xpath 或类似内容在其中显示 filesystem 内容,并具有适当的变量分页、过滤、排序和特定行的选择。

<?xml-stylesheet type="text/xsl" href="csmclientiir.xsl"?>
<csmclient product="abc"   date="4/26/11 2:05 PM">
<system>
    <osname>Linux
    </osname>
    <hostname>AbhishekNix
    </hostname>
    <release>2.6.18-128.el5
    </release>
    <filesystem>
        <file mount='/home/hp1' home='(innfs2:/vol/home/shome/home/hp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/par21' home='(innfs2:/vol/home/shome/home/par21)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/h231' home='(innfs2:/vol/home/shome/home/h231)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/avallin1' home='(innfs2:/vol/home/shome/home/avallin1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/park' home='(innfs2:/vol/home/shome/home/park)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/sp1' home='(innfs2:/vol/home/shome/home/sp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/ganga1' home='(innfs2:/vol/home/shome/home/ganga1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
        <file mount='/home/nbp1' home='(innfs2:/vol/home/shome/home/nbp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    </filesystem>
</system>
<product>
    <showtime>Tue Apr 26 14:05:23 2011
    </showtime>
</product>
</csmclient>

我还提供了我用于 jqGrid 的代码,它只显示标题

jQuery("#listTable").jqGrid({
    url: cpath, 
    datatype: "xml",
    colNames:["Total Space","Free Space","Used Space", "Used Percentage"], 
    colModel:[ {name:"Total Space",index:"Total Space", width:90, xmlmap:"system>filesystem>file>@total"},
               {name:"Free Space",index:"Free Space", width:120, xmlmap:"system>filesystem>file>@free"},
               {name:"Used Space",index:"Used Space", width:180,xmlmap:"system>filesystem>file>@used"},
               {name:"Used Percentage",index:"Used Percentage", width:100, align:"right",xmlmap:"system>filesystem>file>@percentage", sorttype:"float"}
             ],
    height:250,
    pager: '#pager',
    rowNum:10,
    rowList:[10,20,30], 
    viewrecords: true, 
    gridview: true,
    loadonce: true, 
    xmlReader: { 
        root : "csmclient",
        row: "system>filesystem",
        repeatitems: false,
        id: "ASIN"
        },
    caption: "Disk Usage"
    });

【问题讨论】:

    标签: jquery jquery-ui jquery-plugins jqgrid


    【解决方案1】:

    根据the docs

    XML 说明和限制

    任何 XML 标签中的属性都不能用于获取数据。唯一的例外是 id

    您必须将 XML 转换为与 jqGrid 兼容的格式。我会使用XSLT

    【讨论】:

    • 我不能使用jquery xml解析器将每个file转换成jqGrid支持的数据吗?如果是的话,你能帮我解决一下吗,即使是 XSLT 的例子也很好,我还需要变量分页、过滤和排序
    • 是的,您可以使用 jQuery 来重构 XML。就像操作 HTML DOM 一样,只是你不用像 $('#myElement') 这样的选择器开始,而是从像 var xml = '&lt;foo&gt;&lt;bar/&gt;&lt;/foo&gt;;` 这样的 XML 字符串开始,然后 jQueryify 它:$(xml)
    • @Abhishek: addXmlData jqGrid 用于读取 XML 数据的方法不如 addJSONData 灵活。它使用this.textContent || this.text 或其他地方的this.getAttribute("name") 之类的构造(请参阅herehere)。所以在大多数情况下你不能使用属性。因此,您必须以任何方式转换您的 XML 数据以使其与 jqGrid 兼容。
    • @Oleg:我是初学者,请多多包涵,所以第一次我会发送一个请求来获取我的 xml 数据,将该 xml 数据转换为一些等效的数组,第三次我将使用这个数组创建 jqGrid。那是对的吗?如果是,您能否提供我可以构建的数组结构。谢谢
    • @Abhishek:如果服务器将以另一种格式生成服务器响应,则最好:没有表格行属性的 XML 或更好的 JSON 格式。您可以在the documentation 找到更多信息。服务器还应该只回发客户端请求的一页数据。有关分页的更多信息,请参阅here
    猜你喜欢
    • 2021-04-23
    • 1970-01-01
    • 2020-02-05
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多