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 February 14th, 2005, 05:05 PM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default syntax of xsl:if statement

I have a fairly simple setup here (I've stripped out a few levels for clarity).

<Activity>
    <Item>Item Text
        <Answer>Answer Text</Answer>
    </Item>

    <Item>Item Text
    </Item>

    <Item ExampleItem="Yes">Item Text
        <Answer>Answer Text</Answer>
    </Item>
</Activity>

I'm outputting to another XML file. In determining whether to create certain tags in the output file, I need to do this in the Activity template:

IF the Activity has a descendant Item which itself has an Answer descendant, AND the Item's ExampleItem attribute does not equal Yes, THEN I generate an answer_key tag. As you can see from the sample, an Activity can have multiple Items - they will not all have Answer descendants, and in fact some Activities may not have any Items which have an Answer. Basically, I need to generate the tag only if there is at least one positive match for Item/whatever/Answer where Item/@ExampleItem <> "Yes".

I've never worked with "multiple levels" of ancestry, for lack of a better phrase - meaning testing for the existence of a specific descendent, then testing for the existence of a specific descendant of that. Is it possible this way, or do I need to do nested if statements?

<xsl:template match="//Activity">

    <xsl:if test="descendant::Item/descendant::Answer[@ExampleItem='Yes']">
    GETS AN ANSWER KEY
    </xsl:if>
</xsl:template>


 
Old February 14th, 2005, 05:18 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're almost there, you want

<xsl:if test="descendant::Item[@ExampleItem='Yes']/descendant::Answer">

It's surprising how often people forget that you can have predicates on any step in a path expression.

You could also write this as

<xsl:if test="descendant::Item[@ExampleItem='Yes'][descendant::Answer]">

or various other combinations for example

<xsl:if test="descendant::Item[descendant::Answer]/@ExampleItem = 'Yes'">

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 14th, 2005, 05:27 PM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

shorthand

   .//Item[@ExampleItem!='Yes']//Answer


or,

   descendant::Item[@ExampleItem!='Yes']/descendant::Answer

Regards
Bryan


 
Old February 14th, 2005, 05:47 PM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you both. I had actually gotten as far as

<xsl:if test="descendant::Item[descendant::Answer]">

after I posted, but couldn't figure out how to work in the attribute value. Things generally turn out to be a lot simpler than I'm trying to make them... :o)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error in INSERT INTO statement. kingleon Classic ASP Basics 1 May 10th, 2005 06:25 PM
Select Statement syntax jmss66 VB How-To 2 July 12th, 2004 08:34 AM
Syntax error in INSERT INTO statement. askaggs Classic ASP Databases 5 June 10th, 2004 12:21 AM





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