1,589 articles and 11,491 comments as of Monday, June 14th, 2010

Thursday, March 11, 2010

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions

Author: Marc D. Anderson
http://mdasblog.wordpress.com

Another day, another wunnerful article about the DVWP.  This time, I’m going to cover some of the most useful “built-in” Field / Node functions.  These are functions that are a part of XSL itself and help you work with nodes in the result set.  A node is a geekier term for a thing in an XML file.  Pretty much anything can be a node in XML: the entire document, a “row”, a “column”, etc.  Remember that XML provides the data and the XSL does the transformation on it, thus XSLT. Transformation is just deciding how you’d like to deal with the data coming in from the XML file.

position()

Most often, you’ll see this in the XSL like so: position() mod 2 = 1.  position() returns the current item’s position in the rowset. So if the item is number 7 of 312, position() returns 7. The value is used in the position() mod 2 = 1 clause to determine if the row is even or odd, usually for alternating row formatting using the ms-alternating CSS class.

<tr>
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">ms-alternating</xsl:attribute>
</xsl:if>

The <xsl:attribute> tag (which I failed to cover in the main part of the series) allows you to set an attribute of an HTML object.

current()

current() represents the current node in the nodeset.  What that means is that it’s the item you are currently working with.  You’ll see things like this when you set things via the dialogs:

<xsl:variable name="dvt_ParentRow" select="current()" />

The other way that you can use current() is to distinguish between columns in different lists when you have an AggregateDataSource:

<xsl:variable name="Rows" select="/dsQueryResponse/Annual/Rows/Row[@Title = current()/@Title]"/>

What the above mean is that you want to select the items in the Annual list where the Title matches the Title in the current item.  This is basically what happens when you “join” two lists in SharePoint Designer.  It’s not like a database join, but just a filter on the second list per item value in the first.

name()

You can use name() to return the name of a node.  But the cooler thing you can do with it is variably reference columns.

Here’s an example of something I built for a project that used name() to variably sort the rows.  I had links at the top of each column in the DVWP, each of which redirected back to the same page with different values for the SortField and AscDesc.  (There’s more to this; if you’d like to understand the example better, see my blog post Sorting Displays in Data View Web Parts.)

<xsl:sort select="@*[name()=$SortField]" order="{$AscDesc}" /> 

By passing in different column names for SortField and either [ascending | descending] for AscDesc, I was able to sort the rowset by any column and in either ‘direction’.  You might say “Yeah, but List View Web Parts give you that out of the box.”  That’s certainly true, but in the case, there was a *lot* of other stuff going on in the DVWP and this gave us much finer control.

count()

count() is considered a Field / Node function rather than a Math / Number function.  That sort of makes sense, but I still always look for it in the wrong place in the XPath Expression Builder when I’m showing someone how to use it.  (BTW, the descriptions in the XPath Expression Builder are often for the birds, and even after almost four years of availability, there are still “bugs” in them.  First one to email me with an example wins a free copy of the jQuery Library for SharePoint Web Services.)

Using count() is pretty straightforward, and simply returns the number of nodes (generally items) in the nodeset:

<xsl:variable name="NumberOfRows" select="count($Rows)" /> 

As with the other articles in the series, I’m not going to cover all of the functions available in the field / Node group.  Hopefully, though, going over this subset will show you a few more neat things you can do in a DVWP.

In the next article, I’ll go over some more bits and pieces that we haven’t covered yet.  It’ll be a mystery until then.

Author: Marc D. Anderson
http://mdasblog.wordpress.com

Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA.  He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes.  Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice.  Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.

Entries in this series:
  1. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
  2. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
  3. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
  4. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
  5. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
  6. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
  7. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
  8. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
  9. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
  10. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
  11. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
  12. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
  13. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
  14. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
  15. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
  16. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
  17. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
  18. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
  19. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
  20. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
  21. EUSP eBook Store: First SharePoint Title is Now Available
 

Please Join the Discussion

12 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions”
  1. Fredrik says:

    Hi,

    have never tried it and don’t even know if it’s useful, but is it possible to use your jquery for sharepoint web services combined with xsl data views? Thought of having one single central xsl file used to transform the xml data returned from the web service in all locations in a site, so that you can reuse for example a document library view… Would this work? http://www.w3schools.com/xsl/xsl_client.asp

    /Fredrik

  2. @Fredrik: I think you’re asking two separate questions.

    Yes, you can use my jQuery Library for SharePoint Web Services in conjunction with DVWPs. Think of it this way: DVWPs do the formatting on the server side, and then you can further manipulate that display on the client side with jQuery.

    Yes, I save XSL templates centrally all the time. I generally will put them into /Style Library/XSL Style Sheets in MOSS and just a Document library called XSL in WSS. (See my previous article with this tip.)

  3. Fredrik says:

    I was thinking of something like below and use the same type of xsl as you use in a data view web part, not using the DVWP itself…

    $().SPServices({
    operation: “GetListItems”,
    listName: “MyListName”,
    [webURL: “mySiteURL”,
    completefunc: function (xData, Status) {
    xml=xData;
    xsl=loadXMLDoc(”myXSLfile.xsl”);
    // code for IE
    if (window.ActiveXObject)
    {
    ex=xml.transformNode(xsl);
    document.getElementById(”example”).innerHTML=ex;
    }
    }
    });

  4. @Fredrik: Interesting idea. I have never thought of that approach, but it seems like it ought to work. I’ll be interested to hear what you come up with. Might make a good article. ;+)

  5. Fredrik says:

    It worked :-)

    The test code i used below. I have no idea if it’s true, but it feels like performance is much better. The test.xsl is just the code i copied from a data view wp, so you could reuse that….

    There is a jQuery library for this too, didn’t make it work http://www.jongma.org/webtools/jquery/xslt/

    DispRes

    function loadXMLDoc(dname)
    {
    if (window.XMLHttpRequest)
      {
      xhttp=new XMLHttpRequest();
      }
    else
      {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",dname,false);
    xhttp.send("");
    return xhttp.responseXML;
    }
    
    function displayResult(listName)
    {
    	$().SPServices({
    		operation: "GetListItems",
    		listName: listName,
    		webURL: "http://mysite.global.logica.com/personal/ekstromf",
    		completefunc: function (xData, Status) {
    			//xml=loadXMLDoc("test.xml");
    			xml=xData.responseXML;
    			xsl=loadXMLDoc("test.xsl");
    			// code for IE
    			if (window.ActiveXObject)
    			  {
    			  ex=xml.transformNode(xsl);
    			  document.getElementById("example").innerHTML=ex;
    			  }
    			// code for Mozilla, Firefox, Opera, etc.
    			else if (document.implementation &amp;&amp; document.implementation.createDocument)
    			  {
    			  xsltProcessor=new XSLTProcessor();
    			  xsltProcessor.importStylesheet(xsl);
    			  resultDocument = xsltProcessor.transformToFragment(xml,document);
    			  document.getElementById("example").appendChild(resultDocument);
    			  }
    
    		}
    
    	});
    
    }
    displayResult('test');
    
  6. @Fredrik: Nice! I think you could clean up the code even more by taking advantage of jQuery for the loadXMLDoc piece as well as jQuery.support to manage the browser differences. You might not even need that if you could “jQuerify” the XSL handling. Of course, if it works, then you don’t need to mess with it.

    I’m going to look at how I can use this method in my library, for sure.

  7. spevilgenius says:

    Hey guys, I was actually considering this while I was working on getting my popup forms to work. It occurred to me that I could create a 2 dataform webparts for a list using Designer. It should be fairly easy to combine this with the jQuery dialog to create a popup form. I have used part of this with a fairly basic form to update the userinfo list, but I am certain that it could work for other lists.

  8. Lorenzo says:

    Hi Marc,

    I use the “count” function to count the elements in a grouped DVWP. My problem is that the DVWP is grouped by two field and the count($nodeset) function count only the element of the first field.How can I do ?
    Thanks

  9. Lorenzo says:

    It seems doesn’t work.
    The $Rows param is not recognized and I’ve used the /dsQueryResponse/Rows/Row[....] instead but the result of the count function isn’t correct.
    I note that one of the two fields comes from another list and the other is a calculated field.
    How can I do?
    Thanks for your quickly help!

Trackbacks

Check out what others are saying about this post...
  1. [...] past the basics, and <xsl:attribute> is one of them. I mentioned <xsl:attribute> in the last article, but I wanted to cover it more [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!