5

See related question (Using XSLT within Schema to manipulate HTML output from Component)

I have added the following to the XSLT for a content field within a schema...

<template match="*[contains(@class, &apos;cta-button-secondary&apos;)]">
    <copy>
        <attribute name="href">
        <value-of select="./@href"></value-of>
        </attribute>
    <attribute name="class">cta-button secondary</attribute>
    <element name="span" namespace="http://www.w3.org/1999/xhtml">
        <value-of select="."></value-of>
    </element>
    </copy>
</template>

This works perfectly in the W3C XSLT debugger but when I try and use it in Tridion it doesn't seem to be applying. I am aware that this should be executed when the field content is updated rather than on publish but it doesn't seem to work...

The XSLT should find any <a> elements with a class="cta-button-secondary" and replace the class with class="cta-button secondary" (note: 2 classes instead of 1) and inject a <span> element around the <a> value.

Full XSLT:

<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(., &apos; &apos;, &apos;&apos;)) = &apos;&apos;     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(., &apos; &apos;, &apos;&apos;)) = &apos;&apos;        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="*[contains(@class, &apos;cta-button-primary&apos;)]">
        <copy>
            <attribute name="href">
            <value-of select="./@href"></value-of>
            </attribute>
        <attribute name="class">cta-button primary</attribute>
        <element name="span" namespace="http://www.w3.org/1999/xhtml">
            <value-of select="."></value-of>
        </element>
        </copy>
    </template>
    <template match="*[contains(@class, &apos;cta-button-secondary&apos;)]">
        <copy>
            <attribute name="href">
            <value-of select="./@href"></value-of>
            </attribute>
        <attribute name="class">cta-button secondary</attribute>
        <element name="span" namespace="http://www.w3.org/1999/xhtml">
            <value-of select="."></value-of>
        </element>
        </copy>
    </template>
    </stylesheet>

Full XML:

<Content xmlns="uuid:3f71252b-6e99-47f2-8906-ff4488c188a1">
    <body>
        <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="#" class="cta-button-primary">Annual report and accounts 2011</a> or <a href="#" class="cta-button-secondary">Our 2011 achievements.</a></p>
    </body>
</Content>

Any help with this would be appreciated...

3

I don't think you need to add namespace while creating span tag, try removing namespace.

SPAN is a valid tag and it's not required to add on new-inline-tags

Update:

While going through your XSLT, I see that you have written a template which can match for any element and it tries to create a href attribute which looks for @href attribute.

But actually, you are trying to target only <a> tags not all other tags.

Use below XSLT which can be used as a single template match, which does for you

<template match="a">
    <choose>
        <when test="*[contains(@class, &apos;cta-button-primary&apos;)]">
            <copy>
                <attribute name="href">
                    <value-of select="./@href"/>
                </attribute>
                <attribute name="class">cta-button primary</attribute>
                <element name="span">
                    <value-of select="."/>
                </element>
            </copy>
        </when>
        <when test="*[contains(@class, &apos;cta-button-secondary&apos;)]">
            <copy>
                <attribute name="href">
                    <value-of select="./@href"/>
                </attribute>
                <attribute name="class">cta-button secondary</attribute>
                <element name="span">
                    <value-of select="."/>
                </element>
            </copy>
        </when>
    </choose>
</template>
  • I had already removed the namespace whilst tinkering and even with an empty span element it still needed inclusion within this config file before it started working. – John Askew Apr 29 '13 at 16:03
  • @JohnAskew: The field "new-inline-tags" is used for non-valid tags. But in your case, am thinking what are the possibilities of not working – Siva Charan Apr 29 '13 at 16:10
  • I'm not sure why. Had me scratching my head too but I'll mark this as the correct answer as it should work. – John Askew May 1 '13 at 8:09
4

This is now working. The fix was...

Had to add span to new-inline-tags in %TRIDION_HOME%\WebUI\Core\Controls\FormatArea\TcmXhtml\TcmXHTML.config for this to work.

If the element I am adding is not present in this config file under new-inline tags then the XSLT will fail and not apply the template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.