亲爱的Powershell伙计们
我有一个(也许)非常简单的问题,但我不知道如何解决。我想在XPath表示法中使用包含单引号的字符串变量。我使用CMDlet选择-Xml。如果字符串中没有单引号,则Select完全正常工作。但是,如果有一个单引号(例如:不要),它会使我的脚本崩溃。让我给你详细介绍一下。
问题
$Name = "Dont display" ##is working completly fine
(Select-Xml -Path "C:\SomePath\Somexml.xml" -XPath "//q1:Options[q1:Name = '$Name']" -Namespace $namespace).Node.InnerText ##is working completly fine$Name = "Don't display" ##crashes script
(Select-Xml -Path "C:\SomePath\Somexml.xml" -XPath "//q1:Options[q1:Name = '$Name']" -Namespace $namespace).Node.InnerText ##crashes scriptpowershell的错误输出是:
Select-Xml : '//q1:Options[q1:Name = 'Don't display']' has an invalid token.
At line:251 char:41
+ ... ing_Name = (Select-Xml -Path "C:\SomePath\Somexml.xml" -XPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Select-Xml], XPathException
+ FullyQualifiedErrorId : System.Xml.XPath.XPathException,Microsoft.PowerShell.Commands.SelectXmlCommand到目前为止我尝试了什么,
当然,我尝试过不同的引语,例如:
$Name = """Don't display"""
$Name = '"Don't display"'
$Name = "'Don't display'"
$Name = "'Don't display'"
$Name = 'Don't display'
$Name = ''Don't display''看起来,powershell引用规则和XPath表示法可能存在问题。
但也许你们中的任何人都有办法解决这个问题。
非常感谢
发布于 2022-09-01 11:05:38
在XPath字符串文本中似乎没有转义字符,因此不能在文字字符串中使用字符串分隔符,因为它终止字符串--即:
$name = "Don't display"
# single quotes:
# //q1:Options[q1:Name = 'Don't display']
# ^ terminates string literal解决具体问题的一个快速(但却天真)的解决方案是,只使用双引号作为分隔符:
$name = "Don't display"
# double quotes:
# //q1:Options[q1:Name = "Don't display"]
# ^ *doesn't* terminate string literal但是,如果您的数据包含双引号呢?然后你又回到了起点..。
$name = "Don""t display"
# double quotes:
# //q1:Options[q1:Name = "Don"t display"]
# ^ terminates string literal again在病态的情况下,如果您的文字包含单引号和双引号,则不能将它们用作分隔符:
$name = "Don't d""isplay"
# single quotes:
# //q1:Options[q1:Name = 'Don't d"isplay']
# ^ terminates string literal
# double quotes:
# //q1:Options[q1:Name = "Don't d"isplay"]
# ^ also terminates string literal在这种情况下,您可以求助于this答案,这意味着将字符串文本转换为concat表达式,这样您就可以得到:
$name = "Don't d""isplay"
# //q1:Options[q1:Name = concat('Don', "'", 't d"isplay')]
# ^^^^^ use single quotes
# ^^^ use double quotes
# ^^^^^^^^^^^^ use single quotes您可以通过以下方式生成:
$name = "Don't d""isplay"
$squote = "', `"'`", '"
$expr = "concat('{0}')" -f $name.Replace("'", $squote)
# Select-Xml -Xml $xml -XPath "//q1:Options[q1:Name = $expr]"
# ->
# //q1:Options[q1:Name = concat('Don', "'", 't d"isplay')]然后,包含双引号的数据部分用单引号分隔,反之亦然,因此它们都会正确终止。
注意-您可能会对没有一种类型的引号或另一种引用的文字和连续的单引号进行优化,并且需要为$null和其他边缘情况添加一些错误处理,但它基本上可以完成这项工作.
更新
这里有一个完整的代码示例来展示它的作用..。
$xml = [xml] "<root><child Name=`"my'name`" /></root>"
$name = "my'name"
$squote = "', `"'`", '"
$expr = "concat('{0}')" -f $name.Replace("'", $squote)
Select-Xml -Xml $xml -XPath "//child[@Name = $expr]"
# Node Path Pattern
# ---- ---- -------
# child InputStream //child[@Name = concat('my', "'", 'name')]https://stackoverflow.com/questions/73566204
复制相似问题