XSLT problem comparing prior sibling, not returning correct value, returns original

Discussion in 'XML' started by Phoenix, Dec 30, 2004.

  1. Phoenix

    Phoenix Guest

    I am trying to print date headings over comments (as headings)

    I have a simple XML file :
    <comments>
    <comment id=1234 yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id=1309 yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id=1333 yyyymmdd="20041230" flag="N">..text..</comment>
    <comment id=1389 yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id=1409 yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id=1450 yyyymmdd="20041229" flag="Y">..text..</comment>
    <comment id=1464 yyyymmdd="20041229" flag="N">..text..</comment>
    <comment id=1470 yyyymmdd="20041229" flag="Y">..text..</comment>
    </comments>

    Then I run thru a for-each loop (i have lots of formatting to do)
    I want to print the date if the current node's date is different from
    the prior node's date....It does it right the first time, but then
    continues printing the dates thru the end of the file. When I print out
    the preciding sibling (for testing purposes), it seems to be stuck on
    the first node. Any suggestions??


    ......
    <xsl:variable name="commentDate">
    <xsl:value-of select="substring(@yyyymmdd,6,2)"/>-<xsl:value-of
    select="substring(@yyyymmdd,9,2)"/>-<xsl:value-of
    select="substring(@yyyymmdd,1,4)"/>
    </xsl:variable>
    ........
    <xsl:for-each select="comments/comment[@flag='Y']">

    <xsl:if test="position() !='1' and @yyyymmdd !=
    preceding-sibling::comment/@yyyymmdd">
    <tr><td <!---->
    <xsl:value-of select="$commentDate"/><br/>
    <xsl:value-of select="@yyyymmdd"/>:::
    <xsl:value-of select="preceding-sibling::comment/@yyyymmdd"/>
    </td></tr>
    </xsl:if>

    </xsl:for-each>
    ......

    thanks,

    Jenn
     
    Phoenix, Dec 30, 2004
    #1
    1. Advertising

  2. Phoenix

    Joris Gillis Guest

    > Then I run thru a for-each loop (i have lots of formatting to do)
    > I want to print the date if the current node's date is different from
    > the prior node's date....It does it right the first time, but then
    > continues printing the dates thru the end of the file. When I print out
    > the preciding sibling (for testing purposes), it seems to be stuck on
    > the first node. Any suggestions??


    Hi,

    The problem here is caused by the 'preceding-sibling': it does not simple return the previous node, but instead a node-set containing all previous sister-nodes.
    Add '[position()=1]' or its abbreviated equivalent '[1]' to the Xpath expression:

    <xsl:if test="position()='1' or @yyyymmdd != preceding-sibling::comment[1]/@yyyymmdd">
    <tr><td>
    <xsl:value-of select="@yyyymmdd"/>
    </td></tr>
    </xsl:if>

    That prints out:
    <tr>
    <td>20041230</td>
    </tr>
    <tr>
    <td>20041229</td>
    </tr>



    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Scio me nihil scire" - Socrates
     
    Joris Gillis, Dec 30, 2004
    #2
    1. Advertising

  3. Phoenix

    Jenn Lee Guest

    Still not working....I wonder if it is because it is filtering @flag='Y'
    ?

    I tried that [1] and now when I print out the values for testing, it
    looks like it is referring to itself...

    <xsl:value-of select="@yyyymmdd"/>::
    <xsl:value-of select="preceding-sibling::story[1]/@yyyymmdd"/>


    prints this:
    20041230:: 20041230
    20041229:: 20041229
    20041229:: 20041229
    20041229:: 20041229

    so @yyyymmdd != preceding-sibling::comment[1]/@yyyymmdd
    is never true...

    ? any clues?

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Jenn Lee, Dec 30, 2004
    #3
  4. Phoenix

    Joris Gillis Guest

    > Still not working....I wonder if it is because it is filtering @flag='Y'
    > ?


    I don't understand why it's not working. The flag checking might be tricky, but cannot explain the odd behaviour you describe:
    > so @yyyymmdd != preceding-sibling::comment[1]/@yyyymmdd
    > is never true...



    I can only tell that this xml:
    <comments>
    <comment id="1234" yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id="1309" yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id="1333" yyyymmdd="20041230" flag="N">..text..</comment>
    <comment id="1389" yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id="1409" yyyymmdd="20041230" flag="Y">..text..</comment>
    <comment id="1450" yyyymmdd="20041229" flag="Y">..text..</comment>
    <comment id="1464" yyyymmdd="20041229" flag="N">..text..</comment>
    <comment id="1470" yyyymmdd="20041229" flag="Y">..text..</comment>
    </comments>

    with this xsl:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="comment[@flag='Y']">
    <xsl:if test="position()='1' or @yyyymmdd != preceding-sibling::comment[1]/@yyyymmdd">
    <tr><td>
    <xsl:value-of select="@yyyymmdd"/>
    </td></tr>
    </xsl:if>
    </xsl:template>

    <xsl:template match="comment"/>

    </xsl:stylesheet>

    results in this output:
    <tr>
    <td>20041230</td>
    </tr>
    <tr>
    <td>20041229</td>
    </tr>

    AFAIK, that is the ouput you want.


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Scio me nihil scire" - Socrates
     
    Joris Gillis, Dec 30, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    3,756
  2. Replies:
    11
    Views:
    673
    Fredrik Lundh
    Jan 23, 2006
  3. Replies:
    8
    Views:
    336
    Roy Gourgi
    Jun 28, 2005
  4. Red
    Replies:
    2
    Views:
    1,241
    Pavel Lepin
    May 9, 2007
  5. Kim

    Textbox Text in Datagrid Returning to Original Value

    Kim, Jul 7, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    138
Loading...

Share This Page