Showing posts with label XPath. Show all posts
Showing posts with label XPath. Show all posts

Wednesday, March 14, 2012

Unique Item

If we have a list with items which one of the columns can be the same, like this for example:
Offices
Location
Office 1
Israel
Office 2
Italy
Office 3
Israel

And we need to select a distinct item from this list in a DVWP. Add this row to your dvt_1.body
<xsl:for-each select="$Rows[not(@Location = preceding-sibling::*/@Location)]">
This will check the preceding sibling if it has the same name, if true it won’t display that item if it is already exists.
For example:
1.   <xsl:template name=dvt_1.body>  
2.     <xsl:param name=Rows/>  
3.     <xsl:for-each select=$Rows[not(@Location = preceding-sibling::*/@Location)]>  
4.       <xsl:call-template name=dvt_1.rowview” />  
5.     </xsl:for-each>  
6.   </xsl:template>  

Substring In DVWP

If we have column with link in the code, like this for example:
<xsl:value-of select=”@Title” />
But in the page it looks like this:
30;#Dolev
We need to set in the code to take only the string after the #. So we will write:
<xsl:value-of select=”{substring-after(@Title,’;#’)}” />.
As same as if it reversed: Dolev#;21 – we will write:
<xsl:value-of select=”{substring-before(@Title,’#;’)}” />.

Format Date

If we want to filter DVWP to: Created Date > Today, we need to format the Created and format the Today().  In addition we need to “tell” the DVWP that the string is number. So it will look like this in the XPath editor:
[(number(ddwrt:FormatDateTime(ddwrt:FormatDate(string(@Created),1033,1),1033,’ddMMyyyy’))= number(ddwrt:FormatDateTime(ddwrt:FormatDate(string(ddwrt:Today()),1033,1),1033, ’ddMMyyyy’)))]
@Created The selected column.
'ddMMyyyy' The selected format of date (29/07/1989 -> 29071989)

In this XPath code we have to format both of them so they will be able to communicate with each other.

By this format of ddwrt:FormatDate(@Created),1033,’ddMMyyyy’) you can format the date to be as you wish. For example:
ddMMyyyy – 29071989
dd/mm/yy – 29/07/89
dd MMM yyyy – 29 Jul 1989
dd, MMMM – MM, yyyy – 29, July – 07, 1989