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