<!-- Create a node set from your richtext content --> <xsl:variable name="bodyText"> <xsl:text><set></xsl:text> <xsl:value-of select="$currentPage/data [@alias='bodyText']" disable-output-escaping="yes"/> <xsl:text></set></xsl:text> </xsl:variable>
<!-- The above is currently seen as a result tree fragment (RTF) and hence cannot be --> <!-- accessed via xpath, so you need to convert it to an XML node --> <xsl:variable name="myBodyTextNode" select="vizioz:GetNode($bodyText)"/>
<!-- And finally to select the first paragraph --> <xsl:value-of select="$myBodyTextNode/p[1]"/>
</xsl:template>
<msxsl:script language="C#" implements-prefix="vizioz"> <![CDATA[ public XmlElement GetNode(string xmlString) { var xDoc = new XmlDocument(); xDoc.LoadXml(xmlString); return xDoc.DocumentElement; } ]]> </msxsl:script>
</xsl:stylesheet>
And the result is:
This is some body text
So, what does the above do you might be asking!
What it does is it takes your richtext bodyText and convert it to an XML node, then returns the first paragraph child node.
It's a real pain to work out and although there is a "msxsl:node-set" which is meant to convert a string to a node set it does not seem to work, the only way seems to be to pass it into a script function as I have done above.
How to display only the first paragraph in the bodyText
I'm trying to list a group of articles and show only the first paragraph in the bodyText? Does anyone know how to do this in XSLT.
I've tried using umbraco.library:Split to split it out by "<p>" or"<p>" tag but this just seems to break things:
Any help would be much appreciated... This seems straight forward but i'm getting lost (probably missing something)
Hi Hamish,
I saw this post and thought, "oh, I remember how to do that... " well 4 hours later I have the solution for you!
The test data I used was:
And the XSLT macro:
And the result is:
So, what does the above do you might be asking!
What it does is it takes your richtext bodyText and convert it to an XML node, then returns the first paragraph child node.
It's a real pain to work out and although there is a "msxsl:node-set" which is meant to convert a string to a node
set it does not seem to work, the only way seems to be to pass it into a script function as I have done above.
I hope this is of help :)
Chris
A much more elegant solution, as I believe is :
1. Replace '<p>' with '' and '</p>' with '|' (or any other character you prefer, preferably less like to appear in the bodyText)
2. Split the bodyText into an array, using that particular character as a delimiter
3. Output the first element of the array
Update :
Just tried using the paragraph character '§' as the delimiter instead of the vertical lign '|'...
Works just as well, as is even more unlikely to appear in your bodyText...
Just an idea...
Good luck!
Hi,
I wouldn't use the pipe character as I know a lot of my clients use that, but the § character is very unlikely to be used!
Obviously both solutions work, no idea which is the optimum solution, I guess it depends if there is a risk of using the delimiter character :)
Cheers,
Chris
you could replace \n with space, and then all <\p> with \n
and use \n delimiter.
is working on a reply...