473,233 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

Simple Xpath question

Ot
I posted this to the wrong group. It went to m.p.dotnet.languages.vb.
Ooops.

--------------------------------------------------------------------

I have this tiny problem. I have learned that an xpath expression can be
bounded by either single or double quotation marks.

But sometimes I want to search for a title containing both a single and
double quote. Any way to do this?

The user enters the title, so now it is in a String variable.

How can I dynamically construct the right Xpath query to find the book with
that title.

<book>
<title>Who's your "Daddy"</title>
The only logic that I can think of is to detect the occurrence of double
quotes in the data and make an iterator that finds the partial title (the
part that doesn't contain the ' " ') and then do an individual match title
by title.
Nov 12 '05 #1
7 5445


Ot wrote:

I have this tiny problem. I have learned that an xpath expression can be
bounded by either single or double quotation marks.

But sometimes I want to search for a title containing both a single and
double quote. Any way to do this?

The user enters the title, so now it is in a String variable.

How can I dynamically construct the right Xpath query to find the book with
that title.

<book>
<title>Who's your "Daddy"</title>
The only logic that I can think of is to detect the occurrence of double
quotes in the data and make an iterator that finds the partial title (the
part that doesn't contain the ' " ') and then do an individual match title
by title.


Here is an example C# program that breaks up a string as needed to
construct an XPath concat function call

using System;
using System.Xml;

public class Test20040125 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(
@"<book><title>Who's your ""Daddy""</title></book>");
string userInput = @"Who's your ""Daddy""";
XmlElement title = xmlDocument.SelectSingleNode("//title[. = " +
buildXPathString(userInput) + "]") as XmlElement;
if (title != null) {
Console.WriteLine(title.OuterXml);
}
else {
Console.WriteLine("No element found.");
}
}

public static string buildXPathString (string input) {
string[] components = input.Split(new char[] { '\''});
string result = "";
result += "concat(''";
for (int i = 0; i < components.Length; i++) {
result += ", '" + components[i] + "'";
if (i < components.Length - 1) {
result += ", \"'\"";
}
}
result += ")";
Console.WriteLine(result);
return result;
}
}
The example constructs
concat('', 'Who', "'", 's your "Daddy"')
as the XPath concat call so you see that the string is split up into
parts not containing a single quote and parts just containing the single
quote.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2
Ot
"Martin Honnen" <Ma***********@t-online.de> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...

Here is an example C# program that breaks up a string as needed to
construct an XPath concat function call

using System;
using System.Xml;

public class Test20040125 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(
@"<book><title>Who's your ""Daddy""</title></book>");
string userInput = @"Who's your ""Daddy""";
XmlElement title = xmlDocument.SelectSingleNode("//title[. = " +
buildXPathString(userInput) + "]") as XmlElement;
if (title != null) {
Console.WriteLine(title.OuterXml);
}
else {
Console.WriteLine("No element found.");
}
}

public static string buildXPathString (string input) {
string[] components = input.Split(new char[] { '\''});
string result = "";
result += "concat(''";
for (int i = 0; i < components.Length; i++) {
result += ", '" + components[i] + "'";
if (i < components.Length - 1) {
result += ", \"'\"";
}
}
result += ")";
Console.WriteLine(result);
return result;
}
}


The example constructs
concat('', 'Who', "'", 's your "Daddy"')
as the XPath concat call so you see that the string is split up into
parts not containing a single quote and parts just containing the single
quote.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Thanks for your input. I did my best to reconstruct what you wrote in an equivalent VB.NET program. (I made it react to a button-click, but it is really no different.) Thing is, it gets an exception. Can you figure out why? I'm really confused here.

\\\
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xmlDocument As XmlDocument = New XmlDocument
xmlDocument.LoadXml("<book><title>Who's your ""Daddy""</title></book>")
Dim sw As IO.StreamWriter = New System.IO.StreamWriter("C:\testbook.xml")
Dim xm As System.Xml.XmlTextWriter = New System.Xml.XmlTextWriter(sw)
xm.Formatting = Formatting.Indented
xm.Indentation = 2
xm.IndentChar = " "
xmlDocument.Save(xm)
Dim userInput As String = "Who's your ""Daddy"""
Dim query As String = "//title[. = '" & buildXpathString(userInput) & "']"
Console.WriteLine("query={" & query & "}")
Dim title As XmlElement = _
xmlDocument.SelectSingleNode(query)
If title Is Nothing Then
Console.WriteLine("No Element found.")
Else
Console.WriteLine("Title=" & title.OuterXml)
End If
End Sub
Function buildXpathString(ByVal input As String) As String
Dim components() As String = input.Split("'"c)
Dim result As String = ""
For i As Integer = 0 To components.Length - 2
result += components(i) & "''"
Next
result += components(components.Length - 1)
Console.WriteLine("result={" & result & "}")
Return result
End Function
///

