使用XSL在HTML中对数据元素进行计数并在html中打印
问题描述:
我有一个XML URL 1。 我的输出需要是这样的使用XSL在HTML中对数据元素进行计数并在html中打印
- 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>
即使此代码正常工作,也会出现像日期和时间戳合并在一起的问题。理想情况下,计数必须仅在test_datetime元素的日期部分完成。另一个是如何自动取得日期而不是手动写入日期。
我使用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 }
如果你想自动捕捉日期,而无需手动指定,你可以做到这一点
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 }
你好Tuan,这很有意思。但我需要在html中显示这些数据。我怎么做?我是否需要使用app.get将此数据发送到前端?直接在客户端做它不是更好吗? –
你可以随时传递数据。它只是js对象。 –