Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display first 5 nodes of result tree?

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XPath expression nodeName[position() <= 5] should work. Er... position() &lt;= 5 actually
[ May 23, 2002: Message edited by: Mapraputa Is ]
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() &lt;= 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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you code it as &lt;= or as <= ? The latter variant is incorrect, because "<" is allowed in XML document only as tag opener.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BBS is changing my code.
I used & l t ;
(no spaces)
Drew
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested <xsl:template match="items/item[position() &lt;= 5]"> with Xalan and it works
So
<xsl:for-each select="items/item[position() &lt;= 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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you display this?
&amp;lt; (Of course I had to type &amp;amp;lt; to show this... )
[ May 24, 2002: Message edited by: Mapraputa Is ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic