Xpath using contains

173 Views Asked by At

I'm trying to find the element by using contains.The problem is If the string contains one single quote or double quote,it's difficult to get it. I'm using this xpath to match directly to the first element.Is there a better way to implement without using 'and' statement here ?

Xpath Used :-

.//*[local-name()='GEOGRAPHY_TITLE'][contains(.,"APAC > Andaman Islands > test&'<,")and contains(., '"123')]

XML Used :-

<MST>
<MST_GEOGRAPHY >
               <GEOGRAPHY_TITLE>APAC &gt; Andaman Islands &gt; test&amp;'&lt;,\"123</GEOGRAPHY_TITLE>
               <GEOGRAPHY_ID>5a7a24ec-93ff-8be6-7ef9-fa021500df0e</GEOGRAPHY_ID>
               <TENANT_ID>{0559cdcb-c63b-4c81-be91-b78e831bf5a5}</TENANT_ID>
               <ACTIVE>1</ACTIVE>
</MST_GEOGRAPHY>
<MST_GEOGRAPHY >
               <GEOGRAPHY_TITLE>APAC &gt; Andaman Islands &gt; test\"123&amp;'&lt;,\"123</GEOGRAPHY_TITLE>
               <GEOGRAPHY_ID>5a7a24ec-93ff-8be6-7ef9-fa021500df0e</GEOGRAPHY_ID>
               <TENANT_ID>{0559cdcb-c63b-4c81-be91-b78e831bf5a5}</TENANT_ID>
               <ACTIVE>1</ACTIVE>
</MST_GEOGRAPHY>
<MST_GEOGRAPHY >
               <GEOGRAPHY_TITLE>hi</GEOGRAPHY_TITLE>
               <GEOGRAPHY_ID>5a7a24ec-93ff-8be6-7ef9-fa021500df0e</GEOGRAPHY_ID>
               <TENANT_ID>{0559cdcb-c63b-4c81-be91-b78e831bf5a5}</TENANT_ID>
               <ACTIVE>1</ACTIVE>
</MST_GEOGRAPHY>
</MST>
2

There are 2 best solutions below

0
On

This may be a style thing, but I would probably rewrite your XPath to this:

//*:GEOGRAPHY_TITLE[contains(., "APAC > Andaman Islands > test&'<,")][contains(., '"123')]

In this manner the selection of the element is absolute and static rather than the evaluation of the local-name function, this should prove more efficient. As you are already familiar with using multiple predicates as a way of AND'ing constraints, it would seem sensible to me to also use multiple predicates for the contains rather than the 'and' keyword. Typically (but not always) if you place the most selective predicate first, you will see better performance than placing the least selective predicate first.

0
On

It is possible to select the wanted element(s) without using contains() but just with a strict comparison -- like this:

//*/text()[1][. = concat("APAC > Andaman Islands > test&'<,\", '"123')]

Explanation:

Do note the use of the standard XPath function concat() so that either quotes or apostrophes can be use to surround parts of the string that don't contain them -- and to concatenate these parts of the string into the complete string.

Note: This solution uses pure XPath 1.0 only.