XPath lowercase() 函数
我使用 XPATH 从 XML 文档中选择某些节点。
用户能够插入该位置的值。它工作正常,但如果使用不同的情况,它就不起作用。
我决定在比较之前将 XML 值和用户输入更改为小写可能是最好的方法。
我目前已将此作为我的选择器:
NodeIter = nav.Select("/Houses/House/location[contains(../location, '" + location_input + "')]");
我尝试将 lower-case()
函数放在不同的位置,但它对此不满意。
如何才能将 ../location
的值作为小写进行比较?
注意:在我的 C# 代码中使用 ToLower()
将 location_input 设置为 lower。
I'm using XPATH to select certain nodes from an XML document.
The user is able to insert a value for the location. It's working fine, but it does not work if different cases are used.
I've decided that changing both the XML values and the user's input to lower case before being compared is probably the best way to go about it.
I've got this as my selector at the moment:
NodeIter = nav.Select("/Houses/House/location[contains(../location, '" + location_input + "')]");
I've tried putting the lower-case()
function in various locations, but it isn't happy with it.
How do I make it so that the value of ../location
is compared as lower case?
Note: location_input is set to lower using ToLower()
within my c# code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
lower-case() 函数仅从 XPath 2.0 开始受支持。如果您的环境支持此版本的标准,您可以编写:
但是,您很可能会陷入 XPath 1.0 的困境。在这种情况下,您可以滥用 translate() 函数:
The lower-case() function is only supported from XPath 2.0 onwards. If your environment supports this version of the standard, you can write:
However, chances are you're stuck with XPath 1.0. In that case, you can abuse the translate() function:
translate(../location, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
如果你可以只使用 AZtranslate(../location, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
if you can get away with just A-Z小写
http://www. w3.org/TR/xpath-functions/#func-lower-case 是 XPath 2.0 和 XQuery 1.0 的一部分,因此您需要使用 XPath 2.0 或 XQuery 1.0 实现,例如 XQSharp或者如果您想使用此类功能,请像 .NET 版本的 Saxon 9 一样。使用 XPath 1.0,您所能做的就是
NodeIter = nav.Select(string.Format("/Houses/House/location[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXZY', 'abcdefghijklmnopqrstuvwxyz'), '{0}') ]", location_input));
。lower-case
http://www.w3.org/TR/xpath-functions/#func-lower-case is part of XPath 2.0 and XQuery 1.0 so you need to use an XPath 2.0 or XQuery 1.0 implementation like XQSharp or like the .NET version of Saxon 9 if you want to use such functions.With XPath 1.0 all you can do is
NodeIter = nav.Select(string.Format("/Houses/House/location[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXZY', 'abcdefghijklmnopqrstuvwxyz'), '{0}')]", location_input));
.请注意,严格来说,将两个字符串转换为小写(或大写)并不是进行大小写盲比较的正确方法,因为 Unicode 中小写到大写字符的映射不是一对一的。原则上,在 XPath 2.0 中,您应该使用不区分大小写的排序规则。但不幸的是,尽管许多 XSLT 2.0 和 XQuery 1.0 处理器允许您使用区分大小写的排序规则,但没有排序规则 URI 的标准,因此您的代码变得依赖于处理器。
Note that strictly speaking, translating two strings to lower (or upper) case is not a correct way to do a case-blind comparison, because the mapping of lower-case to upper-case characters in Unicode is not one-to-one. In principle, in XPath 2.0 you should use a case-blind collation. Unfortunately though, although many XSLT 2.0 and XQuery 1.0 processors allow you to use a case-blind collation, there are no standards for collation URIs, so your code becomes processor-dependent.
只要您处理 .net,您就可以使用 Microsoft 扩展进行不区分大小写的比较:ms:string-compare
https://msdn.microsoft.com/en-us/library/ms256114(v=vs.120).aspx
As long as you are dealing with .net, you can use a Microsoft extension to do a case-insensitive comparison: ms:string-compare
https://msdn.microsoft.com/en-us/library/ms256114(v=vs.120).aspx
我在使用 VS2017(NetFramework 4.6.1) 时遇到了同样的困境,并安装了 XPath2 NuGet 包。到目前为止,在使用 XPath2 函数时,它对我来说工作得很好。
I had same dilemma using VS2017(NetFramework 4.6.1) and installed the XPath2 NuGet package. So far it has been worked fine for me when using XPath2 functions.