Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT problem comparing prior sibling, not returning correct value, returns original

Reply
Thread Tools

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

 
 
Phoenix
Guest
Posts: n/a
 
      12-30-2004
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

 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      12-30-2004
> 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-v...i?userid=38041)
"Scio me nihil scire" - Socrates
 
Reply With Quote
 
 
 
 
Jenn Lee
Guest
Posts: n/a
 
      12-30-2004
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!
 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      12-30-2004
> 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" xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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-v...i?userid=38041)
"Scio me nihil scire" - Socrates
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
cuserid returns original login after a su to another user David Mathog C Programming 24 10-19-2006 04:50 AM
Returning a value and retain the original values velthuijsen@hotmail.com C++ 8 06-28-2005 08:32 AM
RedirectFromLoginPage never returns to original page Edward Mitchell ASP .Net 8 02-22-2005 04:44 AM
correct or not correct? Dan HTML 7 10-02-2003 10:16 PM



Advertisments