?
Solved

Using wildcards with XPath and translate function

Posted on 2007-11-28
5
Medium Priority
?
1,658 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 2000 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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 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