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 June 1st, 2012, 10:03 PM
Registered User
 
Join Date: May 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default using AND operator

hiya,

how can i get two recursive calls in my template to evaluate my constraint using the AND operator?

xml file structure:

<filterML>
<constraint>
<and> <!-- 2 conditions -->
<operator operation="="> <!-- condition1: get all subjects evaluatung to chemistry -->
<input>subject</input>
<value>chemistry</value>
</operator>
<operator operation="&lt;"> <!-- condition2: get all age < 70 -->
<input>age</input>
<value>70</value>
</operator>
</and>
</constraint>
</filterML>


stylesheet:

<xsl:template name="evaluateFilterML">
<xsl:param name="operator" />
<xsl:param name="input" />
<xsl:param name="value" />
<xsl:choose>
<xsl:when test="$operator = 'equal'">
<xsl:value-of select="$input = $value"/>
</xsl:when>
<xsl:when test="$operator = 'not equal to'">
<xsl:value-of select="$input != $value"/>
</xsl:when>
<xsl:when test="$operator = 'less than'">
<xsl:value-of select="$input &lt; $value"/>
</xsl:when>
<xsl:when test="$operator = 'greater than'">
<xsl:value-of select="$input &gt; $value"/>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="/">
<xsl:param name="currentyear" select="2012" />
<xsl:call-template name="evaluateFilterML">
<xsl:with-param name="operator" select="//filterML/constraint/and/operator[@operation='=']" />
<xsl:with-param name="input"> <!-- recursively evaluate input value -->
<xsl:apply-templates select="//citizensOfBritik/citizen/education/qualification[subject='chemistry']" />
</xsl:with-param>
<xsl:with-param name="value"> <!-- recursively evaluate condition value -->
<xsl:apply-templates select="($currentyear - (substring((dob),1,4))) &lt; 70" />
</xsl:with-param>
</xsl:call-template>
</xsl:template>


--- so i need my stylesheet to output:
<xsl:apply-templates select="//citizensOfBritik/citizen/education/qualification[subject='chemistry']" /> AND <xsl:apply-templates select="($currentyear - (substring((dob),1,4))) &lt; 70" />
 
Old June 2nd, 2012, 01:24 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm a bit confused by what you are doing because your stylesheet appears to be taking both the "FilterML" and some data file relating to citizens and education as input: are these in the same file, or what?

Your FilterML XML source seems to be some kind of little programming language, and when handling such a language using XSLT, I generally find that translating it to an XSLT stylesheet and then executing the stylesheet is a good approach. The resulting XSLT stylesheet is then applied to the data file (the one about citizens and education).

It's not really clear to me whether you are interpreting FilterML or compiling it (translating it to XSLT). Both are possible. Unfortunately you've also failed to tell us which XSLT version you are using.

I did attempt to answer your question in more detail but I realized I made far too many assumptions about what you are doing, which might be completely wrong. You need to explain more carefully.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 2nd, 2012, 08:01 PM
Registered User
 
Join Date: May 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi michael,

sorry for being unclear, i'll start again,

1. YES, im using xslt 1.0 and my citizen data belongs in the same xml file that has my filter i.e.

<filterML>
<constraint>
<and> <!-- 2 conditions -->
<operator operation="="> <!-- condition1: get all subjects evaluatung to chemistry -->
<input>subject</input>
<value>chemistry</value>
</operator>
<operator operation="&lt;"> <!-- condition2: get all age < 70 -->
<input>age</input>
<value>70</value>
</operator>
</and>
</constraint>
</filterML>

.
.
.
<rest of my main 'citizen' xml content sits here>

2. my objective is to get my stylesheet to evaluate the above filterML constraints, so i have a template names 'evaluatefilterML', which consists of my operations, my input and value.
Also, within the same stylesheet, i have declared two match templates (which is incorrect as i explain later) that trigger the evaluation, i.e. they aim to evaluate each operator or condition i.e. 'get me citizens that have studied subject chemistry' and 'get me citizens below age 70', but of course as you can see they are not joined to perform an 'AND'.

-so my trouble is: how do i declare my match template - rather than having two separate ones - that does an AND operation on both conditions in the one go?

<xsl:template match="*/@operation">
<xsl:param name="currentyear" select="2012" />
<xsl:call-template name="evaluateFilterML">
<xsl:with-param name="operation" select="//filterML/constraint/and/operator[@operation='=']" />
<xsl:with-param name="input"> <!-- recursively evaluate input value -->
<xsl:apply-templates select="//citizensOfBritik/citizen/education/qualification/subject" />
</xsl:with-param>
<xsl:with-param name="value"> <!-- recursively evaluate condition value -->
<xsl:apply-templates select="//citizensOfBritik/citizen/education/qualification[subject='chemistry']" />
</xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="operator">
<xsl:param name="currentyear" select="2012" />
<xsl:call-template name="evaluateFilterML">
<xsl:with-param name="operation" select="//filterML/constraint/and/operator[@operation='&lt;']" />
<xsl:with-param name="input"> <!-- recursively evaluate input value -->
<xsl:apply-templates select="//citizensOfBritik/citizen/dob" />
</xsl:with-param>
<xsl:with-param name="value"> <!-- recursively evaluate condition value -->
<xsl:apply-templates select="($currentyear - substring((dob),1,4)) &lt; 70" />
</xsl:with-param>
</xsl:call-template>
</xsl:template>


also, when i execute stylesheet above in that state, saxon returns error against the line: <xsl:apply-templates select="($currentyear - substring((dob),1,4)) &lt; 70" />
error: "the value is not a node-set" ??


hope that has made things clearer...

thank you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
BETWEEN operator RockBal SQL Language 1 April 7th, 2011 12:55 AM
how to use like operator thillaiarasu SQL Server 2000 4 April 23rd, 2010 09:47 AM
Comma Operator phuc_tran BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 6 June 12th, 2009 04:46 PM
=== operator in c# surendraparashar C# 2005 8 November 8th, 2007 05:14 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM





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