如何将数字附加到XSL文件中的ID

时间:2014-11-07 20:42:54

标签: jquery xml xslt

我有以下XSL文件,它将重复多次(在我的情况下是4次):

<xsl:if test="Html/root/lcGroup/txtCity != ''">
    <div id="labOCSign" class="tableHeading"></div>
    <h4>
        <span id="spnCity">
            <xsl:value-of select="Html/root/lcGroup/txtCity" />
        </span>
        <xsl:if test="Html/root/lcGroup/txtZip != ''">
            <xsl:text>, </xsl:text>
            <xsl:value-of select="Html/root/lcGroup/dlState" />
            <xsl:text>&#160;</xsl:text>
            <xsl:value-of select="Html/root/lcGroup/txtZip" />
        </xsl:if>
    </h4>
</xsl:if>

CSS:

.tableHeading
{
    background: #E5E5E5 url("../theImages/recommendationBadge.png") top left no-repeat;
    padding-top: 60px;
    padding-bottom: 25px;
}
.tableHeading2
{
    background: #E5E5E5 url("../theImages/recommendationBadge2.png") top left no-repeat;
    padding-top: 60px;
    padding-bottom: 25px;
}

我有以下JQuery将替换labOCSign

的类
var cityName = $(".spnCity").text();
var weekDay = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][(new Date()).getDay()];

if (cityName.toLowerCase() == "rye") {
    if (weekDay == "Monday" || weekDay == "Wednesday" || weekDay == "Friday") {
        if (validNow("08:00AM", "5:30PM")) {
            $("#labOCSign").removeClass("tableHeading2").addClass("tableHeading");
        }
        else {
            $("#labOCSign").removeClass("tableHeading").addClass("tableHeading2");
        }
    }
}
else if (cityName.toLowerCase() == "new roc") {
    if (weekDay == "Monday" || weekDay == "Wednesday" || weekDay == "Friday") {
        if (validNow("08:00AM", "5:30PM")) {
            $("#labOCSign").removeClass("tableHeading2").addClass("tableHeading");
        }
        else {
            $("#labOCSign").removeClass("tableHeading").addClass("tableHeading2");
        }
    }
}

我现在遇到的问题是脚本只占用一个实例。

如何在spanCity之后添加一个数字,以确保它们在XSL文件中都是唯一的?

所以,它将是#spnCity1,#spnCity2,#spnCity3,#spnCity4 ......

1 个答案:

答案 0 :(得分:2)

如果提供的XSLT部分是例如在<xsl:for-each>循环中,您可以使用position()获取递增的值(循环中的当前位置)和concat()

<span id="concat('spnCity', position())">

更新,因为建议的方法在设置中无效:

应该有两种不同的符号:

<span>
  <xsl:attribute name="id" select="concat('spnCity', position())"/>
</span>

<span>
  <xsl:attribute name="id">
    <xsl:value-of select="concat('spnCity', position())"/> 
  </xsl:attribute>
</span>

可以在属性之后添加内容:

<span>
  <xsl:attribute name="id">
    <xsl:value-of select="concat('spnCity', position())"/> 
  </xsl:attribute>
  <xsl:value-of select="Html/root/lcGroup/txtCity" />
</span>

刚刚创建了两个版本的Demo