Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath for attributes with single and double quotes

Status
Not open for further replies.

Girisht

Programmer
Aug 2, 2001
5
US
In my XML document, some node attributes data contains both single quote and double quote characters, such as <input msg="Hello &#34;World&#34;, What's up"/>. The double quotes are in form of escape sequence. I am not able to locate such elements using an XPath expression. I tried the following:

//*[@msg="Hello "World", What's up"]
Failes with string error at double quote before World

//*[@msg='Hello "World", What's up']
Failes with string error at single quote after What

//*[@msg="Hello &#34;World&#34;, What's up"]
No Error but does not return any node

//*[@msg="Hello &#34;World&#34;, What&#39;s up"]
No Error but does not return any node

I tried both MSXML 3.0 and MSXML 4.

Any ideas??
 
Have you tried replacing both your XML file and XSLT with XML's predefined entities for " and '.

Hello &quot;World&quot;, What&apos;s up

I ran this test through and it worked. XPath will attempt to resolve entities while working through the location path. It is possible that if you use HTML friendly representation for quotes and apostrophes that XPath does not know how to convert these into the values they are representing. Therefore they cannot find the location path for those characters. You should use XMLs representation for the 5 predefined entities it provides.

-jay
 
I can change the XPath to use escape sequence suggested by you but the source XML is out of my control and it has &#34; for double quot and ' for single quot
 
I think the problem is not the escaping per se, but the fact that single-quote is a string-demiliter in the expression:
<xsl:template math="node[@attribute='textvalue']">
I don't see a good solution, but I can think of a rather silly way around it:
put the string in a variable: that way there are no single-quotes in your expresion. However: variables are not allowed in the match-attribute, so you'll have to test yourself.
This is the result:
Code:
<xsl:variable name="matchtext">
  <xsl:text>Hello &#34;World&#34;, What&#39;s up</xsl:text>
</xsl:variable>
...
<xsl:template match="//*[@msg]">
  <xsl:if test="@msg=$matchtext">
    <!-- Yes it's a hit -->
  </xsl:if>
</xsl:template>
 
Sorry for the misinformation.

Jel is definitely write on this one. I did the following successful test:

Code:
xml file:
<?xml version="1.0" encoding="UTF-8"?>
<blah>
	<blah msg="Hello &#34;World&#34;, What&#39;s up">Blah</blah>	
</blah>

xslt file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="//*[@msg]">
		<xsl:variable name="matchtext" select="@msg"/>
		<xsl:if test="@msg=$matchtext">
			<xsl:value-of select="$matchtext"/>
	    </xsl:if>
	</xsl:template>		
</xsl:stylesheet>

Thanks for the correction Jel.

-jay
 
There is tool called XPathVisualizer on TopXml.com that uses MSXML3 and validates the XPaths. Try testing your solution on it. Does not work :(
 
I tested this with Saxon and Xalan (java). I also tested in XML Spy which uses MSXML 4.

-jay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top