Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ignore case in javax.xml.xpath expression 1

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
US
I have a java app in which I use javax.xml.xpath to get references to nodes in XML documents. I'm looking for nodes that have attributes with specific names. I dynamically add the attribute name using inputMsgName as in the following:

Code:
String expression="/xs:schema/xs:complexType[@name='" + inputMsgName + "']/xs:complexContent/xs:extension/xs:sequence/xs:element";
XPathExpression expr = xpath.compile(expression);
Object result = expr.evaluate(doc,XPathConstants.NODESET);

Originally the attribute names in my XML files were all initial cap, but now some files also have attribute names with iniital lower case.

Is there a way to indicate that I want to ignore case in the supplied inputMsgName?

I realize that I can rewrite my app to have an additional call with inputMsgName converted to initial lower case, but I would rather go with a simpler solution if it is possible.

Thanks.
 
[0] It needs some precision. Xml is case-sensitive and XPath is as well. One can make though xpath comparison test in the predicate on the basis of uniform case (upper or lower) or with more effort transform the case of individual character. Furthermore, I doubt much op is working with xpath 2.0.

[1] There is xpath 1.0 function (as well as 2.0, sure) to do the "case-insensitive" comparison.
>xs:complexType[@name='" + inputMsgName + "']
[tt]xs:complexType[[blue]translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcedfghijklmnopqrstuvwxyz')[/blue]='" + inputMsgName[blue].toLowerCase()[/blue] + "'][/tt]
The limitation to ascii alphabetic characters seems obvious, otherwise it would be quite a bit of more work.
 
There's only one reply I can give to that: Yuc. ;-) Might be good for data transformation, but not to get 'some field like ...' from your carefully designed and published object structure.

I can only conclude: XML is case-sensitive. (And it should stay that way)
 
Yes but you can design an XPath query (as well as an XSL transformation) that will fit on different XML documents with the same tags with different capitalization (if that word exists in English)

Cheers,
Dian
 
It matters if you have specified the format/structure of the XML file, not if it's supplied from some 3rd party. In the first case the supplying party should correct their application for generating the XML, in the latter case, just add the case-insensitivity to your XPath queries, as it's not under your control.

HTH
 
I have nothing else to say except my first posted response. That speaks for itself and it is plainly sufficient.
 
Thanks everyone.

I checked with the xml supplier and they will not modify their files, so I plan to use the case insensitive solution suggested by Dian. (Thanks Dian!)
 
Oops -- the credit for the actual solution goes to tsuji -- thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top