Sitecore item guid without curly braces - xslt

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?

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.

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.

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, '{}', '')}">

You can do it like this to include only the hyphens:
item.ID.Guid.ToString("D")
Alternatively you can use the following formats:
D: hyphens fed3f822-e79f-4318-a99d-aaf75feea459
N: digits fed3f822e79f4318a99daaf75feea459
B: braces {fed3f822-e79f-4318-a99d-aaf75feea459}
P: parenthese (fed3f822-e79f-4318-a99d-aaf75feea459)

Related

Jmeter Regular Expression Extraction

Could someone help me in getting the value "1237857346" from the following using regex or any other way I could get the value "1237857346" in from HTML in JMeter.
<select class="card_account" name="existing__account">
<option value="" disabled="disabled">Card Number</option>
<option value="1237857346" selected="selected">************4567</option>
</select>
Little bit of background. I am using JMeter and trying to extra the value "1237857346" to pass it in the next request.
It is not very good idea to parse HTML using Regular Expressions as it evidenced by the famous Stack Overflow answer
I would suggest switching to XPath Extractor instead. Add the XPath Extractor as a child of HTTP Request sampler which returns that select and configure it as follows:
XML Parsing Options: tick Use Tidy box. It may not be necessary but if your server response is not XML/XHTML compliant you'll get nothing
Reference Name: anything meaningful, i.e. value - it will be the name of the variable holding extracted data
XPath Expression: //select[#class='card_account']/option[#selected='selected']/#value - it will take
select having class = card_account
option with selected = "selected"
value attribute of the above option
and store it to "value" variable. You will be able to refer to it as ${value} where required.
See following material for further reference:
XPath Tutorial
XPath Language Specification
Using the XPath Extractor in JMeter
You can use the following regex:
<option[^<]*>Card Number</option>\s*<option[^<]*?value="(\d+)"
The value will be in group 1 ($1$), which is exactly what you need.
See demo
In case the are always 12 asterisks (that can be matched with \*{12}) in the <option> node value, you'd can use:
<option[^<]*value="(\d+)"[^<]*>\*{12}\d+</option>
See another demo.

Binding HTML strings in Ember.JS

I am using a third party indexing service (Swiftype) to search through my database. The returned records contains a property called highlight. This simply adds <em> tags around matching strings.
I then bind this highlight property in Ember.JS Handlebars as such:
<p> Title: {{highlight.title}} </p>
Which results in the following output:
Title: Example <em>matching</em> text
The browse actually displays the <em> tags, instead of formatting them. I.e. Handlebars is not identifying the HTML tags, and simply printing them as a string.
Is there a way around this?
Thanks!
Handlebars by default escapes html, to prevent escaping, use triple brackets:
<p> Title: {{{highlight.title}}} </p>
See http://handlebarsjs.com/#html-escaping
Ember escapes html because it could be potentional bad code which can be executed. To avoid that use
Ember.Handlebars.SafeString("<em>MyString</em>");
Here are the docs
http://emberjs.com/guides/templates/writing-helpers/
if you've done that you could use {{hightlight.title}} like wished,...
HTH

Umbraco copy-of not showing xml markup

I’m using Umbraco 4.7.0
My goal is to get the image path from a hard coded media node id of 4191. If I create a new macro with the code:
<xsl:copy-of select="umbraco.library:GetMedia(4191, false())"/>
I get the output:
/media/17675/my image.jpg50033618497jpg
I was expecting some well formed xml, however, it appears I’m missing all the tags. I therefore cannot reference the path for the image directly.
Am I missing something really simple here?
EDIT
I discovered how to get the raw xml output from my copy-of statement. I needed to wrap it in a <textarea> tag:
<textarea>
<xsl:copy-of select="umbraco.library:GetMedia(4191, false())"/>
</textarea>
This should do it:
<xsl:copy-of select="umbraco.library:GetMedia(4191, 0)/umbracoFile"/>
See also http://our.umbraco.org/wiki/reference/umbracolibrary/getmedia

JavaScript Regx to remove certain string if a pattern is found

Lets say i have
input string as
<div id="infoLangIcon"></div>ARA, DAN, ENGLISHinGERMAN, FRA<div id="infoPipe"></div><div id="infoRating0"></div><div id="infoPipe"></div><div id="infoMonoIcon"></div>
so i want to check if inforating is 0 and then remove the div and previous div also. The output is
<div id="infoLangIcon"></div>ARA, DAN, ENGLISHinGERMAN, FRA</div><div id="infoPipe"></div><div id="infoMonoIcon"></div
Regex is not your best option here. It is not reliable when it comes to HTML.
I suggest you use DOM functions to do this (I gave you a Javascript example, you have not provided a language to be used). If I understood correctly, if there is an element with the ID of infoRating0, you want to remove it and its previous sibling. This little snippet should do that:
if (document.getElementById('infoRating0')) {
var rating0=document.getElementById('infoRating0'),
rParent=rating0.parentNode;
rParent.removeChild(rating0.previousSibling);
rParent.removeChild(rating0);
}
Also, your HTML is invalid. You can only use an ID once in your HTML. You have two divs with the same ID (infoPipe) which you should REALLY fix. Use classes instead.
jsFiddle Demo

XSLT character-escaping query

Lets say I have the following code snippet below, how do I also apply the disable-output-escaping to the {name} in the title attribute?
<a title="{name}"><xsl:value-of select="name" disable-output-escaping="yes" /></a>
This has really got me stumped.
Thanks guys.
This cannot be done with XSLT. The spec says:
It is an error for output escaping to
be disabled for a text node that is
used for something other than a text
node in the result tree.
Thus it makes no difference if you use Attribute Value Templates or xsl:attribute with xsl:value-of, because you're generating an attribute node, not a text node. It's a limitation in the language.
You can't as is. The {name} shortcut doesn't allow additional parameters. Use the <xsl:attribute> tag instead.