天天看点

XSL常见问题及解决(一)如何实现给html中标签li的id自动按序号命名

1、如果xml文件中已经有id,且该id是按照规律命名,我们只需要在xsl中读取该属性的值,然后为li赋值即可

xsl:

<xsl:template match="sec" mode="nav-item">
    <li class="navItem">
        <xsl:variable name="id">
            <xsl:if test="@id">
                <xsl:value-of select="@id"/>
            </xsl:if>
        </xsl:variable>
        <a href="#{$id}">
            <xsl:apply-templates select="title" mode="nav-item"/>
        </a>
    </li>
</xsl:template>      

xml:

XSL常见问题及解决(一)如何实现给html中标签li的id自动按序号命名

2、id可以用attribute元素去实现,元素的属性name为“id”;元素的值即为id名称

因为没有现成更的例子,暂时以a标签的href属性和img标签的src&alt属性为例,举一反三

xsl:

<a target="_blank">
    <xsl:attribute name="href">
        <xsl:value-of
                select="fig/alternatives/graphic[@specific-use='big']/@xlink:href"/>
    </xsl:attribute>
    <img>
        <xsl:attribute name="src">
            <xsl:value-of
                    select="fig/alternatives/graphic[@specific-use='big']/@xlink:href"/>
        </xsl:attribute>
        <xsl:attribute name="alt">
            <xsl:value-of
                    select="fig/alternatives/graphic[@specific-use='big']/@xlink:href"/>
        </xsl:attribute>
    </img>
</a>      

继续阅读