473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
3 2704
> 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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
4
by: Richard Rudie | last post by:
Have any other Windows iTunes users looked at the XML file iTunes uses as its database? (Does iTunes for Mac use an XML file, too?) When I noticed that it was XML, I thought it might be useful, or...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
1
by: Patrick.O.Ige | last post by:
I have a xml file and i want to format it using XSL My XSL file and XML below I needed to do a distinct which is ok on the first node "Code" For the "programDescription" i did below which gets the...
8
by: Hercules Dev. | last post by:
Hi all, I'm new in xslt and xpath, so my question might be simple but i'm learning. I have an XML document and need to transform it into another XML, I use xslt and it works, but there is a...
2
by: Pandikkannan | last post by:
I am new to XSLT. The requirement i have is to convert an XML to another XML format. My source XML looks something like: <QryLMLCheck>...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
11
by: =?ISO-8859-1?Q?Jean=2DFran=E7ois_Michaud?= | last post by:
Context: I'm trying to compare XML tree fragments and I'm doing so by outputting the attributes of each element in the tree and outputting it to a string then normalizing the strings. Then I'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.