VSP*_*VSP 2 c# xml xpath case-insensitive selectsinglenode
我有一个类似问题,即XPath忽略大小写的SelectNodes问题,但在我的情况下,大写/小写问题是在名称为“ application”的节点中(有时是“ Application”,有时是“ application”)。
我将如何应用其他帖子的解决方案?还是在这种情况下适用另一种?
xml:
<?xml version="1.0" encoding="utf-16" ?>
<application>
<forms>
<action type="update">
<form uid="" >
</form>
</action>
</forms>
</application>
Run Code Online (Sandbox Code Playgroud)
在C#3.5中:
XmlNode nodex= oXMLDoc1.SelectSingleNode("Application/forms/action/form/@uid")
nodex.Value="UniqueIDx";//nodex is null :S
Run Code Online (Sandbox Code Playgroud)
只需使用:
*[translate(name(), 'APPLICATION', 'application')='application']
/forms/action/form/@uid
Run Code Online (Sandbox Code Playgroud)
当当前(初始上下文)节点有一个带有名称的子节点时,这将在所有情况下正确选择想要的属性,即字符串“application”的任何大写。
基于 XSLT 的验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select=
"*[translate(name(), 'APPLICATION', 'application')='application']
/forms/action/form/@uid"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于以下 XML 文档时:
<aPPliCatioN>
<forms>
<action>
<form uid="xyz"/>
</action>
</forms>
</aPPliCatioN>
Run Code Online (Sandbox Code Playgroud)
选择想要的节点并将其字符串值复制到输出:
xyz
Run Code Online (Sandbox Code Playgroud)
说明:
正确使用标准 XPath 函数name()和translate().
我们可以将xml及其变量转换为小写。
string value = "aBc";
XmlNode xmlnode = xmldoc.SelectSingleNode(string.Format("/some/path/add[translate(@key, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = '{0}']", value.ToLower()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17286 次 |
| 最近记录: |