为什么使用XML数据来设置HTML标签的样式是非法的?例如:
<li style="width:<xsl:value-of select="width"/>px">
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做?那里有替代方法吗?
Tom*_*lak 37
为什么我不能这样做?
Run Code Online (Sandbox Code Playgroud)<li style="width:<xsl:value-of select="width"/>px">
因为XSL 本身就是XML .这是任何东西......但不是XML.
您的意思是属性值模板:
<li style="width:{width}px">
Run Code Online (Sandbox Code Playgroud)
或更明确的表单,用于更复杂的表达式:
<li>
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="some[condition = 'is met']">thisValue</xsl:when>
<xsl:otherwise>thatValue</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</li>
Run Code Online (Sandbox Code Playgroud)
或动态属性名称(注意名称中的属性值模板):
<li>
<xsl:attribute name="{$attrName}">someValue</xsl:attribute>
</li>
Run Code Online (Sandbox Code Playgroud)
附加说明:必须在所有其他子节点之前创建属性.换句话说,保持<xsl:attribute>在顶部.
您的原始xsl格式不正确,因为您无法将xsl标记放在另一个节点中.
我认为您需要使用xsl:属性,如下所示:
<li>
<xsl:attribute name="style">
width:<xsl:value-of select="width"/>px;
</xsl:attribute>
</li>
Run Code Online (Sandbox Code Playgroud)