sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
David Thielen's Avatar

xpath when quotes are in node


Question posted by: David Thielen (Guest) on November 12th, 2005 05:09 AM
I have the following xml:

<root>
<name>abc "123" xyz</name>
</root>

When I get an XPathNavigator to root and then call navigator.Evaluate("name
= \"abc \\\"123\\\" xyz"), I get an XPathException. I also tried \\" and " -
all throw an exception.

How am I supposed to handle this?

--
thanks - dave
4 Answers Posted
Martin Honnen's Avatar
Guest - n/a Posts
#2: Re: xpath when quotes are in node



David Thielen wrote:
[color=blue]
> I have the following xml:
>
> <root>
> <name>abc "123" xyz</name>
> </root>
>
> When I get an XPathNavigator to root and then call navigator.Evaluate("name
> = \"abc \\\"123\\\" xyz"), I get an XPathException. I also tried \\" and " -
> all throw an exception.[/color]

XPath allows both ' and " as the delimiters to string literals so in C#
a string literal like
"name = 'abc \"123\" xyz'"
should do, the outer " delimits the C#/.NET string, the ' delimits the
XPath string and \" is necessary to escape the double quote in the C#
string literal.

But there can be ugly situations with both ' and " used where you need
to split strings and use concat to build your XPath expression in a
syntactically correct way.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
David Thielen's Avatar
Guest - n/a Posts
#3: Re: xpath when quotes are in node

Hello;

But isn't \" an official part of xpath?

My problem is that the xpath statements come from xml so we have <node
attr='xpath'/> and so the xpath is "abc \\\"123\\\" xyz" making an actual
node of.

This works fine in java (dom4j/jaxen) and I assumed this is standard???

--
thanks - dave


"Martin Honnen" wrote:
[color=blue]
>
>
> David Thielen wrote:
>[color=green]
> > I have the following xml:
> >
> > <root>
> > <name>abc "123" xyz</name>
> > </root>
> >
> > When I get an XPathNavigator to root and then call navigator.Evaluate("name
> > = \"abc \\\"123\\\" xyz"), I get an XPathException. I also tried \\" and " -
> > all throw an exception.[/color]
>
> XPath allows both ' and " as the delimiters to string literals so in C#
> a string literal like
> "name = 'abc \"123\" xyz'"
> should do, the outer " delimits the C#/.NET string, the ' delimits the
> XPath string and \" is necessary to escape the double quote in the C#
> string literal.
>
> But there can be ugly situations with both ' and " used where you need
> to split strings and use concat to build your XPath expression in a
> syntactically correct way.
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>[/color]
Martin Honnen's Avatar
Guest - n/a Posts
#4: Re: xpath when quotes are in node


David Thielen wrote:
[color=blue]
> But isn't \" an official part of xpath?[/color]

As an escape mechanism? No, at least in XPath 1.0 there is no such
escape. You can use ' and " but there is no escape character.
And if the XPath is used inside of an XML document (e.g. an XSLT
stylesheet) then you can use the XML escape mechanisms e.g. &quot;
<xsl:value-of select="&quot;Kibology&quot;" />


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
David Thielen's Avatar
Guest - n/a Posts
#5: Re: xpath when quotes are in node

Yuck - ok - thanks


--
thanks - dave


"Martin Honnen" wrote:
[color=blue]
>
> David Thielen wrote:
>[color=green]
> > But isn't \" an official part of xpath?[/color]
>
> As an escape mechanism? No, at least in XPath 1.0 there is no such
> escape. You can use ' and " but there is no escape character.
> And if the XPath is used inside of an XML document (e.g. an XSLT
> stylesheet) then you can use the XML escape mechanisms e.g. "
> <xsl:value-of select=""Kibology"" />
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>[/color]
 
Not the answer you were looking for? Post your question . . .
196,844 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,844 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors