XSLT and SharePoint – Part 2

So where did we get to? Ah yes, we have created our first XSLT Stylesheet which presents the data from our list in a simple tabular format.

In reality, these are links so we would probably not present them in a tabular format so we will make some changes to the XSLT.

In the interests of brevity I am only going to show the elements of the Stylesheet – the rest will stay the same as in Part 1.

Firstly, we will get rid of the HTML table.

<xsl:template match="/">
 <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
 <xsl:for-each select="$Rows">
  <xsl:call-template name="dvt_1.rowview" />
 </xsl:for-each>
</xsl:template>
<xsl:template name="dvt_1.rowview">
 <xsl:value-of select="@Title" />
 <br />
 <xsl:value-of select="@URL" />
 <br />
</xsl:template>
Items

Itemised list

So we now have even simpler looking XSLT and a list of items from our list that is looking presentable. However, we still do not have any hyperlinks so we will add those in now using standard HTML <a> tags.

<xsl:template match="/" >
 <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
 <xsl:for-each select="$Rows">
  <xsl:call-template name="dvt_1.rowview" />
 </xsl:for-each>
</xsl:template>
<xsl:template name="dvt_1.rowview">
 <a href="{@URL}"><xsl:value-of select="@Title" /></a>
</xsl:template>
Links

Hyperlinks

Now down to 9 lines of XSLT and we are presenting the content from our list as hyperlinks.

The main thing to note here is the shortcut way of including the URL.
{@URL} embeds the value of that column.

An alternative approach that is longer but gives more control is to use:

<xsl:attribute name="href">
 <xsl:value-of select="@URL" />
</xsl:attribute>

I have one more part to this little mini-series which takes this one stage further.

This entry was posted in SharePoint and tagged , , , , . Bookmark the permalink.

28 Responses to XSLT and SharePoint – Part 2

  1. Pingback: Sharepoint XSL / XSLT | Just tinkering Blog

  2. Robert Dolliver says:

    I get a an Unable to display webpart error when I replace the code from above. I have the xsl from part 1 working. But step one of part 2 I get the webpart error. any ideas?

    • paylord says:

      Hello Robert,

      If you have copied and pasted in the XSLT from the page then my guess would be the quotation marks. They do not always paste in nicely so I would suggest you go through and replace them and see if that gets you back on track? If this does not solve the problem then you probably have a minor typo which has made the HTML fail in some way – double check your tags are opened and closed correctly.

      Let me know how you go.

      Dave

  3. Les Zatony says:

    I had the same issue as Robert. The problem was that you have a backward quote in the line
    <xsl:variable name="Rows“
    Once I fixed that it worked fine.

    • paylord says:

      Hello Les,

      You are correct that there are some issues with quotation marks as these do not seem to copy across very well. I am considering moving all my snippets into GitHub which should make things easier in future but well spotted on finding the problem.

      Dave

  4. paylord says:

    Updated to display example scripts better and corrected minor typo

  5. Solo says:

    Czy da się do tego użyć kolumny typu text?

  6. Solo says:

    Is it possible to use a text column?

    • paylord says:

      Hey Solo,

      This is using a text field. It is the XSL that presents it as a link.

      Dave

      • Solo says:

        Hi.
        If I use the text box is added to the address of the website address. Instead wp.pl is mywebsite/page/wp.pl

      • paylord says:

        Hey Solo

        I am not quite sure I understand what you are asking. However, if you have an incomplete URL SharePoint will automatically pad it out. So, if you add an address such as wppl then SharePoint will assume it is a page and add mywebsite/pages/ to prefix it. If the address you use is /mypl then SharePoint will treat it as a relative path and add the domain mywebsite.

        Hope this helps.

        Dave

      • Solo says:

        If I use in the column Title I for example. link is OK.
        If I use another column that does not work. The TD is empty.

      • Solo says:

        If I use in the column Title I for example. link is OK.
        If I use another column that does not work. The TD is empty.

      • paylord says:

        OK – so the concept is exactly the same. You must make sure that you use the internal name of the column in XSLT though. So if you have renamed the column then you must use the name from when you created it. Also, if you have spaces in the name then you will have to replace the spaces – so ‘This Column’ will be ‘This_x0020_Column’.

        If you can give me an example of what is not working it would help me to understand the problem.

        Dave

  7. Solo says:

    Thank you for your commitment.
    I added a list of address http://wp.pl returns: http://mysite.pl/info/uslugi/Strony/%3Ca%20href=%22http://wp.pl%22%3Ehttp://wp. en% 3C / a% 3E
    It may be a template page or site?

  8. Solo says:

    I did another test. I added the HTML field in which I put the contents of the HTML. Template returned clean HTML code.

    • paylord says:

      Hey Solo

      If you want to display HTML content directly from a list field then you can use

      <xsl:value-of disable-output-escaping="yes" select="@FieldName" />

      Hope this helps.

      Dave

  9. Perhaps it would be better to show a screenshot:


    • paylord says:

      OK – now we are making some progress.

      First, I have made a mistake. The value for disable-output-escaping should be “yes” and not “True”.

      Second, can you check that the columns you are trying to show are included in the view?

  10. In fact they were not.
    And I’ve added this:

    But I still do not have an active link in the column of type text.
    Only in a column of type HTML

    • paylord says:

      You should only be using columns of type Text. It is possible to use Hyperlink columns but that is more complex as you have to extract the URL component or just display it as is using disable-output-escaping.

      1. Please check that the column abc is included in your view and that the internal name of it is abc – note that XSLT is case sensitive
      2. Try displaying the value of abc on its own without being part of any link
      3. The url part that you are trying to insert into the href part of the a tag must be a valid URL – so where you are using Title, the only valid one seems to be row 3
      4. I would suggest you try afresh with a new list and simply have the standard Title column with the description of your link and then a second text column containing a valid URL. If you follow the steps in the Part 1 and then Part 2 it should work fine.

      Good luck and let me know how you get on.

      Dave

  11. I was wrong again. abc is called pykw🙂
    Now it is so:

  12. Unfortunately, clicking on Onet added to the name of the site link.

    A list I have 1000 pieces of 2-3 with links.

    • paylord says:

      Hey Solo,

      OK so I think we are finally making progress. It looks like later versions of SharePoint are automatically reformatting a URL in a text field to an A tag. This means that it all gets very confused when we then try to create an A tag using XSLT.

      There does not seem to be any nice way to persuade SharePoint not to do this auto formatting so you have a couple of options:

      1. Just use the link as is with disable-output-escaping=’yes’
      2. Remove the A tag from around the link so you can use XSLT to format as you want to – a quick way to do this is something like:
      <xsl:variable name="myurl" select="substring-before(substring-after(@sms,’>’),'<‘)" />

      Another alternative could be to use a multi-line text column forced to text only but I have not tested this.

      Hope this helps.

      Dave

  13. They should call in a new window.
    Only columna html works fine, but it can not import this amount of data.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s