2

我试图通过使用我从内容中选择的值计算 URL 来更改主题href中某些元素的 s 。<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.

如何才能做到这一点?

4

3 回答 3

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>
于 2013-05-29T15:01:14.203 回答
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。

也许这对你有用。

维托

于 2012-11-22T07:06:21.850 回答
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 操作的代码块中,用于实现幻灯片效果。我删除了所有相关的 JavaScript 代码,并再次复制了它的“真实”XPath,最后一切正常。希望这有帮助。

于 2014-07-19T00:49:45.657 回答