如何使用Diazo规则计算href值

时间:2012-11-09 22:02:24

标签: diazo

我正在尝试使用从内容中选择的值计算网址来更改主题中某些href元素的<a>。但是,我根本无法弄清楚如何更改href属性。似乎attributes规则中未理解<replace>属性。

最终,我希望能够做到这样的事情:

<replace css:theme="a.languageswitcher" attributes="href">
  <!-- use some XSL logic here to stitch together the new href -->
</replace>

所以以下规则有效,但对我没用:

<copy attributes="href" css:theme="a.languageswitcher" css:content="#portal-logo" />

<merge attributes="href" css:theme="a.languageswitcher" css:content="#portal-logo" />

但是这个已经不起作用,attributes="href"使得这个规则被忽略了。

<replace attributes="href" css:theme="a.languageswitcher" css:content="#portal-logo" />

另一方面,如果我尝试从头开始重建<a>元素,那么我会在他的问题中遇到@ ross-patterson所描述的错误:Diazo - Conditionally add a class to a theme element

    <replace theme="//a[@class='languageswitcher']">
      <a class="languageswitcher">
        <xsl:attribute name='href'>
          foo
        </xsl:attribute>
      </a>
    </replace>

产生错误:

XSLTApplyError: xsl:attribute: Cannot add attributes to an element if children have been already added to the element.

如何做到这一点?

3 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

<replace css:theme="a.languageswitcher">
    <xsl:for-each css:select="a.languageswitcher">
        <xsl:variable name="link">
            http://example.com
        </xsl:variable>
        <a href="{$link}"><xsl:copy-of select="." /></a>
    </xsl:for-each>
</replace>

答案 1 :(得分:2)

在我使用此规则的主题中

<replace css:content-children="#hptoolbar-cerca"><xsl:attribute name="href">
<xsl:value-of select="//li[@id='portaltab-catalog']//a/@href" /></xsl:attribute><xsl:apply-templates select="node()"/></replace>

使用内容中另一个元素的href修改#hptoolbar-cerca的href。

也许这对你有用。

维托

答案 2 :(得分:0)

我的情况类似。我对我的主题代码感到满意,只想“替换”其href值。在Chrome检查器中复制XPath后,最简单的规则如下所示:

<copy attributes="href"
       theme="/html/body/div[2]/div[1]/div/div[1]/a"
     content="//*[@id='portal-logo']" />

我希望成功地将此技巧应用于所有href值,但有些值不起作用。我发现这些<a>元素位于JavaScript操作的代码块中,用于实现Slide效果。我删除所有相关的JavaScript代码,并再次复制其“真正的”XPath,最后一切正常,如预期的那样。希望这有用。

相关问题