has as output

\\\
result={Who''s your "Daddy"}
query={//title[. = 'Who''s your "Daddy"']}
An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in system.xml.dll
Additional information: System error.
Unhandled Exception: System.Xml.XPath.XPathException: '//title[. = 'Who''s your "Daddy"']' has an invalid token.
///

The contents of testbook.xml file is

\\\
<?xml version="1.0" encoding="utf-8"?>
<book>
<title>Who's your "Daddy"</title>
</book>
///
Nov 12 '05 #3
Ot

"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...


Ot wrote:

I have this tiny problem. I have learned that an xpath expression can be bounded by either single or double quotation marks.

But sometimes I want to search for a title containing both a single and
double quote. Any way to do this?

The user enters the title, so now it is in a String variable.

How can I dynamically construct the right Xpath query to find the book with that title.

<book>
<title>Who's your "Daddy"</title>
The only logic that I can think of is to detect the occurrence of double quotes in the data and make an iterator that finds the partial title (the part that doesn't contain the ' " ') and then do an individual match title by title.


Here is an example C# program that breaks up a string as needed to
construct an XPath concat function call

using System;
using System.Xml;

public class Test20040125 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(
@"<book><title>Who's your ""Daddy""</title></book>");
string userInput = @"Who's your ""Daddy""";
XmlElement title = xmlDocument.SelectSingleNode("//title[. = " +
buildXPathString(userInput) + "]") as XmlElement;
if (title != null) {
Console.WriteLine(title.OuterXml);
}
else {
Console.WriteLine("No element found.");
}
}

public static string buildXPathString (string input) {
string[] components = input.Split(new char[] { '\''});
string result = "";
result += "concat(''";
for (int i = 0; i < components.Length; i++) {
result += ", '" + components[i] + "'";
if (i < components.Length - 1) {
result += ", \"'\"";
}
}
result += ")";
Console.WriteLine(result);
return result;
}
}
The example constructs
concat('', 'Who', "'", 's your "Daddy"')
as the XPath concat call so you see that the string is split up into
parts not containing a single quote and parts just containing the single
quote.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Thanks for your input. I did my best to reconstruct what you wrote in an
equivalent VB.NET program. (I made it react to a button-click, but it is
really no different.) Thing is, it gets an exception. Can you figure out
why? I'm really confused here.

\\\
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim xmlDocument As XmlDocument = New XmlDocument
xmlDocument.LoadXml("<book><title>Who's your
""Daddy""</title></book>")
Dim sw As IO.StreamWriter = New
System.IO.StreamWriter("C:\testbook.xml")
Dim xm As System.Xml.XmlTextWriter = New
System.Xml.XmlTextWriter(sw)
xm.Formatting = Formatting.Indented
xm.Indentation = 2
xm.IndentChar = " "
xmlDocument.Save(xm)
Dim userInput As String = "Who's your ""Daddy"""
Dim query As String = "//title[. = '" & buildXpathString(userInput)
& "']"
Console.WriteLine("query={" & query & "}")
Dim title As XmlElement = _
xmlDocument.SelectSingleNode(query)
If title Is Nothing Then
Console.WriteLine("No Element found.")
Else
Console.WriteLine("Title=" & title.OuterXml)
End If
End Sub
Function buildXpathString(ByVal input As String) As String
Dim components() As String = input.Split("'"c)
Dim result As String = ""
For i As Integer = 0 To components.Length - 2
result += components(i) & "''"
Next
result += components(components.Length - 1)
Console.WriteLine("result={" & result & "}")
Return result
End Function
///

has as output

\\\
result={Who''s your "Daddy"}
query={//title[. = 'Who''s your "Daddy"']}
An unhandled exception of type 'System.Xml.XPath.XPathException' occurred
in system.xml.dll
Additional information: System error.
Unhandled Exception: System.Xml.XPath.XPathException: '//title[. = 'Who''s
your "Daddy"']' has an invalid token.
///

The contents of testbook.xml file is

\\\
<?xml version="1.0" encoding="utf-8"?>
<book>
<title>Who's your "Daddy"</title>
</book>
///
Nov 12 '05 #4


Ot wrote:
"Martin Honnen" <Ma***********@t-online.de> wrote in message
Here is an example C# program that breaks up a string as needed to
construct an XPath concat function call

using System;
using System.Xml;

public class Test20040125 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(
@"<book><title>Who's your ""Daddy""</title></book>");
string userInput = @"Who's your ""Daddy""";
XmlElement title = xmlDocument.SelectSingleNode("//title[. = " +
buildXPathString(userInput) + "]") as XmlElement;
if (title != null) {
Console.WriteLine(title.OuterXml);
}
else {
Console.WriteLine("No element found.");
}
}

public static string buildXPathString (string input) {
string[] components = input.Split(new char[] { '\''});
string result = "";
result += "concat(''";
for (int i = 0; i < components.Length; i++) {
result += ", '" + components[i] + "'";
if (i < components.Length - 1) {
result += ", \"'\"";
}
}
result += ")";
Console.WriteLine(result);
return result;
}
}
The example constructs
concat('', 'Who', "'", 's your "Daddy"')
as the XPath concat call so you see that the string is split up into
parts not containing a single quote and parts just containing the single
quote.


