Solved

xpath test on "

Posted on 2004-03-30
4
412 Views
Last Modified: 2008-02-26
hi,

I used xpath expression:
//Object1/@name[contains(.,'"')]
to test if the Object1 name attribute contains "
It does not return the first Object1.

However, if I use expression:
//Object1/@name[contains(.,'"')]
It gives correct object.

Why?

<test>
<Object1 name="&quot;AA&quot; BB">11</Object1>
<Object2 name="&quot;AA&quot;">22</Object2>
<Object1 name = "CC">33</Object1>
</test>
0
Comment
Question by:danclemson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10721048
It's your parser.  it's not unescaping the value in the literal.  The .NET parser (which I tested with) performed as you expected:

//Object1/@name[contains(.,'&quot;')] worked and found the match
while

//Object1/@name[contains(.,'"')]
threw a parser error
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 125 total points
ID: 10721347
No, it's not a matter of unescaping.  I'll bet he's using DOM XPath methods.

If he's using the XPath in a SelectSingleNode() or SelectNodes() expression, then his behavior is as expected.  

If he's using XSLT, then the XPath expression

//Object1/@name[contains(.,'"')]

is not well-formed, hence your parser throwing the exception.  All conforming parsers will throw this exception, including the one danclemson is using.  But he's probably using the XPath as SelectSingleNode, where the XPath does not get parsed!

Here's why...

The XML parser reads this:

<Object1 name="&quot;AA&quot; BB">11</Object1>

and then it expands the entity in the name attribute, so that the value, in memory, is:

"AA" BB

Once the attribute has been parsed, then entity isn't there any more.  Instead, the *actual* character represented by the entity is there. This isn't a problem, because the attribute has already been parsed, and all knowledge of the delimiters originally used in the attribute is lost.  

Now you need an XPath expression to select this attribute.  If you use XSLT, then the XPath expression you use must be well-formed, and your XPath expression will contain a &quot; instead of a doublequote character, because the doublequote character is not well formed.  So in my test, I used:

<xsl:value-of select="@*[contains(., '&quot;')]"/>

but when you're using selectSingleNode(), as long as you don't violate your language's rules for nesting of quotes and apostrophes, you DON'T use the entity reference.  The XPath in the selectSingleNode is never parsed by the XML parser!

selectSingleNode(\"@*[contains(., '"')]\")
or possibly
selectSingleNode("@*[contains(., '"')]")

Regards,
Mike Sharp
0
 

Author Comment

by:danclemson
ID: 10723720
Hi,

Thanks for the reply.
I think both of you are right.
I was using XMLSPY xpath evaluation.
Don't know what's the underline implementation of this xpath evalutation function. But seems it's not escaping the entity reference.
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 10728422
In my version of XML Spy (4.4), it finds the node with

//Object1/@name[contains(.,'&quot;')]

and not

//Object1/@name[contains(.,'"')]

But XML Spy has some peculiarities.  I believe they're treating it as an XSLT XPath, because that's what you'd be doing with Spy.  I'm surprised you don't get it with the first expression and you do with the second...what version are you using?

I created an XPath evaluator some years ago that worked through the web.  That's when I discovered that the selectSingleNode method does not parse the XPath as "XML", and entities are not expanded.  

Regards,
Mike Sharp
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question