 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 28th, 2007, 10:34 AM
|
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
XPath Invalid token
I Have an issue with XPATH and im looking for some help
Here is my code behind on my aspx page
XmlNodeList codelist = bankdoc.SelectNodes("//bank[text() = '" + GetXPathString(strBank) + "'] and ..branch[text()= '" + GetXPathString(strBranch) + "'] ");
GetXPathString is a fxn to concat the 2 string, here it is
public static string GetXPathString(string input)
{
string [] fragments = input.Split(new char[] {'\''});
string result = "";
result += "concat(''";
for (int i = 0; i < fragments.Length; i++)
{
result += ", '" + fragments[i] + "'";
if (i < fragments.Length - 1)
{
result += ", \"'\"";
}
}
result += ")";
return result;
}
However when I run I get the error;
'//bank[text() = 'concat('', 'XXX rrrreee')'] and ..branch[text()= 'concat('', 'xxx wwwwol')'] ' has an invalid token.
Exception Details: System.Xml.XPath.XPathException: '//bank[text() = 'concat('', 'XXX rrrreee')'] and ..branch[text()= 'concat('', 'XXX rrrreee')'] ' has an invalid token.
any ideas?
__________________
Thank You
|
|

November 28th, 2007, 10:38 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
I have not checked the complete code but you can't have ..branch, that needs to be ../branch
|
|

November 28th, 2007, 10:43 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
concat is a function, so shouldn't be inside single quotes
Wrong: text()='concat('a','b')'
Correct: text()=concat('a', 'b')
/- Sam Judson : Wrox Technical Editor -/
|
|

November 29th, 2007, 06:28 AM
|
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey , ive removed the single quotes from around the concat, thanks, I am still receining this error thou:
'//bank[text() = concat('', 'Clears through Bank nameâ)] and ..branch[text()= concat('', 'london uk')] ' has an invalid token.
Here is the code now:
XmlNodeList codelist = bankdoc.SelectNodes("//bank[text() = " + GetXPathString(strBank) + "] & ..branch[text()= " + GetXPathString(strBranch) + "] ");
Also have tried
XmlNodeList codelist = bankdoc.SelectNodes("//bank[text() = " + GetXPathString(strBank) + "] and ..branch[text()= " + GetXPathString(strBranch) + "] ");
When Split this code i.ie
XmlNodeList codelist = bankdoc.SelectNodes("//bank[text()= " + GetXPathString(strBank) + "] ");
OR
XmlNodeList codelist = bankdoc.SelectNodes("//branch[text()= " + GetXPathString(strBranch) + "] ");
It works fine, it ojnly gives me the erroer when I want to query 2 nodes, so i must doing something wrong in the âANDâ part , any ideas, im lost.
Here is my XML:
basically I am trying to select the <nsc> element, from the values of 2 drop down menus, the first selects <bank> (itâs a distinct list of banks) and the seconds selects <branch> and list of all branchs for the bank selected , I want to print the <nsc> value to a text box.
<Bank>
<nsc>000000</nsc>
<bank>Clears through bank name</bank>
<branch>london uk</branch>
<address1>The Square</address1>
<address2> </address2>
<address3> </address3>
<keywords> </keywords>
<telephone></telephone>
<fax></fax>
<atm>Yes</atm>
</Bank>
Hope this makes sense
|
|

November 29th, 2007, 06:59 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Since the bug was in your GetXPathString() method, and you haven't shown the new version of the method, one must assume that you haven't corrected it successfully.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 29th, 2007, 07:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
It would be easier to debug if we could see the actual XPath generated.
--
Joe ( Microsoft MVP - XML)
|
|

November 29th, 2007, 08:37 AM
|
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi
sorry, here is my method
public static string GetXPathString(string input)
{
string [] fragments = input.Split(new char[] {'\''});
string result = "";
result += "concat(''";
for (int i = 0; i < fragments.Length; i++)
{
result += ", '" + fragments[i] + "'";
if (i < fragments.Length - 1)
{
result += ", \"'\"";
}
}
result += ")";
return result;
}
here is the XPATH :
'//bank[text() = concat('', 'Clears through Bank name')] and ..branch[text()= concat('', 'Bank aa')] '
|
|

November 29th, 2007, 08:48 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I think someone already pointed out that
..branch
is not valid, you presumably intended ../branch
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 29th, 2007, 09:01 AM
|
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i tried that, my code/xpathIs now:
XmlNodeList codelist = bankdoc.SelectNodes("//bank[text() = " + GetXPathString(strBank) + "] and ../branch[text()= " + GetXPathString(strBranch) + "] ");
when i run i get this eror:
Expression must evaluate to a node-set.
|
|

November 29th, 2007, 09:37 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Basically you're xpath looks like this:
//bank[text()='A'] and ../branch[text()='B']
The problem is that the 'and' is not inside a predicate (i.e. square brackets []).
What are you trying to return? bank elements, or branch elements?
Can we see a segment of your input XML? Is branch the parent of bank, or vice versa?
/- Sam Judson : Wrox Technical Editor -/
|
|
 |