EDN Admin
Well-known member
Im trying to use XPath to validate data in an XML file and am having difficulty with determining whether or there are non numeric characters in a field. The problem seems to involve using the value of an attribute as input data for an XPath function.
We have used a couple of other test tools to evaluate the XPath expressions. Both of them produced the expected results.
I have created a small test program that shows the problem. It is at the end of this post. I have summarized the results in the following table. As you can see, the results arent consistent with the value of the Value attribute.
<table border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none
<tbody>
<tr style="
<td width="152" valign="top" style="width:114.1pt; border:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt
<p style="margin-bottom:.0001pt; line-height:normal XML
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal Context
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <span>XPath
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal Result
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal Expected Result
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name="<span>acctID" Value="00000003a91" />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal translate(string(@Value), 0123456789, )
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red ""(empty string)
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal "a"
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name="<span>acctID" Value="00000003a91" />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal translate("00000003a91", 0123456789, )
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal "a"
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal "a"
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name="<span>acctID" Value="00000003a91" />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal translate(@Value, 0123456789, )
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red ""
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red "a"
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name=<span>acctID Value=00000003a91 />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal string(number(@Value)) = "<span>NaN"
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal true
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal true
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name=<span>acctID Value=00000003191 />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal string(number(@Value)) = "<span>NaN"
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red true
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red false
</td>
</tr>
</tbody>
</table>
Can someone help me get this running or suggest some alternatives please?
Thanks
Mike Heres my test program. I entered the text from the table above into three textboxes in a small winForms application
<span style="font-size:9.5pt; font-family:"Courier New"
<pre> XPathDocument xPathDocument = new XPathDocument(new StringReader(theXml.Text));
XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
XPathNodeIterator nodes = xPathNavigator.Select(theContext.Text);
XPathExpression query = nodes.Current.Compile(theXPath.Text);
object result = xPathNavigator.Evaluate(query, nodes);
[/code]
I set a breakpoint on the line after the call to Evaluate the expression. It was done this way as I wanted to examine "result" with the Visual Studio debugger.
View the full article
We have used a couple of other test tools to evaluate the XPath expressions. Both of them produced the expected results.
I have created a small test program that shows the problem. It is at the end of this post. I have summarized the results in the following table. As you can see, the results arent consistent with the value of the Value attribute.
<table border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none
<tbody>
<tr style="
<td width="152" valign="top" style="width:114.1pt; border:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt
<p style="margin-bottom:.0001pt; line-height:normal XML
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal Context
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <span>XPath
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal Result
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal Expected Result
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name="<span>acctID" Value="00000003a91" />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal translate(string(@Value), 0123456789, )
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red ""(empty string)
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal "a"
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name="<span>acctID" Value="00000003a91" />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal translate("00000003a91", 0123456789, )
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal "a"
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal "a"
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name="<span>acctID" Value="00000003a91" />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal translate(@Value, 0123456789, )
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red ""
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red "a"
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name=<span>acctID Value=00000003a91 />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal string(number(@Value)) = "<span>NaN"
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal true
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal true
</td>
</tr>
<tr style="
<td width="152" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal <Data Name=<span>acctID Value=00000003191 />
</td>
<td width="167" valign="top" style="
<p style="margin-bottom:.0001pt; text-align:center; line-height:normal //Data[@Name="<span>acctID"]
</td>
<td width="169" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal string(number(@Value)) = "<span>NaN"
</td>
<td width="88" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red true
</td>
<td width="61" valign="top" style="
<p style="margin-bottom:.0001pt; line-height:normal; background:red false
</td>
</tr>
</tbody>
</table>
Can someone help me get this running or suggest some alternatives please?
Thanks
Mike Heres my test program. I entered the text from the table above into three textboxes in a small winForms application
<span style="font-size:9.5pt; font-family:"Courier New"
<pre> XPathDocument xPathDocument = new XPathDocument(new StringReader(theXml.Text));
XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
XPathNodeIterator nodes = xPathNavigator.Select(theContext.Text);
XPathExpression query = nodes.Current.Compile(theXPath.Text);
object result = xPathNavigator.Evaluate(query, nodes);
[/code]
I set a breakpoint on the line after the call to Evaluate the expression. It was done this way as I wanted to examine "result" with the Visual Studio debugger.
View the full article