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 27th, 2008, 04:38 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Expected values in a condition, XPath 1.0

I'm trying to validate an attribute value to only have 'true or false'. I tried using contains() and '|' or ',' to surround the possible values as below

contains(concat('|',//node()[local-name()='message']/@msgRequired,'|'),"|true|false|")

unfortunately it doesn't work so I had to do it the traditional way

//node()[local-name()='message']/@msgRequired="false" or //node()[local-name()='message']/@msgRequired="true"

But I have another validation need that is expecting 10 possible values and writing the traditional method as above maybe too much to do. So, I wonder if I clean the code above just a bit to make it easier to read or can I use contains() as described earlier?

Many thanks again!



 
Old June 27th, 2008, 05:14 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

contains() looks for a substring, whereas you seem to be trying to match the whole value, so you want "=".

The simplest approach is just to spell it out:

/path/node[.='true' or .='false' or .='maybe']

In XPath 2.0 you can write

/path/node[.=('true', 'false', 'maybe')]

or you can use regular expressions:

/path/node[matches(., '^(true|false|maybe)$')]

Another approach in XPath 1.0 is to put your strings in a lookup document

<values>
  <value>true</value>
  <value>false</value>
  <value>maybe</value>
</values>

and then

/path/node[. = $values/value]

assuming your API gives you a chance to bind the variable.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 27th, 2008, 05:48 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

First of all, I bought your books (XSLT 2 and Professional XML). Bought them years back but never really got to appreciate XML until now that I am involved in a serious project.

Now I'm pulling my hair out because I have tons of pages to go through and time is against me.

I really appreciate your quick response to my inquiry. As for reference, I have this as a direct translation

//node()[local-name()='message']/@msgRequired [.='true' or .='false' or .='maybe']

Everything is fine now. :)

Thanks and more power!









Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath Error : Expression Expected deean XML 1 June 14th, 2008 05:57 PM
List of Distinct values using xpath and javascript pnviv XSLT 4 November 5th, 2007 06:52 AM
NEWBIE Question: Expected "" in search condition aasiddle SQL Language 5 March 19th, 2007 03:49 PM
NEWBIE Question: Expected "" in search condition aasiddle ASP.NET 2.0 Basics 0 March 4th, 2007 11:21 AM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM





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