css - 是否可以将 last-child 与内联样式一起使用而不是在 CSS 文件中使用?

标签 css xslt css-selectors

我正在为 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" />&amp;b=<xsl:value-of select="from/longitude" />&amp;c=<xsl:value-of select="to/latitude" />&amp;d=<xsl:value-of select="to/longitude" />&amp;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/

相关文章:

html - 如何在 <div> 中找到第二个 <p> 元素?

xslt - 在 XSLT/XPath 中,如何获取与特定名称不匹配的第一个属性?

javascript - 有没有办法让 querySelectorAll 只搜索一个元素的子元素,而不是所有的后代元素?

css - 是否可以在 CSS 动画上交替使用 z-index

javascript - 如何在 jQuery 中用动态生成的元素替换 Div 元素?

javascript - 将鼠标悬停在选项卡上并将其下拉

javascript - 要动画的元素的初始状态

java - 如何使用 XSLT 和 Java 而不是 XSLFO 创建 PDF?

jsp - 如何使用 XSLT 或 JSP 检索网站的收藏夹图标?

jQuery选择器从特定无序列表中获取li对象