I am having trouble with some XSLT Filtering. I have changed the Rich text field settings on my schema to include:
<template match="@class[.='cta-button-secondary']">
<attribute name="class">cta-button cta-button-secondary</attribute>
</template>
within the <stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
element
I expect this to find any anchor elements with a class attribute containing cta-button-secondary
(e.g. <a class="cta-button-secondary" href="#">Link</a>
) and replace the class attribute to contain cta-button cta-button-secondary (2 css classes).
After publishing it doesn't work.
I suspect it is my poor knowledge of XLST that is the problem.
I have tried alternate implementations of the same e.g.
<template match="a/@class">
<if test="contains(.,'cta-button-secondary')">
<attribute name="class">
<text>cta-button secondary</text>
</attribute>
</if>
</template>
...but with no success.
Can anyone help?
XLST:
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
<output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></output>
<template match="/ | node() | @*">
<copy>
<apply-templates select="node() | @*"></apply-templates>
</copy>
</template>
<template match="*[ (self::br or self::p or self::div) and normalize-space(translate(., ' ', '')) = '' and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) and not(following::node()[not( (self::text() or self::br or self::p or self::div) and normalize-space(translate(., ' ', '')) = '' and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) )]) ]">
<!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks -->
</template>
<template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]">
<!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space -->
<text> </text>
</template>
<template match="@class[.='cta-button-secondary']">
<attribute name="class">cta-button cta-button-secondary</attribute>
</template>
</stylesheet>
Component XML:
<Content xmlns="uuid:3f71252b-6e99-47f2-8906-ff4488c188a1">
<step_title>Expand our impact</step_title>
<heading_line_1>Expand our impact</heading_line_1>
<title_emphasis>Line 1</title_emphasis>
<intro_text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labasdsadore et dolore magna aliqua.c</intro_text>
<expand_button_label>More about this</expand_button_label>
<body>
<h3 xmlns="http://www.w3.org/1999/xhtml">How we spent it</h3>
<ol xmlns="http://www.w3.org/1999/xhtml" class="ordered">
<li>ordered list item 1</li>
<li>ordered list item 2</li>
<li>ordered list item 3</li>
<li>ordered list item 4</li>
<li>ordered list item 5</li>
<li>ordered list item 6</li>
<li>ordered list item 7</li>
<li>ordered list item 8</li>
<li>ordered list item 9</li>
<li>ordered list item 10</li>
<li>ordered list item 11</li>
<li>ordered list item 12</li>
</ol>
<p xmlns="http://www.w3.org/1999/xhtml">Thanks to the hard work of our supporters we increased what we spent on cancer services to a record £105.9 million in 2011. That's £10 million more than in 2010.</p>
<p xmlns="http://www.w3.org/1999/xhtml">For a full breakdown of these charts, take a look at our <a href="#">Annual report and accounts 2011</a> or <a href="#" class="cta-button-secondary">Our 2011 achievements.</a></p></body>
<quote xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-13343" xlink:title="Quote2"></quote>
<right_column_image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-13350" xlink:title="Wise with money graph"></right_column_image>
</Content>