我想将XML元素的值与字符串进行比较,并根据结果分配变量。但是这不起作用所以我把if语句设为1 = 1或另一次尝试是1 ='1',但是仍未分配变量,我假设if语句没有触发。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="id"/>
<xsl:template match="/">
<xsl:if test="mainData/info/country[@name=$id]">
<xsl:for-each select="mainData/info/country[@name=$id]">
<xsl:if test="1 = 1"><!--should always fire-->
<xsl:variable name="color" select='"red"'/>
</xsl:if>
<div class="modal-content">
<div class="row" id="top-content">
<div class="col 6 top-modal">
<div class="card card-modal">
<div class="card-image">
<img alt="Country Flag"><xsl:attribute name="src"><xsl:value-of select="flag" /></xsl:attribute></img>
</div>
<div class="card-content">
<span class="card-title"><xsl:value-of select="@name"/></span>
</div>
</div>
</div>
<div class="col 6 top-modal">
<div class="card card-modal">
<div class="card-panel blue accent-3 modal-card-info">
<span class="white-text">
<h6>Region: <xsl:value-of select="facts/Region"/></h6>
<h6>Code: <xsl:value-of select="facts/Abbreviation"/></h6>
<h6>Capital: <xsl:value-of select="facts/Capital"/></h6>
<h6>Languages: <xsl:value-of select="facts/Language"/></h6>
<h6>Population: <xsl:value-of select="facts/Population"/></h6>
<h6>Currency: <xsl:value-of select="facts/Currency"/></h6>
<h6>All Games:</h6>
<h6>Golds: <xsl:value-of select="medals/gold"/></h6>
<h6>Silvers: <xsl:value-of select="medals/silver"/></h6>
<h6>Bronze: <xsl:value-of select="medals/bronze"/></h6>
<h6>Bronze: <xsl:value-of select="$color"/></h6><!-- check the var-->
</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col 10">
<div class="card-panel blue accent-3">
<span class="white-text"><xsl:value-of select="description"/></span>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Close</a>
</div>
</xsl:for-each>
</xsl:if>
<xsl:if test="not(mainData/info/country[@name=$id])">
<div class="modal-content">
<div class="row">
<div class="col 10 no-data">
<div class="card-panel center-align grey darken-3">
<span class="white-text center-align ">No Data Available</span>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Close</a>
</div>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
xsl:if
正在解雇。您的问题是变量的范围。
<xsl:if test="1 = 1"><!--should always fire-->
<xsl:variable name="color" select='"red"'/>
</xsl:if>
color
变量的范围仅限于xsl:if
的主体,而不是其他任何地方,因此您无法在其他地方使用它。
解决方案是将其更改为此,并将条件放在变量声明
中<xsl:variable name="color">
<xsl:if test="1 = 1">
<xsl:text>red</xsl:text>
</xsl:if>
</xsl:variable>
如果您有多种颜色选择,可以将其替换为xsl:choose
。