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"?>
<books>
    <book title="Advanced C#">
        <content>
            <![CDATA[ here is the content of the book ]]>
        </content>
    </book>
    ...
</books>


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"?>
<books>
    <book title="Advanced C#">
        <content>
            <![CDATA[ ... the output of this function will be <![CDATA[ bla bla bla ]]> ... ]]>
        </content>
    </book>
    ...
</books>



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




Using:

Javascript

now in your JavaScript you can use it like :

// the mode
if(site.mode == 'normal')
{
   // your normal mode script
   MyPhotoGallery.Init();
}else{
   // 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
http://stackoverflow.com/questions/3336424/does-xslt-have-split-function

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:choose>
        <xsl:when test="not(contains($text, $separator))">
            <item>
                <xsl:value-of select="normalize-space($text)"/>
            </item>
        </xsl:when>
        <xsl:otherwise>
            <item>
                <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
            </item>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>



2- Calling the template :

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