2

I am trying to change the hrefs of certain <a> elements in the theme by computing the URLs using values I select from the content. However, I can't figure out how to change the href attribute at all. It seems like the attributes attribute is not understood in the <replace> rule.

Ultimately, I would like to be able to do something like:

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

So the following rules work, but are useless to me:

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

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

But this one already does not work, the attributes="href" makes it so this rule is ignored.

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

On the other hand, if I try to rebuild the <a> element from scratch, then I run into the error described by @ross-patterson in his question: 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>

produces the error:

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

How can this be done?

Community
  • 1
  • 1
fulv
  • 1,016
  • 1
  • 9
  • 18
  • This is really tricky IMO.... faced with the same problem & after a lot of trying, I gave up and just made sure the link was somewhere in my Plone content so I could use a rule to pull it in – Danimal Jun 16 '15 at 12:41

3 Answers3

3

Something like this ought to work:

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

in a theme I used this rule

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

to modify an href of #hptoolbar-cerca with the href from another element in the content.

Maybe this could be useful for you.

Vito

Vito
  • 1,201
  • 1
  • 10
  • 16
0

My case is similar. I am happy with my theme code, only want to "replace" its href values. With XPath copied in the Chrome inspector, the simplest rule looks like this:

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

I expect to successfully apply this trick to all the href values, but some do not work. I find these <a> elements are in the code block manipulated by JavaScript for implementing Slide effect. I remove all its related JavaScript codes, and copy its "real" XPath again, finally everything works fine as expected. Hope this helpful.

marr
  • 861
  • 4
  • 19