Reputation: 313
I am having an XML like this:
<root>
<parent1>
<parent2>
<child label="single'quote"/>
<child label="double"quote"/>
</parent2>
<parent3/>
<parent4>
<child label = "single'quote"/>
</parent4>
</parent1>
</root>
My aim is to select the child node with a particular label,say single'quote
I can use the XPath Expression,
//child[@label='single'quote']
which will obviously throw error because of the special character ' in the value.
I came up with a solution to use the logic:
set an attribute dummy to the parent node with the value of the label, and compare the two attributes. [Added dummy="single'quote" to parent1 node.]
So, We can read the child nodes with the label as "single'quote" using:
//child[@label=//parent1/@dummy]
Though it works perfectly fine in IE and Firefox9, it fails in Chrome21.
Is there any flaw with the logic? How can I correct the expression for Chrome?
Thanks a lot.
Upvotes: 1
Views: 709
Reputation: 243599
Use:
/*/parent1/parent2/*[@label="single'quote"]
Here is a screen-shot of the evaluation performed with the XPath Visualizer:
For the second question use:
/*/parent1/parent2/*[@label='double"quote']
Here is a screen-shot from the XPath Visualizer:
Upvotes: 1