1

Here is my scenario: I'm working on an e-commerce site built in Sharepoint 2010. I'm making a product catalog powered by a List, and styled by an XSLT stylesheet.

The (current) problem: Certain products have promotions from time to time. I have a field in my list with the text of the promotion, plus a checkmark (yes/no) if it's on promotion. If it is, my stylesheet will display that promotion text-otherwise it'll just have the product as normal.

What I need however is a dynamic way to add a superscript number like 1 to the end of the promotion text. This is for disclaimer purposes.

On a page only containing products on promotion, this is easy enough: I just use the position() function, wrapped by <sup></sup> to increment by 1.

Here is part of my code, starting the loop:

 <div>
 <xsl:for-each select="$Rows">
 <xsl:call-template name="dvt_1.rowview" />
 </xsl:for-each>
 </div>

Then the template:

 <xsl:template name="dvt_1.rowview">
 <xsl:variable name="PromotionVar"><xsl:value-of select="@OnPromotion"/>       </xsl:variable>

Then testing for the promotion:

 <xsl:if test="$PromotionVar='Yes'">
            <p class="text-red">Promotion: <xsl:value-of select="@PromotionText1" disable-output-escaping="yes"/>
            <sup>
            <xsl:variable name="i" select="position()" />
                <xsl:copy>
                    <xsl:value-of select="$i"/>
                </xsl:copy>
                </sup>
                </p>
    </xsl:if>
 </xsl:template>

However on pages that have products that are not on promotion, this doesn't work as position() is based on all matched records.

Now if this were another scripting language like Javascript, I could just assign a variable and increment it in an if statement.. but from searching on Stackoverflow, I understand that XSLT is a functional language, and variables are actually immutable... so how should I approach this?

Here is another answer that may have what I need.. or not. It's all a bit complicated.

In XSLT how do I increment a global variable from a different scope?

Does anyone know how I should approach this? Thanks.

Community
  • 1
  • 1
rudoyan
  • 11
  • 3

1 Answers1

0

In the extreme off-chance somebody out there reads this question, I've figured out a solution using JQuery.

First, I make up some classes for my tags named "promo_super" for promotions, and "disclosure_super" for disclosures. This is in my XSLT stylesheet:

<xsl:if test="$PromotionVar='Yes'">
    <p class="text-red"><strong>Promotion: 
    <xsl:value-of select="@PromotionText1" disable-output-escaping="yes"/>
    <sup><a href="#disclosures" class="promo_super">X</a></sup>
    </strong></p>
</xsl:if>

Then in my JQuery script, which I put at the bottom of the page (so it executes after the list has been rendered):

<script>

var promoNumber = 0;

$(".promo_super").each(function() {
    var text = $(this).text();
    promoNumber = promoNumber + 1;
    text = text.replace("X", promoNumber);
    $(this).text(text);
});

In this code I create a variable that will be used to number my promotions. Then, for each instance of that class (.promo_super), I replace the 'X' in between the tags, and I increment that number by 1.

var disclosureNumber = 0;

$(".disclosure_super").each(function() {
    var text = $(this).text();
    disclosureNumber = disclosureNumber + 1;
    text = text.replace("X", disclosureNumber);
    $(this).text(text);
});
</script>

I do the same thing for my disclosures, using a different variable and class so I don't add the numbers together. Sorting order is handled by the View's Filters, so assuming both lists are sorted the same, the numbers will match up.

Maybe this isn't the most elegant solution but it seems to work fine!

rudoyan
  • 11
  • 3