Solved

Using wildcards with XPath and translate function

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 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