使用XSL计算XML URL中的数据元素并在html中打印
我有一个XML URL 1 。 我的输出需要是类似的东西
- 2009-12-01:2
- 2009-12-02:2
我试图用XSL实现这个(这是我第一次使用它,所以我的代码可能看起来很愚蠢)。
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <xsl:copy-of select="document('http://vhost11.lnu.se:20090/final/getFilterData.php?parameter=User_IDpatient&value=3')/EData/test_sessionID"/> 2009-12-01 : <xsl:value-of select="count(EData/test_sessionID[test_datetime=2009-12-01 18:00:00])" /> 2009-12-02 : <xsl:value-of select="count(EData/test_sessionID[test_datetime=2009-12-02 18:00:00])" /> </body> </html> </xsl:template> </xsl:stylesheet>
即使这个代码有效,也会有一些问题,例如date和时间戳被合并。 理想情况下,计数只能在test_datetime元素的date部分完成。 另一个是如何自动取得date,而不是手动写入。
我使用node.js作为我的服务器。 在控制台或Chrome的开发人员工具中,我不会收到任何错误。 所以,在如何进行中有点失落。
你可以用这个camaro
const transform = require('camaro') const rp = require('request-promise') rp('http://vhost11.lnu.se:20090/final/getFilterData.php?parameter=User_IDpatient&value=3') .then(xml => { return transform(xml, { '2009-12-01': 'count(//test_datetime[text() = "2009-12-01 18:00:00"])', '2009-12-02': 'count(//test_datetime[text() = "2009-12-02 18:00:00"])' }) }) .then(console.log)
产量
{ '2009-12-01': 2, '2009-12-02': 2 }
如果您想自动捕获date而无需手动指定,则可以执行此操作
const transform = require('camaro') const rp = require('request-promise') const _ = require('lodash') rp('http://vhost11.lnu.se:20090/final/getFilterData.php?parameter=User_IDpatient&value=3') .then(xml => { return transform(xml, { data: ['//test_datetime', { value: './text()' }] }) }) .then(json => _.groupBy(json.data, 'value')) .then(grps => _.mapValues(grps, 'length')) .then(console.log)
产量
{ '2009-12-01 18:00:00': 2, '2009-12-02 18:00:00': 2 }