468,771 Members | 1,930 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

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 5256


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Joshua Beall | last post: by
9 posts views Thread by Jon Thackray | last post: by
9 posts views Thread by David Thielen | last post: by
5 posts views Thread by Gnic | last post: by
6 posts views Thread by Armel Asselin | last post: by
3 posts views Thread by Jason Mobarak | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.