2015-02-05 63 views
0

我对XSLT很陌生。我有以下的代码,我相信可以作出更多的清洁/干:使用xsl:属性时在哪里放置子元素?

<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> 

理想我只想在锚改变href。我读过你可以这样做:

<xsl:element name="a"> 
    <xsl:attribute name="href"> 
     <xsl:choose> 
      <xsl:when test="custom-link"> 
       <!-- Custom link --> 
       <xsl:value-of select="custom-link"/></xsl:text> 
      </xsl:when> 
      <xsl:otherwise> 
       <!-- Organic link --> 
       <xsl:value-of select="organic-link"/></xsl:text> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:attribute> 
</xsl:element> 

但我不太明白如何把链接值

回答

1

脱离上下文来评估代码是非常困难的。我相信以下内容:

<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> 

简化了您现在所拥有的功能 - 所以如果工作正常,应该这样做。但我无法测试它。

1

另一种方法是使用xsl-attribute

<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> 
+0

这不起作用:您不能使用*属性值模板*和''的'select'属性。 – 2015-02-05 16:09:43

+0

你仍然在模仿他的道路,你刚刚拿出了{},就好像他们没有任何意义。他们是这样。 – 2015-02-05 16:37:50

+0

您的代码将查找源XML文档中(单个)节点的值。该节点的路径从根元素“/ film”开始。无论在该节点上发现什么(如果它存在*)都将是“href”属性的内容。我的代码查找**两个**节点的内容,然后将其与**文本文本**结合起来 - 所以最后,“href”属性的内容(即生成的HTML文档中的实际链接)将会是一个以'/ film'目录开头的URL。所以这是两个非常不同的结果。 - (*)我可以告诉你确定节点不存在,因为... – 2015-02-05 16:59:02

相关问题