Thanks for your input. I did my best to reconstruct what you wrote in an
equivalent VB.NET program. (I made it react to a button-click, but it is
really no different.) Thing is, it gets an exception. Can you figure out
why? I'm really confused here.
Function buildXpathString(ByVal input As String) As String
Dim components() As String = input.Split("'"c)
Dim result As String = ""
For i As Integer = 0 To components.Length - 2
result += components(i) & "''"
Next
result += components(components.Length - 1)
Console.WriteLine("result={" & result & "}")
Return result
End Function


The suggested solution is to construct some XPath expression with a
concat(...)
function call which the C# method above does while your VB.NET method
doesn't do that.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #5
Ot

"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:ee**************@TK2MSFTNGP12.phx.gbl...
The suggested solution is to construct some XPath expression with a

concat(...)
function call which the C# method above does while your VB.NET method
doesn't do that.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Forgive my confusion, but isn't a string a string regardless of how it is
constructed? Isn't this an Xpath issue now, not a programming issue?
Doesn't your C# program eventually issue the same Xpath query? It seems to
me it does, hence my confusion.

As you can see from my quoted output I get

query={//title[. = 'Who''s your "Daddy"']}

(the '{' and '}' I put in just to show the extent of the string, they are
not part of the string itself.)

then I do

Dim title As XmlElement = _
xmlDocument.SelectSingleNode(query)

and the error occurs.

Thanks for taking your time to help me with this, it is appreciated.
Nov 12 '05 #6


Ot wrote:
"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:ee**************@TK2MSFTNGP12.phx.gbl...
The suggested solution is to construct some XPath expression with a
concat(...)
function call which the C# method above does while your VB.NET method
doesn't do that.


Forgive my confusion, but isn't a string a string regardless of how it is
constructed? Isn't this an Xpath issue now, not a programming issue?
Doesn't your C# program eventually issue the same Xpath query? It seems to
me it does, hence my confusion.

As you can see from my quoted output I get

query={//title[. = 'Who''s your "Daddy"']}


Yes, but that doesn't work, you need to construct the XPath expression
//title[. = concat('Who', "'", 's your "Daddy"')]
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #7
Ot

"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...


Ot wrote:
"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:ee**************@TK2MSFTNGP12.phx.gbl...
The suggested solution is to construct some XPath expression with a

concat(...)
function call which the C# method above does while your VB.NET method
doesn't do that.

Forgive my confusion, but isn't a string a string regardless of how it

is constructed? Isn't this an Xpath issue now, not a programming issue?
Doesn't your C# program eventually issue the same Xpath query? It seems to me it does, hence my confusion.

As you can see from my quoted output I get

query={//title[. = 'Who''s your "Daddy"']}


Yes, but that doesn't work, you need to construct the XPath expression
//title[. = concat('Who', "'", 's your "Daddy"')]
--

Martin Honnen
http://JavaScript.FAQTs.com/


Ah, dawns the light!

Thanks for your help!
Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
9
by: Jon Thackray | last post by:
I'm trying to use XPath to enable me to number items within sections in a paper. I have so far tried about ten things, all of which it seemed to me should work, and none of which do. Essentially...
3
by: Kathy Burke | last post by:
Hi again, I'm using the following xpath (works in visualizer) with a SelectSingleNode("xpath") statement. //Station/(WI])]/@order Problem is I get an error "expression passed to this method...
6
by: Tommy | last post by:
For a XML fragment like below <ns0:BusCar_Request xmlns:ns0="http://BTSDG_SQL"> <ns0:sync> <ns0:after> <ns0:BusinessCards NameOnCard="NameOnCard_1" TitleOnCard="TitleOnCard_1" Quantity="10" />...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.