2017-10-21 23 views
1

我有一個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的開發人員工具中看到任何錯誤。所以,在如何着手方面有點失落。

回答

0

您可以使用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 } 
+0

你好Tuan,這很有意思。但我需要在html中顯示這些數據。我怎麼做?我是否需要使用app.get將此數據發送到前端?直接在客戶端做它不是更好嗎? –

+0

你可以隨時傳遞數據。它只是js對象。 –

相關問題