天天看點

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>      

繼續閱讀