Solved

Using wildcards with XPath and translate function

Posted on 2007-11-28
5
1,655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

689 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