#1
  1. No Profile Picture
    El Guapo
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    You have moved into a dark place. It is pitch black. You are likely to be eaten by a grue.
    Posts
    56
    Rep Power
    13

    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
    See my PHP in action:
    Poetry Archives
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    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
  4. #3
  5. No Profile Picture
    El Guapo
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    You have moved into a dark place. It is pitch black. You are likely to be eaten by a grue.
    Posts
    56
    Rep Power
    13
    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
    See my PHP in action:
    Poetry Archives
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    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() &lt; 6">
    <xsl:value-of select="@value"/>
    </xsl:if>

    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

  8. #5
  9. No Profile Picture
    El Guapo
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    You have moved into a dark place. It is pitch black. You are likely to be eaten by a grue.
    Posts
    56
    Rep Power
    13
    Well, I was doing something similar, Jason:

    Code:
    <xsl:choose>
            <xsl:when test="position() &lt;=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.
    See my PHP in action:
    Poetry Archives
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    ahhhhh,

    good to know thanks for the heads-up.

    let me know ..

    sorry, i couldn't help you out!!


    ================
    justin_dago
  12. #7
  13. No Profile Picture
    El Guapo
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    You have moved into a dark place. It is pitch black. You are likely to be eaten by a grue.
    Posts
    56
    Rep Power
    13
    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() &lt;=5]">
            Do Something
      </xsl:template>
      
      <xsl:template match="entry[position() &gt;=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
    See my PHP in action:
    Poetry Archives
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    Cool, Thanks jough!!

    Have fun!




    justin.dago

IMN logo majestic logo threadwatch logo seochat tools logo