Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom list with two columns. I want to use xsl to take the value of the fields in the first column, build a hyperlink and put it in the second column.

Please don't suggest calculated columns or lookup columns; I want to use xsl. This is what I have come up with; it doesn't give an error-page, but it doesn't do anything, either! Could somebody please help me?

<xsl:variable name="varFirstColumn">
    <xsl:value-of select="@Column1" />
</xsl:variable>         

<xsl:template match="FieldRef[@Name='Column2']" mode="Text_body">
    <xsl:param name="thisNode" select="."/>
        <a target="_blank"> 
            <xsl:attribute name="class">myCSSclass</xsl:attribute>    
            <xsl:attribute name="href">mydomain.com/mysite/<xsl:value-of select="$varFirstColumn"/>.html</xsl:attribute>
        MyTextHere</a>
</xsl:template> 

I tried this, too:

<xsl:template match="FieldRef[@Name='Column2']" mode="Text_body">
    <xsl:param name="thisNode" select="."/>
    <a href="mydomain.com/mysite/mypage\{Column1}" target="_blank">Dir</a>
</xsl:template> 

[edit] I get an error saying The variable or parameter 'Column1' is either not defined or it is out of scope.. Now, Column1 is a Number column.

[update] The xsl does work if I don't try to look up the value of what is in Column1.

[a bit more info] I am using a custom xsl file in the _layouts directory, and using the match to make changes to any column with that fieldref name. Both columns are in the same view and in the same list.

share|improve this question
    
What context? Are you doing this in a dataview web part or what? –  PirateEric Oct 30 '12 at 19:18
    
It is a custom list - no web parts, and I am editing a custom xsl file in _layouts. –  BGM Oct 30 '12 at 19:49
    
Does the list view contain both columns? Which file, _layouts/xsl/fldtypes_*.xsl ? How do you connect it to the field? Please add all this information to your question. –  Andrey Markeev Oct 30 '12 at 19:59
    
I have a custom xsl file in the /_layouts directory. In xsl, you don't have to connect it to any list directly - you can just "match" the column's field name and the changes get made. Yes, both the list and the view contain both columns. –  BGM Oct 30 '12 at 20:03
1  
Thanks for updating the question, now it is much better :) And yes, I've added the answer and also included some explanations and links, so hopefully next time you will have your template working from the first try! –  Andrey Markeev Oct 30 '12 at 21:54
show 3 more comments

3 Answers

up vote 3 down vote accepted

Your template matches a FieldRef element, and inside the template you're trying to get the field value as an attribute of current element (i.e. FieldRef). But the FieldRef element basically doesn't have any columns. It itself represents one column and contains some of it's properties. Ordinary FieldRef element looks as follows:

<FieldRef Name="Author" ImnHeader="TRUE" Type="User" FieldType="User"
          DisplayName="Created By" ID="1df5e554-ec7e-46a6-901d-d85a3881cb18"
          LinkToItemAllowed="Prohibited" />

To get the field value for current row, you should use $thisNode parameter, which always contains Row element, i.e. contents of $thisNode could look something like this:

  <Row ID="1"
       PermMask="0x7fffffffffffffff"
       Attachments="0" 
       Title="Theories of Truth"
       FileLeafRef="1_.000"
       FileLeafRef.Name="1_"
       FileLeafRef.Suffix="000"
       FSObjType="0"
       ...
  />

Solution

Having all this in mind, now it is easy to recognize the mistake. In your second code fragment, please replace {Column1} to be {$thisNode/@Column1}, and that's that.

Final code:

<xsl:template match="FieldRef[@Name='Column2']" mode="Text_body">
    <xsl:param name="thisNode" select="."/>
    <a href="mydomain.com/mysite/mypage/{$thisNode/@Column1}" target="_blank">Dir</a>
</xsl:template> 

More info

It is always a good idea to have a notion of what you're doing. SharePoint list view XSLT is big and complicated thing, thus you will save much time it you explore it a bit before trying to squeeze something from it.

Good SharePoint XSLT rendering system overview can be found on MSDN:

Also it is sometimes very useful to examine examples of input and output of the transformations:

share|improve this answer
    
Excellent! +1 Solved. (and thank you) –  BGM Oct 30 '12 at 21:53
    
You're welcome :) –  Andrey Markeev Oct 30 '12 at 22:20
add comment

Try mode="Text_body" without the capital B in Body.

and <a href="mydomain.com/mysite/mypage/{$varFirstColumn}" target="_blank">Dir</a>

share|improve this answer
    
Thanks - I made the correction, but it didn't solve it. –  BGM Oct 30 '12 at 19:55
add comment

Why do you need a template?

<a href="http://www.yourdomain.com/{@Column1}.html" class="yourClass">Dir</a>

Should work.

KS

share|improve this answer
    
Well, I don't know, actually! I don't really know anything about xsl - I'm only at the level of hacking it at the moment. If I didn't use a template, where in my xsl file would I put your line of code? –  BGM Oct 30 '12 at 21:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.