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

P

Phoenix

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
 
J

Joris Gillis

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,
 
J

Jenn Lee

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?
 
J

Joris Gillis

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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top