This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I hope this is an easy one. What's the easiest way to display the first 5 nodes in from a result tree when using XSLT? I'm working with an XML file that has 20 nodes, but I only need to display the first 5. When I use a for-each or specify a template it always processes every node in the XML document. Thanks! Drew
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
posted
0
XPath expression nodeName[position() <= 5] should work. Er... position() <= 5 actually [ May 23, 2002: Message edited by: Mapraputa Is ]
Could you possibly explain how to use 'position() <= 5' with an 'xsl:if'? So that if the position is less than or equal to 5 it will get added to the result tree. I've been playing around with this for a while, but still haven't got it to work. Drew
Ingo Schildmann
Greenhorn
Joined: Sep 20, 2000
Posts: 7
posted
0
Copying the first n nodes of a given XML document can be rather complex, so more information like the structure of your document would be helpful. Maybe you want just the first 5 elements of a flat list instead of every node (including whitespace, comments etc.) of a complex tree.
Drew Lane
Ranch Hand
Joined: May 13, 2001
Posts: 296
posted
0
Here's a snippet of the format of the XML: <item> <title>Title Info</title> <link>link Info</link> <description>description</description> </item> <item> <title>Title Info</title> <link>link Info</link> <description>description</description> </item> <item> <title>Title Info</title> <link>link Info</link> <description>description</description> </item> This just repeats about 20 times. I just need the first 5 nodes with all the data (ie title, link, description) Drew
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
posted
0
Do you want to use xsl:if to select first five nodes, or for something else? If the first, you do not need xsl:if, you can use a template instead. Something like <xsl:template match="items/item[position() <= 5]"> <xsl:copy-of select="."/> <!-- whatever other processing you need to apply --> </xsl:template> [ May 23, 2002: Message edited by: Mapraputa Is ]
Drew Lane
Ranch Hand
Joined: May 13, 2001
Posts: 296
posted
0
This line is causing me problems: [position() <= 5] 500 Servlet Exception rss-5.xsl:49: com.caucho.xpath.XPathParseException: expected `]' at `&' in rss/channel/item[position() <= 5] in pattern `rss/channel/item[position() <= 5]'
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
posted
0
Do you code it as <= or as <= ? The latter variant is incorrect, because "<" is allowed in XML document only as tag opener.
Drew Lane
Ranch Hand
Joined: May 13, 2001
Posts: 296
posted
0
Yes, I used < The only way I've been able to get it to work the way I want it to is to call the template 5 times from another template. Seems like there should be an easier way. The way you suggested makes sense, it just doesn't seem to like the syntax. All I really need is a simple for loop - sheesh! Drew
Drew Lane
Ranch Hand
Joined: May 13, 2001
Posts: 296
posted
0
The BBS is changing my code. I used & l t ; (no spaces) Drew
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
posted
0
I tested <xsl:template match="items/item[position() <= 5]"> with Xalan and it works So <xsl:for-each select="items/item[position() <= 5]"> <xsl:value-of select="./title"/> </xsl:for-each> wont work for you either? [ May 23, 2002: Message edited by: Mapraputa Is ]
Drew Lane
Ranch Hand
Joined: May 13, 2001
Posts: 296
posted
0
OK, it's working!! I had a space in the wrong place! <xsl:for-each select="item[position()< =5]"> ^ BTW, the resin xsl parser doesn't seem to like the & l t ; (how do you display this?) It worked find with the < (left arrow) I'll go back and test it again with the position function in the template "match". I suspect it will work now. THANK YOU!! Drew
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
posted
0
how do you display this? &lt; (Of course I had to type &amp;lt; to show this... ) [ May 24, 2002: Message edited by: Mapraputa Is ]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.