473,403 Members | 2,366 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,403 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 5453


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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.