You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Robert van Loenhout <r....@greenvalley.nl> on 2005/02/17 10:44:27 UTC

xpath

Hi,

I just have a little xpath escape question for which I can't seem to 
find the answer.

How do I escape special characters in xpath to match attribute values?

For example

         Document doc = new DocumentImpl();
         Element root = doc.createElement("root");
         doc.appendChild(root);
         Element child = doc.createElement("element");
         child.setAttribute("name","\"value");
         root.appendChild(child);
         Node node = 
XPathAPI.selectSingleNode(root,"child::element[@name=\"&quot;value\"]/@name");

The node will be null, so this is not the right way. What is?

TIA,
Robert


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: xpath

Posted by Robert van Loenhout <r....@greenvalley.nl>.
Robert van Loenhout wrote:
> Phil Weighill-Smith wrote:
> 
>> In this case, why not use:
>>
>> ...(root, "child::element[@name='\"value'/@name");
> 
> 
> Because the xpath is created dynamically.
> And even though a " quote can be detected first, it still does
> not help if both a " and a ' is contained in the value.

Okay, I found the answer to my question in the W3C XPath mailing list 
archive.
It is impossible to escape a (double)quote character in XPath 1.0.
The workaround is to use concat.

http://lists.w3.org/Archives/Public/www-xpath-comments/2004JanMar/0011.html


> 
> 
>>> How do I escape special characters in xpath to match attribute values?
>>>
>>> For example
>>>
>>>         Document doc = new DocumentImpl();
>>>         Element root = doc.createElement("root");
>>>         doc.appendChild(root);
>>>         Element child = doc.createElement("element");
>>>         child.setAttribute("name","\"value");
>>>         root.appendChild(child);
>>>         Node node = 
>>> XPathAPI.selectSingleNode(root,"child::element[@name=\"&quot;value\"]/@name"); 
>>>
>>>
>>> The node will be null, so this is not the right way. What is?
>>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: xpath

Posted by Robert van Loenhout <r....@greenvalley.nl>.
Phil Weighill-Smith wrote:
> In this case, why not use:
> 
> ...(root, "child::element[@name='\"value'/@name");

Because the xpath is created dynamically.
And even though a " quote can be detected first, it still does
not help if both a " and a ' is contained in the value.


>>How do I escape special characters in xpath to match attribute values?
>>
>>For example
>>
>>         Document doc = new DocumentImpl();
>>         Element root = doc.createElement("root");
>>         doc.appendChild(root);
>>         Element child = doc.createElement("element");
>>         child.setAttribute("name","\"value");
>>         root.appendChild(child);
>>         Node node = 
>>XPathAPI.selectSingleNode(root,"child::element[@name=\"&quot;value\"]/@name");
>>
>>The node will be null, so this is not the right way. What is?
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: xpath

Posted by Phil Weighill-Smith <ph...@volantis.com>.
In this case, why not use:

...(root, "child::element[@name='\"value'/@name");

Phil :n.
On Thu, 2005-02-17 at 09:44, Robert van Loenhout wrote:

> Hi,
> 
> I just have a little xpath escape question for which I can't seem to 
> find the answer.
> 
> How do I escape special characters in xpath to match attribute values?
> 
> For example
> 
>          Document doc = new DocumentImpl();
>          Element root = doc.createElement("root");
>          doc.appendChild(root);
>          Element child = doc.createElement("element");
>          child.setAttribute("name","\"value");
>          root.appendChild(child);
>          Node node = 
> XPathAPI.selectSingleNode(root,"child::element[@name=\"&quot;value\"]/@name");
> 
> The node will be null, so this is not the right way. What is?
> 
> TIA,
> Robert
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org

-- 
Phil Weighill-Smith <ph...@volantis.com>
Volantis Systems