我正在为 XSLT 文件中的元素设置样式,因此所有样式都在此处完成。我想从最后一个列表元素中删除底部边框,但不知道如何在 XSLT 中应用它。这是我的代码:
<xsl:element name="div">
<xsl:attribute name="style">
<xsl:text>width:120px; margin:0 auto; padding: 0; border: 1px solid black; border-radius: 15px;padding-bottom: 20px; background: #6A819E; margin-top: 20px;</xsl:text>
</xsl:attribute>
<xsl:element name="ul">
<xsl:attribute name="style">
<xsl:text>width:120px; margin:0 auto; padding: 0; background: #6A819E;</xsl:text>
</xsl:attribute>
<xsl:for-each select="flights/flight">
<xsl:apply-templates select="route" />
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="route">
<xsl:element name="li">
<xsl:attribute name="style">
<xsl:text>list-style-type:none; width:120px; margin:0 auto; margin-top: 20px; border-bottom: 1px solid black; text-align:center; background: #6A819E;</xsl:text>
<xsl:if test="position() = last()">border: none;</xsl:if>
</xsl:attribute>
<a><xsl:attribute name="href">map.php?a=<xsl:value-of select="from/latitude" />&b=<xsl:value-of select="from/longitude" />&c=<xsl:value-of select="to/latitude" />&d=<xsl:value-of select="to/longitude" />&e=<xsl:value-of select="routename" /></xsl:attribute><xsl:attribute name="style">
<xsl:text> text-decoration:none; color:black;</xsl:text>
</xsl:attribute>
<xsl:value-of select="routename" />
</a>
</xsl:element>
你可以在列表样式中看到我在末尾应用 last-child,我现在这是错误的,但我想不出另一种方法来做到这一点。我还可以问一下,这是使用 XSLT 文件应用样式的正确方法吗?
最佳答案
你应该尝试这样的事情:
<xsl:attribute name="style">
<xsl:text>list-style-type:none; width:120px; margin:0 auto; margin-top: 20px; border-bottom: 1px solid black; text-align:center;</xsl:text>
<xsl:if test="position() = last()">border: none;</xsl:if>
</xsl:attribute>
您还可以使用 CSS 类而不是使用内联样式。在这种情况下,您应该能够使用 CSS 中的 last-child 选择器(但是,该选择器在 IE7、IE8 ( http://caniuse.com/#search=%3Alast-child ) 中不受支持。
更新 1:如果路由元素中插入了文本节点,上述方法将不起作用,因此另一种方法是对最后一个元素使用不同的模板。
<xsl:template match="route[last()]">
<!-- Special behavior for last element -->
</xsl:template>
更新 2:使用 if 语句同时忽略与路由不同的所有节点的另一种选择是:
<xsl:if test="not(following-sibling::route)">border:none;</xsl:if>
关于css - 是否可以将 last-child 与内联样式一起使用而不是在 CSS 文件中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920210/