Xml 使用xsl:attribute时将子元素放置在何处?

Xml 使用xsl:attribute时将子元素放置在何处?,xml,xslt,Xml,Xslt,我对XSLT很陌生。我有以下代码,我相信可以使其更干净/干燥: <xsl:choose> <xsl:when test="custom-link"> <!-- Custom link --> <a class="no-ajax" href="{custom-link/item/@handle}"> <img src="//images.mysite.com/2/1120/630/

我对XSLT很陌生。我有以下代码,我相信可以使其更干净/干燥:

<xsl:choose>
    <xsl:when test="custom-link">
        <!-- Custom link -->
        <a class="no-ajax" href="{custom-link/item/@handle}">
            <img src="//images.mysite.com/2/1120/630/5{lead-image/@path}/{lead-image/filename}" alt="" />
        </a>
    </xsl:when>
    <xsl:otherwise>
        <!-- Organic link -->
        <a class="no-ajax" href="/film/{primary-category/item/@handle}/{film-title/@handle}/">
            <img src="//images.mysite.com/2/1120/630/5{lead-image/@path}/{lead-image/filename}" alt="" />
        </a>
    </xsl:otherwise>
</xsl:choose>

但是我不太明白如何将链接值放进去。

另一种方法是使用
xsl属性

<a class="no-ajax>
    <xsl:attribute name="href">
        <xsl:choose>
            <xsl:when test="custom-link">
                <xsl:value-of select="custom-link/item/@handle"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat('/film/', primary-category/item/@handle, '/', film-title/@handle, '/')"/>
             </xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
    <img src="//images.mysite.com/2/1120/630/5{lead-image/@path}/{lead-image/filename}" alt="" />
</a>

断章取义地评估代码是非常困难的。我认为:

<a class="no-ajax">
    <xsl:attribute name="href">
        <xsl:choose>
            <xsl:when test="custom-link">
                <xsl:value-of select="custom-link/item/@handle"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat('/film/', primary-category/item/@handle, '/', film-title/@handle, '/')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
    <img src="//images.mysite.com/2/1120/630/5{lead-image/@path}/{lead-image/filename}" alt="" />
</a>


是你现在拥有的流线型的等价物-因此,如果这是有效的,那么这也应该是有效的。但是我没有办法测试它。

这行不通:你不能将属性值模板与
select
属性一起使用。你仍然在复制他的路径,你只是取出了
{}
,好像它们没有任何意义。代码在源XML文档中查找(单个)节点的值。该节点的路径从根元素
/film
开始。在该节点上找到的内容(如果存在*)将是
href
属性的内容。我的代码查找两个节点的内容,然后与文本相结合-因此,最后,
href
属性的内容(即生成的HTML文档中的实际链接)将是一个以“/film”目录开头的URL。这是两个截然不同的结果(*)我可以肯定地告诉您节点不存在,因为。。。。。。因为属性不能有子元素。
<a class="no-ajax">
    <xsl:attribute name="href">
        <xsl:choose>
            <xsl:when test="custom-link">
                <xsl:value-of select="custom-link/item/@handle"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat('/film/', primary-category/item/@handle, '/', film-title/@handle, '/')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
    <img src="//images.mysite.com/2/1120/630/5{lead-image/@path}/{lead-image/filename}" alt="" />
</a>