問題如何利用 XSL 提供由運行的代碼確定的條件格式(例如,表行之間的增加或減少)?背景編輯: 這是一個使用 ASP.NET Core 作為審核外部資源運行狀況的長期運行服務(wù)的主機來運行的 BackgroundService 實現(xiàn)。這可能是 .NET Core 缺乏 .NET Framework 中存在的一些功能的問題,所以我認(rèn)為值得一提這個想法的開始是因為我正在尋找一種方法來制作一個簡單的 HTML 模板,我的 C# 數(shù)據(jù)對象可以以一種簡單而有效的方式映射到該模板,而無需操作 StringBuilder。懷著這樣的希望,我找到了這個 StackOverflow 響應(yīng),它說 XSLT 將是一個很好的解決方案,而無需采用完整的 MVC 方法。這就是一切的開始(為簡潔起見,進(jìn)行了刪節(jié)):<?xml version="1.0" encoding="utf-8" ?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" encoding="utf-8"/> <xsl:template match='/Email'> <html> <head> <style> <xsl:value-of select="Styles"/> </style> </head> <body> <h1>Total</h1> <table id="tblTotal" class="clsPositive"> <tr> <th>Audit Date</th> <th>Source 1</th> <th>Source 2</th> ... </tr> <xsl:for-each select="Total/ArrayOfElement/AuditElement"> <xsl:sort select="ReportDate" order="descending"/> <tr> <td><xsl:value-of select="ReportDate"/></td> <td><xsl:value-of select="Source1CountFormatted"/> (<xsl:value-of select="Source1GrowthFormatted"/>)</td> <td><xsl:value-of select="Source2CountFormatted"/> (<xsl:value-of select="Source2GrowthFormatted"/>)</td> ... </tr> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>有了這個,我有了一個想法,根據(jù)所表示的數(shù)據(jù)類型有條件地格式化增長百分比(您可以通過表上的類屬性看到它的開頭,我用它來確定增加或減少是否為正或消極的)。然而,當(dāng)我嘗試執(zhí)行此操作時,Visual Studio 告訴我 XSL 格式錯誤:<td><xsl:value-of select="Source1CountFormatted"/> (<i class="<xsl:value-of select="Source1GrowthCss"/>"><xsl:value-of select="Source1GrowthFormatted"/> </i>)</td>
1 回答

汪汪一只貓
TA貢獻(xiàn)1898條經(jīng)驗 獲得超8個贊
這里的語法是錯誤的:
<i class="<xsl:value-of select="Source1GrowthCss"/>">
要動態(tài)設(shè)置屬性的值,您可以使用xsl:attribute,如下所示:
<i>
<xsl:attribute name="class">
<xsl:value-of select="Source1GrowthCss"/>
</xsl:attribute>
</i>
但這是相當(dāng)啰嗦的。理想的方法是使用“屬性值模板”,它更清晰
<i class="{Source1GrowthCss}">
這里,大括號表示要計算表達(dá)式,而不是按字面輸出。
- 1 回答
- 0 關(guān)注
- 145 瀏覽
添加回答
舉報
0/150
提交
取消