Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old September 12th, 2007, 09:31 AM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default Testing for presence of a variable

Hi everyone!

I came across a little problem recently and I need to know if there's a way to solve this.

Let's say I have a style sheet called "wrapper.xsl" that imports two other stylesheets called "doSomething.xsl" and "placeHolder.xsl".

"wrapper.xsl" defines a variable named "location" which is used in "doSomething.xsl". However, due to the nature of my application, I sometimes need to use "doSomething.xsl" directly, without "wrapper.xsl".

My question is: is it possible to check for the presence of a variable in the context of "doSomething.xsl"?

I was thinking of something like this in "doSomething.xsl":

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:variable name="hasLocation">
  <xsl:choose>
   <xsl:when test="boolean($location)">
    <xsl:value-of select="true()"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="false()"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>
 <xsl:template match="/">
  <xsl:if test="$hasLocation">

  </xsl:if>

 </xsl:template>
</xsl:stylesheet>

I don't know if there is a solution. I probably will find a workaround in the application, but still, I find this issue interesting.

Thanks,

Rushman

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
__________________
Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
  #2 (permalink)  
Old September 12th, 2007, 09:41 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, it isn't possible.

I think it's probably poor coding style to reference a variable that's defined only in an importing stylesheet, though it's legal.

I would suggest defining $location in doSomething.xsl, with some fallback value such as an empty string, and then wrapper.xsl can override it with a "real" value. This way $location will always exist and you can test whether it has been overridden by testing whether it is equal to "".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old September 12th, 2007, 10:36 AM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Thank you M. Kay, your help is always greatly appreciated.

Rushman


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing with the presence of an XML tag rather tha Mike250 SQL Server 2005 1 June 6th, 2008 03:50 PM
How to Test for Presence of a File BrianWren Visual Basic 2005 Basics 7 April 30th, 2008 05:44 PM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
How to check for the presence of an attribute dsekar_nat XSLT 2 July 27th, 2006 05:04 AM
check presence of <?xml version="1.0"> sonhir XSLT 2 June 8th, 2006 04:54 PM





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