我試圖通過xslt將xml文件從ECB轉換爲xhtml,但我有某處錯誤。通過xslt更改xml到xhtml
這是XML文件
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="output.xsl"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2011-10-18'>
<Cube currency='USD' rate='1.3676'/>
<Cube currency='JPY' rate='104.97'/>
<Cube currency='BGN' rate='1.9558'/>
<Cube currency='CZK' rate='24.925'/>
<Cube currency='DKK' rate='7.4456'/>
<Cube currency='GBP' rate='0.87020'/>
<Cube currency='HUF' rate='298.40'/>
<Cube currency='LTL' rate='3.4528'/>
<Cube currency='LVL' rate='0.7057'/>
<Cube currency='PLN' rate='4.3684'/>
<Cube currency='RON' rate='4.3525'/>
<Cube currency='SEK' rate='9.1589'/>
<Cube currency='CHF' rate='1.2348'/>
<Cube currency='NOK' rate='7.7605'/>
<Cube currency='HRK' rate='7.4715'/>
<Cube currency='RUB' rate='42.8780'/>
<Cube currency='TRY' rate='2.5568'/>
<Cube currency='AUD' rate='1.3489'/>
<Cube currency='BRL' rate='2.4332'/>
<Cube currency='CAD' rate='1.4018'/>
<Cube currency='CNY' rate='8.7262'/>
<Cube currency='HKD' rate='10.6373'/>
<Cube currency='IDR' rate='12061.31'/>
<Cube currency='ILS' rate='4.9936'/>
<Cube currency='INR' rate='67.5500'/>
<Cube currency='KRW' rate='1567.60'/>
<Cube currency='MXN' rate='18.5187'/>
<Cube currency='MYR' rate='4.2854'/>
<Cube currency='NZD' rate='1.7360'/>
<Cube currency='PHP' rate='59.256'/>
<Cube currency='SGD' rate='1.7423'/>
<Cube currency='THB' rate='42.095'/>
<Cube currency='ZAR' rate='11.0432'/>
</Cube>
</Cube>
</gesmes:Envelope>
,這裏是我的XSLT文件
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Exchange rates</title>
</head>
<body>
<table>
<tr>
<th>Rate</th>
</tr>
<xsl:for-each select="/gesmes:Envelope/Cube/Cube/Cube">
<tr>
<td><xsl:value-of select="Cube/[@currency='USD']"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
,並導致這個樣子:
USD 1.3676
JPY 104.97
BGN 1.9558
等
我需要一個解決方案,而不是一個方式如何o獲得正確的源代碼。
然後,我只是運行的.xsl文件?因爲它顯示我錯了,我看到源代碼,這是錯誤的。 – user1006959
@ user1006959:您需要複製代碼,將其粘貼到文本編輯器中並將其保存在文件中。然後將此文件中的轉換應用於包含源XML的文件(使用您喜歡的任何方法 - 例如從命令行)。我從未發佈未經測試的解決方案。正如在這種情況下,我總是運行解決方案並驗證它產生了預期的結果。然後我簡單地複製代碼並將其粘貼到我的答案中。如果您遇到問題如何進行轉換,請提出另一個問題。 –
好的,我創建了兩個新文件。我將源文件複製到這些文件中。但是當我在Opera,Chrome或Safari上運行它時,它顯示我只是'Currency \t Rate'。你有什麼主意嗎? – user1006959