p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   XPath Invalid token (http://p2p.wrox.com/showthread.php?t=64143)

Hughesie78 November 28th, 2007 10:34 AM

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?

Martin Honnen November 28th, 2007 10:38 AM

I have not checked the complete code but you can't have ..branch, that needs to be ../branch


samjudson November 28th, 2007 10:43 AM

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 -/

Hughesie78 November 29th, 2007 06:28 AM

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

mhkay November 29th, 2007 06:59 AM

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

joefawcett November 29th, 2007 07:51 AM

It would be easier to debug if we could see the actual XPath generated.

--

Joe (Microsoft MVP - XML)

Hughesie78 November 29th, 2007 08:37 AM

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')] '

mhkay November 29th, 2007 08:48 AM

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

Hughesie78 November 29th, 2007 09:01 AM

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.

samjudson November 29th, 2007 09:37 AM

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 -/

Hughesie78 November 29th, 2007 09:50 AM

hey,
here is my XML:
<Banks>
  <Bank>
    <nsc>000000</nsc>
    <bank>Clears through Bank name</bank>
    <branch>london uk</branch>
  </Bank>
  <Bank>
    <nsc>111111</nsc>
    <bank>Clears through Bank name</bank>
    <branch>Dublin Irl</branch>
  </Bank>
  <Bank>
    <nsc>000000</nsc>
    <bank>Clears through Bank name</bank>
    <branch>Manc uk</branch>
  </Bank>
  <Bank>
    <nsc>111111</nsc>
    <bank>Bank two</bank>
    <branch>Dublin Irl</branch>
  </Bank>

</Banks>

so I have drop down with a lis distinct bank, once they select the bank a 2nd drop down shows list of distinct branch.
once they select the branch, i was to display the <nsc> value.

<Banks> is root node
<Bank> is parent
<bank> and <nsc> are siblings .. you can correct me there im relatively new to XML.

samjudson November 29th, 2007 09:56 AM

The following xpath should get you the nsc element:

/Banks/Bank/nsc[../bank='A' and ../branch='B']

/- Sam Judson : Wrox Technical Editor -/

mhkay November 29th, 2007 09:58 AM

Well yes, you would expect an expression passed to SelectNodes() to evaluate to a node-set, and an expression that uses the "and" operator will evaluate to a boolean. So what result were you expecting for your expression?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Hughesie78 November 29th, 2007 12:59 PM

hey, thanks it worked perfect!!


All times are GMT -4. The time now is 10:23 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.