 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 24th, 2004, 04:50 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
if statement and contains function
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
|
|

September 24th, 2004, 05:05 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Richard,
It would be easier to help if you explained the expected output (or result) of your tranformation.
Regards
Bryan
|
|

September 24th, 2004, 05:08 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 24th, 2004, 05:25 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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'))">
</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
|
|

September 24th, 2004, 05:39 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

September 24th, 2004, 06:16 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |