Solved

Using wildcards with XPath and translate function

Posted on 2007-11-28
5
1,634 Views
Last Modified: 2013-11-18
I am trying to write an Xpath query that uses the translate function. The problem that I'm having is that sometimes my value in translate function needs to be a wildcard (like '*').  How do I get the following XPath to work ? Note that I'm using XPath 1.0

/MyNode[translate(Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=*]/Name
0
Comment
Question by:mromeo
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20368492
I am not sure I understand what you want to do
you can translate as many characters to a * as you want to
what effect are you trying to achieve?

what is in Name and what do you want to test
the translate that you use just makes everything  a lowercase
0
 

Author Comment

by:mromeo
ID: 20368678
Look at the following web site.
http://www.xmlme.com/XpathTool.aspx?mid=82
This is an xpath query tool.  If you enter the following into the query expression, then you get the expected result of 'Alice Smith'

//purchaseOrder/shipTo[translate(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='alice smith']/name

But if you enter:
//purchaseOrder/shipTo[translate(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')=*]/name

You do not get the results desired -- which I would like to be the list of all names.  I would like to be able to enter the * wildcard to get all names back.  But I also want the translate function there in case there is a name to search. My program needs to be able to handle both cases.  Let me know if you still need more information.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20368939
the '=' tests for exact string-match
so you can't make it work with a wildcard

the only thing I can imagine that comes close is this
//purchaseOrder/*[contains(translate(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'smith')]/name

now you use contains()
if you now have a '' as the second argument you get all the names

since you dynamically construct the XPath (I assume)
it is a piece of cake for you to replace an '*' in the request by a '' in the XPath

similarly you could switch between '=' and 'contains()' based on the content of the request, in the code

cheers

Geert
0
 

Author Comment

by:mromeo
ID: 20377863
Excellent...this works for me! Thanks!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20377876
welcome
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to dynamically set the form action using jQuery.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now