<nodes>
<node>
<nodeFullName>JohnSmith</nodefullname>
<nodeName>John</nodeName>
</node>
<node>
<nodeFullName>JohnSecond</nodefullname>
<nodeName>John</nodeName>
</node>
</nodes>
我想返回与给定名称和姓氏匹配的节点(不区分大小写)
$XMLFILE.SelectNodes("//nodes/node[translate(nodeName,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')=""$Name"" and starts-with(nodeFullName,concat($Name,$LastName) ]")
然而,两个节点都被返回,第二个条件(之后和)没有被检查,我缺少什么?
答案 0 :(得分:2)
在关闭过滤器之前,不要关闭Start-With功能,只关闭concact。该行的结尾应为:
$Name,$LastName))]")
而不是:
$Name,$LastName)]")
事先在PowerShell中加入名字和姓氏可能更简单,而不是使用concact,imho。
$FullName = $Name.tolower() + $LastName.tolower()
然后你的开头可以阅读:
starts-with(translate(nodeFullName,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),""$FullName"")]")
以下是我测试过的完整代码:
$XMLFILE = [XML]@"
<nodes>
<node>
<nodeFullName>JohnSmith</nodeFullName>
<nodeName>John</nodeName>
</node>
<node>
<nodeFullName>JohnSecond</nodeFullName>
<nodeName>John</nodeName>
</node>
</nodes>
"@
$Name = "John"
$LastName = "SMITH"
$FirstName = $Name.ToLower()
$FullName = $Fname + $LastName.ToLower()
$XMLFILE.SelectNodes("//nodes/node[translate(nodeName,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')=""$FName"" and starts-with(translate(nodeFullName,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),""$FullName"")]")
结果导致:
nodeFullName nodeName
------------ --------
JohnSmith John