aspose file tools*
The moose likes XML and Related Technologies and the fly likes Display first 5 nodes of result tree? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Display first 5 nodes of result tree?" Watch "Display first 5 nodes of result tree?" New topic
Author

Display first 5 nodes of result tree?

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
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
XPath expression nodeName[position() <= 5] should work. Er... position() &lt;= 5 actually
[ May 23, 2002: Message edited by: Mapraputa Is ]

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
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
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
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
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

Joined: May 13, 2001
Posts: 296
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
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

Joined: May 13, 2001
Posts: 296
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
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
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

Joined: May 13, 2001
Posts: 296
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
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display first 5 nodes of result tree?
 
Similar Threads
find text in xml using xpath
XSL question
tree refresh problem in TreeTable
JTree in Browser
I could not expand myfaces tree