|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
XSLT - Sorting, Limiting Nodes
Hi,
I have a long XML document that I'm using to write and sort news for my personal website that I transform with XSLT and pre-process with PHP/Sablotron to produce valid XHTML. But I don't want to display *every* node in the tree - only the first five full entries, and then just the headlines for the rest. Is there a way to use XSL to transform this page thusly? Then of course I'd need to have visitors click on the headlines to traverse to just *that* node (also pre-processed with PHP/Sablot) on a results page. My XML document is here: http://64.191.4.184/home.xml and the XSL transformation that I'm using for it is here: http://64.191.4.184/home.xsl And if you simply go to: http://64.191.4.184/index.php you can see them processed with Sablot, CSS, etc. -- Jough |
|
#2
|
|||
|
|||
|
XPATH will help you accomplish this..
here's an introduction: http://www.devshed.com/Server_Side/XML/XPath/page1.html you're links are not loading for me right now, so this is all i can say at the moment, but this should get you started |
|
#3
|
|||
|
|||
|
Thanks for your reply. I had read that article, but it doesn't really go in depth enough for my needs (like many articles on that site - great introductions if you've never heard of a technology, but they generally don't give you enough info to be useful for a real-world application).
Anyway, traversing the nodes isn't really a problem - I just can't seem to find a way to limit node retreival in XSLT (using XPath or otherwise) as I would in say, a SQL db call: SELECT field FROM table WHERE var='value' LIMIT 0,5; Is there way to limit node traversal in XSL/XPath/XLink/XPointer/XWhatever? -- Jough |
|
#4
|
|||
|
|||
|
i'm not an expert, but
how about something like this: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="//adjust-your-position"> <xsl:if test="position() < 6"> <xsl:value-of select="@value"/> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet> |
|
#5
|
|||
|
|||
|
Well, I was doing something similar, Jason:
Code:
<xsl:choose>
<xsl:when test="position() <=5">
<xsl:apply-templates select="headline" />
<xsl:apply-templates select="date" />
<xsl:apply-templates select="body" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="headline" />
</xsl:otherwise>
</xsl:choose>
But there's a bug in Sablotron that parses child nodes of the root element incorrectly - it parses whitespace text as a child node, regardless of whether there's an <xml:strip-space elements="*" /> transormation or not. So the above works with the MS XML 4 parser but not with Sablotron. I've e-mailed them to see if they have a fix or workaround. I'll post it here when I get a reply. -- Jough P.S. And yes, I suppose I could just double my position() test number to account for the whitespace text nodes, but that would be wrong, especially if I want to code for standards compliance across multiple platforms - it's much better to get the Ginger Alliance people (those wacky Czechs who make Sablotron) to get their parser up to spec. |
|
#6
|
|||
|
|||
|
ahhhhh,
good to know thanks for the heads-up. let me know .. sorry, i couldn't help you out!! ![]() ================ justin_dago |
|
#7
|
|||
|
|||
|
That's okay. Actually, I wound up doing it a slightly different way, which selects the child nodes of the root more exactly:
Code:
<xsl:template match="entry[position() <=5]">
Do Something
</xsl:template>
<xsl:template match="entry[position() >=6]">
Do Something Else
</xsl:template>
That way, I specifically only select the "entry" elements whose positions are tested in the brackets. Sablotron wrongly implements the parsing of white space (they say that's how it *should* be done, but the W3C spec, as well as every other parser on the market that I've tested, seems to disagree with them) but either way, you can always test using element[child/test/condition]. Good to know. Cheers, -- Jough |
|
#8
|
|||
|
|||
|
Cool, Thanks jough!!
Have fun! justin.dago |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSLT - Sorting, Limiting Nodes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|