Monday, 25 February 2013

XML >> How to put CDATA inside CDATA

In order to put <![CDATA[ in a <![CDATA[ tag you must escape the second one. so lets assume that i have the following XML document

<?xml version="1.0" encoding="utf-8"?>
    <book title="Advanced C#">
            <![CDATA[ here is the content of the book ]]>

till now all was fine, so no problem but what if the content of the book already contains a <![CDATA[ tag ?

<?xml version="1.0" encoding="utf-8"?>
    <book title="Advanced C#">
            <![CDATA[ ... the output of this function will be <![CDATA[ bla bla bla ]]> ... ]]>

now the format of the XML file is invalid due to ]]> inside the parent <![CDATA[ tag. you can solve this by escaping the closing tag ]]> by replacing it by ]]]]><![CDATA[> so

<![CDATA[ ... text text text <![CDATA[ bla bla bla ]]> ... ]]>

will be
<![CDATA[ ... text text text <![CDATA[ bla bla bla ]]]]><![CDATA[> ...  ]]>

hope this help :)

Monday, 17 December 2012

PHP Array casting >> Using foreach with a variable which may NOT be an array

simply cast the variable to array:

$keywords = (array) $keywords;
if the variable is an array no thing will happen , otherwise it will be transformed to an array e.g.

array(1,'2','item3') => array(1,'2','item3')
1 => array(1)
'keyword' => array('keyword')

Wednesday, 14 November 2012

Sharepoint 2010, edit mode branding

During my branding, I felt that I need to distinguish between the Edit mode and the Display Mode, example:

  • I need to run some JavaScript  only in display mode and not in edit mode like photo galleries, sliders.
  •  hide some unused element with CSS in edit mode 
  • make the site 100% width in order to free up the Work Space

So I followed the following strategy:

  1. in the Master page I detect the site mode
  2. store the mode in a JavaScript variable
  3. on page load add a CSS class to the body depending on the site mode

In the Master page 

Register the PublishingWebControls in the master page

<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Add the following in the head section after loading jQuery and before referencing any other JavaScript file 

<script type="text/javascript">
     var site = {
      dir : '<asp:Literal runat="server" Text="<%$Resources:wss,multipages_direction_dir_value%>"  />',
      lang : '<asp:Literal runat="server" Text="<%$Resources:wss,language_value%>"  />'
<PublishingWebControls:EditModePanel runat="server">
    <script type="text/javascript">site.mode = 'edit'</script> 
<script type="text/javascript">
  $(function(){ $('body').addClass('mode-' + site.mode) })



now in your JavaScript you can use it like :

// the mode
if(site.mode == 'normal')
   // your normal mode script
   // your edit mode script

// Direction or Language 
if(site.dir == 'rtl'){
  // right to left direction 

  alert("the direction is :" + site.dir +" and the language is : "  site.lang);

In Css

.mode-edit .myDiv{border:1px solid red}
.mode-diplay .myDiv{border:1px solid #000}

Tuesday, 13 November 2012

String split in XSLT 1.0

based on a question on stackoverflow

In XSLT 1.0 there are no split function.
so we can define it like :

1- Define the template

<xsl:template match="text/text()" name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="separator" select="','"/>
        <xsl:when test="not(contains($text, $separator))">
                <xsl:value-of select="normalize-space($text)"/>
                <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>

2- Calling the template :

<xsl:call-template name="tokenize">
   <xsl:with-param name="string" select="'item1, item2, item3'" />