By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,948 Members | 1,573 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,948 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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
Jul 20 '05 #2

P: n/a
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!
Jul 20 '05 #3

P: n/a
> 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:output 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
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.