Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Case insensitivity using Xpath and libXML

by Justudo (Novice)
on May 14, 2004 at 04:31 UTC ( [id://353275]=perlquestion: print w/replies, xml ) Need Help??

Justudo has asked for the wisdom of the Perl Monks concerning the following question:

Hi all monks. I'm using libXML and a Xpath fashion of search. Have a sample Xpath search string like.
//userprop[nameprop="Version" and contains(valueprop,"v1.1")]
Is it possible to add in case insensitive searches like this ?
//userprop[nameprop="Version" and contains(lc(valueprop),lc("v1.1"))]
Sample data :
<userprop> <nameprop>Version</nameprop> <valueprop>v1.1</valueprop> </userprop> <userprop> <nameprop>Version</nameprop> <valueprop>V1.1</valueprop> </userprop> <userprop> <nameprop>Subject</nameprop> <valueprop>Not applicable</valueprop> </userprop>

Replies are listed 'Best First'.
Re: Case insensitivity using Xpath and libXML
by derby (Abbot) on May 14, 2004 at 12:05 UTC
    It looks ugly but translate should work. Try (untested):
    //userprop[nameprop="Version" and contains(translate(valueprop, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),"v1.1 +")]
    I'm not sure why you would want to lc the static string.
    -derby
Re: Case insensitivity using Xpath and libXML
by dakkar (Hermit) on May 14, 2004 at 13:27 UTC

    XSLT does not define any case-mapping functions. translate works only if you know which characters you can find in the string (for the same reasons that in Perl tr{A-Z}{a-z} will not lowercase a string correctly)

    You could write an extension function in Perl, register it in libxmls's xpath context, and use it. There should be a test in the XML::LibXML distribution defining extension functions.

    -- 
            dakkar - Mobilis in mobile
    

    Most of my code is tested...

    Perl is strongly typed, it just has very few types (Dan)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://353275]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found