Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old September 23rd, 2004, 10:07 AM
Authorized User
 
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt looping

Hello,
Firstly I am very new to XSLT so sorry if this is really simple.
I have the following xml file

<cruise>
   <properties>
       <membmer>
              blah
       <membmer>
       <membmer>
               blah
       <membmer>
   <properites>
   <build>
         <message>blah1 <message>
         <message>blah2 <message>
         <message>blah5 <message>
         <message>blah2 <message>
         <message>blah3 <message>
         <message>blah1 <message>
         <message>blah1 <message>
    <build>
<cruise>

Now I need to write some xslt which loops thorugh all elements in message and outputs those that have a 1 in them. Therefore the are 7 messages and ideally I want only 3 to be shown.
I have messed around all afternoon trying to get this done. Any help would be appreciated

Thanks
Richard
 
Old September 23rd, 2004, 10:16 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here you go

<xsl:for-each select="message[contains(.,'1')]">
   <xsl:value-of select="." /><br/>
</xsl:for-each>

Regards

Bryan
 
Old September 23rd, 2004, 10:27 AM
Authorized User
 
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for teh reply Bryan, but it doesnt work.
Do I not need to specify where <messages> is?

Thanks
Richard

 
Old September 23rd, 2004, 10:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't think in terms of looping, xslt is not a procedural language.

Write one template rule for messages containing a 1:

<xsl:template match="message[contains(.,'1')]">
  <xsl:value-of select="."/>
</xsl:message>

and another rule (that does nothing) for the rest:

<xsl:template match="message"/>



Michael Kay
http://www.saxonica.com/
 
Old September 23rd, 2004, 10:36 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was assuming you were in the "build" context.

Here is the full stylesheet
By the way, your sample xml is not at all well formed...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  <xsl:template match="/">
        <xsl:apply-templates select="cruise/build/message" />
  </xsl:template>

  <xsl:template match="message[contains(.,'1')]">
    <xsl:value-of select="." /><br/>
  </xsl:template>

  <xsl:template match="message" />

</xsl:stylesheet>

Bryan

 
Old September 23rd, 2004, 10:51 AM
Authorized User
 
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for your help guys. Am very impressed with response time!

Out of interest if the XML was :

<cruise>
   <properties>
       <membmer>
              blah
       </membmer>
       </membmer>
               blah
       </membmer>
   </properites>
   <build>
         <message>blah1 </message>
         <message>blah2 </message>
         <message>blah5 </message>
         <message>blah2 </message>
         <message>blah3 </message>
         <message>blah1 </message>
         <message>blah1 </message>

         <build2>
             <message>blah1 </message>
             <message>blah8 </message>
         </build2>

    </build>
</cruise>

and I want to look in messages for blah1 in both <build> and <build2> how do I do this?

I have seen the // tag around which seems to be used to search tags and there inner tags.

Am I going along the correct lines?

Thanks again

Richard
 
Old September 23rd, 2004, 11:20 AM
Authorized User
 
Join Date: Sep 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hiya
i have solved the problem of looking in inner tags by having

<xsl:template match="/">
        <xsl:apply-templates select="cruise/build/*" />
  </xsl:template>

instead of

<xsl:template match="/">
        <xsl:apply-templates select="cruise/build/message" />
  </xsl:template>

Thanks for your help
Richard






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT 1.0: Looping through nodes kwilliams XSLT 4 December 1st, 2008 06:21 PM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
XSLT Looping Logic - Very Urgent ujayaraman XSLT 2 March 3rd, 2005 10:42 AM
xslt looping with tables... lukemedway_uk XSLT 16 December 24th, 2003 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.