1

What is the best way to strip/replace the {} characters from the Sitecore item @id output in xslt?

Problem: I have to identity certain tags in my html from the id attribute. Using names is dangerous because of the risk of the Sitecore end user typing spaces or illegal characters in the name.

On the other hand using the item id causes the id in the html to say: id="{xxxxxxxx-xxx(...)}, in which case the {} are illegal as characters in html id attribute.

So: What is the best way to strip/replace the {} characters from the xslt @id output?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • Is the problem that you are using AVT and don't know how to represent the curly vrackets inside it? The answer is, they must be escaped by doubling. So, inside an AVT use `{{` instead of `{` and `}}` instead of `}` – Dimitre Novatchev May 22 '11 at 16:10

4 Answers4

2

I am not familiar with Sitecore but with XSLT/XPath the expression translate(@id, '{}', '') should suffice to remove any curly braces from the id attribute value. Be careful however with any XSLT code using attribute value templates as there the curly braces have a special meaning. So <xsl:value-of select="translate(@id, '{}', '')"/> is safe as the select attribute is not treated as an attribute value template.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

You can do it like this to include only the hyphens:

item.ID.Guid.ToString("D")

Alternatively you can use the following formats:

  1. D: hyphens fed3f822-e79f-4318-a99d-aaf75feea459
  2. N: digits fed3f822e79f4318a99daaf75feea459
  3. B: braces {fed3f822-e79f-4318-a99d-aaf75feea459}
  4. P: parenthese (fed3f822-e79f-4318-a99d-aaf75feea459)
nologo
  • 5,918
  • 3
  • 36
  • 50
1

Right way to do this would be with NormalizeGuid.

I have used before (on Sitecore V5) NormalizeGuid Method from MainUtils. I just tested on Sitecore 6.2 but it is breaking because there are 2 same method names. This results in an XslTransformException

Which version of Sitecore you are using? I suggest trying out NormalizeGuid:

Goes something like:

Web.Config

<extension mode="on" type="Sitecore.MainUtil, Sitecore.Kernel" namespace="http://www.sitecore.net/util" singleInstance="true"/>

XSLT

xmlns:util="http://www.sitecore.net/util" 

and

<xsl:variable name="itmId"><xsl:value-of select="@id"/></xsl:variable>
<xsl:value-of select="util:NormalizeGuid($itmId)"/>

If you get RTE it is possible to fix it with a custom wrapper class. Take a look this post.

Actually, I think you can get around this by using GenerateShortID() it's also in MainUtil.

Community
  • 1
  • 1
jpkeisala
  • 8,566
  • 7
  • 30
  • 39
1

Thanks Martin, using the translate() function helped me out, but your answer is not complete.

The question - as I understand it - was concerning stripping curly braces inside angle braces in HTML, and here <xsl:value-of select="..."/> unfortunately wont work.

Consider this (illegal) code:

<div id="<xsl:value-of select="translate(@id, '{}', '')"/>">`

Instead, use this:

<div id="{translate(@id, '{}', '')}">
Brad Mace
  • 27,194
  • 17
  • 102
  • 148