Solved

Using wildcards with XPath and translate function

Posted on 2007-11-28
5
1,638 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I assign SimpleXMLElements to variables? 5 65
Stupid git question 2 88
AL3 Files 4 29
Grunt script for Build Process 1 29
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

810 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