gpt4 book ai didi

c# - XPath 查询中的撇号 (')

转载 作者:IT老高 更新时间:2023-10-28 20:46:27 25 4
gpt4 key购买 nike

我使用以下 XPATH 查询 列出站点下的对象。 ListObject[@Title='SomeValue']。 SomeValue 是动态的。只要 SomeValue 没有撇号 ('),此查询就有效。也尝试使用转义序列。没用。

我做错了什么?

最佳答案

这很难做到。

看看XPath Recommendation ,你会看到它定义了一个文字:

Literal ::=   '"' [^"]* '"' 
| "'" [^']* "'"

也就是说,XPath 表达式中的字符串文字可以包含撇号或双引号,但不能同时包含两者。

你不能使用转义来解决这个问题。像这样的文字:

'Some'Value'

将匹配此 XML 文本:

Some'Value

这确实意味着可能有一段 XML 文本无法生成 XPath 文字来匹配,例如:

<elm att="&quot;&apos"/>

但这并不意味着不可能用 XPath 匹配该文本,它只是很棘手。在您尝试匹配的值包含单引号和双引号的任何情况下,您都可以构造一个表达式,使用 concat 来生成它将匹配的文本:

elm[@att=concat('"', "'")]

所以这导致我们这样做,这比我想要的要复杂得多:

/// <summary>
/// Produce an XPath literal equal to the value if possible; if not, produce
/// an XPath expression that will match the value.
///
/// Note that this function will produce very long XPath expressions if a value
/// contains a long run of double quotes.
/// </summary>
/// <param name="value">The value to match.</param>
/// <returns>If the value contains only single or double quotes, an XPath
/// literal equal to the value. If it contains both, an XPath expression,
/// using concat(), that evaluates to the value.</returns>
static string XPathLiteral(string value)
{
// if the value contains only single or double quotes, construct
// an XPath literal
if (!value.Contains("\""))
{
return "\"" + value + "\"";
}
if (!value.Contains("'"))
{
return "'" + value + "'";
}

// if the value contains both single and double quotes, construct an
// expression that concatenates all non-double-quote substrings with
// the quotes, e.g.:
//
// concat("foo", '"', "bar")
StringBuilder sb = new StringBuilder();
sb.Append("concat(");
string[] substrings = value.Split('\"');
for (int i = 0; i < substrings.Length; i++ )
{
bool needComma = (i>0);
if (substrings[i] != "")
{
if (i > 0)
{
sb.Append(", ");
}
sb.Append("\"");
sb.Append(substrings[i]);
sb.Append("\"");
needComma = true;
}
if (i < substrings.Length - 1)
{
if (needComma)
{
sb.Append(", ");
}
sb.Append("'\"'");
}

}
sb.Append(")");
return sb.ToString();
}

是的,我在所有边缘情况下都对其进行了测试。这就是为什么逻辑如此复杂的原因:

    foreach (string s in new[]
{
"foo", // no quotes
"\"foo", // double quotes only
"'foo", // single quotes only
"'foo\"bar", // both; double quotes in mid-string
"'foo\"bar\"baz", // multiple double quotes in mid-string
"'foo\"", // string ends with double quotes
"'foo\"\"", // string ends with run of double quotes
"\"'foo", // string begins with double quotes
"\"\"'foo", // string begins with run of double quotes
"'foo\"\"bar" // run of double quotes in mid-string
})
{
Console.Write(s);
Console.Write(" = ");
Console.WriteLine(XPathLiteral(s));
XmlElement elm = d.CreateElement("test");
d.DocumentElement.AppendChild(elm);
elm.SetAttribute("value", s);

string xpath = "/root/test[@value = " + XPathLiteral(s) + "]";
if (d.SelectSingleNode(xpath) == elm)
{
Console.WriteLine("OK");
}
else
{
Console.WriteLine("Should have found a match for {0}, and didn't.", s);
}
}
Console.ReadKey();
}

关于c# - XPath 查询中的撇号 ('),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1341847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com