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 April 13th, 2016, 09:27 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Checking if Parameter is Empty/Null

Hi,

I have an XSLT parameter which is defined but not given an initial value (the parameter is populated by a webapp we don't have control over and which doesn't always send a value).

When it does have a value, it is an XML document which we can then run Xpath queries against.

What is the best way to check if this parameter is empty/null?
I'm initially doing a string-length command to check if it is > 0, but I'm also not sure that when the parameter is populated with an XML document it returns a string length of over zero.

Thanks,
Neil
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old April 13th, 2016, 09:37 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Is that XSLT 1.0 or 2.0? Can you change the XSLT to have
Code:
<xsl:param name="param-name" select="/.."/>
as the default value? That would be an empty node set so you could check
Code:
<xsl:if test="not($param-name)">...</xsl:if>
to check for that empty node set as a default.

In case of XSLT 1.0, check whether the processor supports http://exslt.org/exsl/functions/object-type/index.html, if so you can check
Code:
<xsl:param name="param-name"/>...
<xsl:if test="exsl:object-type($param-name) = 'string' and $param-name = ''">...</xsl:if>
, with XSLT 2.0 you can use
Code:
<xsl:param name="param-name"/>...
<xsl:if test="$param-name instance of xs:string and $param-name = ''">...</xsl:if>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; April 13th, 2016 at 09:55 AM.. Reason: adding type tests for string type and value test for empty string
 
Old April 13th, 2016, 10:46 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi Martin, thanks for your reply.

I see what you mean, however the parameter is actually pre-populated outside of the transform stylesheet so I can't set a default value (as this would override that preset value).
The problem is that the parameter is not always pre-populated, and I need to test for those instances.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old April 13th, 2016, 10:55 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If a parameter is not set, it has an empty string as its value, see the two checks I suggested.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking to see if a label's text is empty mh ASP.NET 2.0 Basics 2 October 26th, 2005 10:38 AM
Help checking for empty dataset Payback ASP.NET 1.0 and 1.1 Basics 5 March 28th, 2005 05:00 AM
Checking for empty string richw32001 General .NET 1 August 8th, 2004 01:37 PM
checking for empty recordsets ! spraveens Classic ASP Databases 0 September 9th, 2003 10:18 PM





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