|
Subject:
|
if statement and contains function
|
|
Posted By:
|
richjo100
|
Post Date:
|
9/24/2004 4:50:16 AM
|
hello all, If you could help with the following I would be most grateful
Imagine the following xml:
<start> <build> <message> blah5 </message> <message> blah1 </message> <message> blah2 </message> <message> blah3 </message> <message> blah4 </message> <message> blah5 </message> <message> blah1 </message> <message> blah4 </message> </build> </start>
I have written some XSLT (with your help!) to extract the first blah1 in the message tag. I have written some more code to extract the next 4 elements (blah2, blah3, blah4, blah5) However I have hardcoded this to look 4 times. I have used following-sibling::*[1],following-sibling::*[2], following-sibling::*[3],following-sibling::*[4]
The reason I look 4 times is that I want to stop when I reach another Blah1. However Blah1 might appear after 6 times in which case I will lose 2 blah* because I am only looking 4 ahead. I have been messing around with If and Conatins to try and get it to keep looking until it reach Blah1 but am stuck
Any help wuold be appreciated Richard
|
|
Reply By:
|
barcher
|
Reply Date:
|
9/24/2004 5:05:43 AM
|
Hi Richard,
It would be easier to help if you explained the expected output (or result) of your tranformation.
Regards Bryan
|
|
Reply By:
|
richjo100
|
Reply Date:
|
9/24/2004 5:08:21 AM
|
HI barcher Thanks for the reply The output I want is going to be in html table Each row will consist of blah1 blah2 blah3 blah4 That is all the output I want. Is this what you meant? Cheers Richard
|
|
Reply By:
|
richjo100
|
Reply Date:
|
9/24/2004 5:25:18 AM
|
Hi, I have solved one of the problems. I can analyse the text to make sure it is what I want. The code is:
<xsl:variable name="empname1" select="following-sibling::*[1]" /> <xsl:if test = "not(contains($empname1, 'blah1'))"> <!-- analysis stop. --> </xsl:if>
however the only thing with this is that I have to check following-sibling::*[1],following-sibling::*[2], following-sibling::*[3],following-sibling::*[4], following-sibling::*[5],following-sibling::*[6]
which will look up to 6 and stop after 4 if blah1 is seen.
However this seems very inefficient. This is where my xslt knowledge gets a bit dodgy. I want to loop from 1-6 siblings to save code space. In addition the number I might want to check might rise from 6 to about 20. Is this possible?
Cheers Richard
|
|
Reply By:
|
barcher
|
Reply Date:
|
9/24/2004 5:39:22 AM
|
If there are only ever 2 "blah1"'s the following might help:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <xsl:apply-templates select="start/build/message" /> </xsl:template>
<xsl:template match="message[contains(., 'blah1')]"> <xsl:for-each select="following-sibling::message[following-sibling::message[contains(., 'blah1')]]"> <xsl:value-of select="." /><br /> </xsl:for-each> </xsl:template>
<xsl:template match="message" />
</xsl:stylesheet>
|
|
Reply By:
|
richjo100
|
Reply Date:
|
9/24/2004 6:16:33 AM
|
Hi barcher,
Thanks for your help!
your xslt works for my example great! however when i move it up to my main app the contains doesnt seems to work. It seems to carry on even if it reaches "blah1" and stops at the next "blah1". I will look up some doc on contains to try and figure it out.
Out of interest could you do a loop by number. For example I want the next 10 siblings whatever there value?
Thanks for your help again Cheers Richard
|
|
Reply By:
|
richjo100
|
Reply Date:
|
9/27/2004 5:04:02 AM
|
Hi I found some coutner stuff here. I thought it might be of interest to others
http://rdcpro.com/Members/rdcpro/snippets/recursionandcounting/
|