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 November 19th, 2003, 10:30 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Predicate question

My transform seems to be working but I want to make sure this does what I think it does.

I have a document with one or more "meeting_pattern" elements, some of which may have a "topic" child element. After displaying other information from each meeting pattern I want to display the "topic" from the first meeting pattern that has a "topic" child.

Should this do that?
<other stuff>
<xsl:apply-templates select="meeting_pattern[topic][position() = 1] mode ="topic"/>
<xsl:template match="meeting_pattern" mode="topic">
<xsl:value-of select="topic"/>
</xsl:template>

 
Old November 19th, 2003, 11:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Depends on the structure of your file. If all meeting_pattern elements are siblings it does not matter, if they are not it might. Example:
Code:
<meetings>
  <meeting>
   <meeting_pattern><topic>A</topic></meeting_pattern>
  </meeting>
  <meeting>
   <meeting_pattern></meeting_pattern>
  </meeting>
  <meeting>
   <meeting_pattern><topic>B</topic></meeting_pattern>
   <meeting_pattern><topic>C</topic></meeting_pattern>
  </meeting>
  <meeting>
   <meeting_pattern><topic>D</topic></meeting_pattern>
  </meeting>
  <meeting>
   <meeting_pattern></meeting_pattern>
  </meeting>
</meetings>
When you ask for "/meetings/meeting/meeting_pattern[topic][1]" you are asking for all meeting_pattern elements that have a topic child and are the first meeting_pattern in their block so you get Topics A, B and D. If you just want the first one with a topic over the whole document then use parentheses, "(/meetings/meeting/meeting_pattern[topic])[1]".
If your structure was simpler, like this:
Code:
<meetings>     <meeting_pattern><topic>A</topic></meeting_pattern>
<meeting_pattern></meeting_pattern>       <meeting_pattern><topic>B</topic></meeting_pattern>
<meeting_pattern><topic>C</topic></meeting_pattern>    <meeting_pattern><topic>D</topic></meeting_pattern>
<meeting_pattern></meeting_pattern>
</meetings>
then "/meetings/meeting_pattern[topic][1]" and "(/meetings/meeting_pattern[topic])[1]" would both give Topic A.
I have used the short form of "[1]" which equals "[position() = 1]" in all these examples.

Joe (MVP - xml)
 
Old November 19th, 2003, 11:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

It's ok, but in that case you'll be able to process only that "meeting_pattern" (which is the first "meeting_pattern" having a "topic" child in document order). So for generality, you'll need this (note that you missed quotation in <xsl:apply-templates select="meeting_pattern" mode ="topic"/>):

Code:
<other stuff>
<xsl:variable name="selected-pattern" select="(//meeting_pattern[topic])[1]"/>
<xsl:apply-templates select="meeting_pattern" mode ="topic"/>
<xsl:template match="meeting_pattern" mode="topic">



   <xsl:if test="generate-id() = generate-id($selected-pattern)">
      <xsl:value-of select="topic"/>
   </xsl:if> 
</xsl:template>
Regards,
Armen
 
Old November 19th, 2003, 11:30 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My document is like your first example but that is actually the behavior that I need so one topic per <meeting> is perfect.

Very nice explanation, Thanks!
-Paul






Similar Threads
Thread Thread Starter Forum Replies Last Post
Complex predicate question dcheslow XSLT 4 February 25th, 2007 02:21 PM
A problem with predicate in XSLT1.0:( sureshk XSLT 1 March 8th, 2006 01:13 PM
SUM function with DISTINCT predicate saturdave SQL Server ASP 1 April 8th, 2004 10:44 PM
Predicate not being checked properly JeanFrancois XSLT 2 June 16th, 2003 09:04 AM





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