html - XSL:无法根据条件将值填充到 HTML
问题描述
我在准备正确的 XSLT 模板以将值填充到 HTML 时遇到问题。在我的情况下,我想填充列Test2的值,当它是它的验证类型时。
我的 XSL 模板部分是:
<xsl:for-each select="CurrentFile/RejRow[Col/ValidationType='Task: Non-Numeric']">
<tr>
<td>
<span>
<xsl:value-of select="Col/ColVal"/>
</span>
</td>
</tr>
</xsl:for-each>
XML 是:
<CurrentFile>
<RejRow>
<Col>
<ColName>Test1</ColName>
<ColVal>TestVal1</ColVal>
</Col>
<Col>
<ColName>Test2</ColName>
<ColVal>TestVal2</ColVal>
<ValidationType>Task: Non-Numeric</ValidationType>
</Col>
</RejRow>
</CurrentFile>
更新:当我需要检查具有多个验证的多个列并仅输出未通过它的列时,我当前的答案不适用于这种情况。仍然需要帮助。
这种方法对我不起作用:
<xsl:for-each select="RejRow[count(Col/ValidationType)!=0]">
<tr>
<xsl:for-each select="Col[ColName='Test2']">
<xsl:choose>
<xsl:when test="Col[ValidationType='Task: Non-Numeric']">
<td class="warningTd">
<span class="warningRed">
<xsl:value-of select="ColVal"/>
</span>
</td>
</xsl:when>
<xsl:when test="Col[count(ValidationType)=0]">
<td class="warningTd">
<span class="normal">
<xsl:value-of select="ColVal"/>
</span>
</td>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:for-each>
解决方案
假设您只想在 a<RejRow>
同时具有 a<ValidationType>
和 a<Col>
且<ColName>
等于时输出 a Test2
:
<xsl:template match="/CurrentFile">
<table>
<xsl:for-each select="RejRow[Col[ValidationType and ColName='Test2']]">
<tr>
<xsl:for-each select="Col[ColName='Test2']">
<td class="warningTd">
<span>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="ValidationType = 'Task: Non-Numeric'">warningRed</xsl:when>
<xsl:otherwise>normal</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="ColVal" />
</span>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
请注意,<td>
如果有多个 ,这将创建多个Col[ColName='Test2']
。
输出是:
<table>
<tr>
<td class="warningTd">
<span class="warningRed">TestVal2</span>
</td>
</tr>
</table>
推荐阅读
- php - Xampp 在其服务中显示十字标志
- php - Vuejs从从php服务器收到的日期开始修复日期
- vue.js - 如何根据 API 获取的内容创建列表 + 详细信息页面
- c# - 'Sap.Data.Hana.HanaConnection' 的类型初始化程序引发了异常。---> System.IO.FileNotFoundException: 找不到 libADONETHDB.dll
- node.js - 如何使用节点 oidc 提供程序获取用户信息
- api - 使用 C# 计算 bitbns 加密交换 API 的有效负载和签名(C# 等效于 javascript 示例代码不起作用)
- docker - Docker 容器不健康但日志中没有错误
- javascript - 在chartjs中绘制数学函数
- google-apps-script - 你如何从谷歌脚本执行 .bat 文件?
- java - 在android 7及更高版本中运行我自己的应用程序时如何禁用语音通话和移动数据