here sample code:
$xml = new-object xml $xml.load('c:\users\example\desktop\examp.xml') # works $node = $xml.selectsinglenode("//server[@name='example']") # not work $t = 'example' $node = $xml.selectsinglenode("//server[@name=$t]") why? if see xml well, please so.
strings in xpath expressions must enclosed in quotes (either single or double). @mathiasr.jessen pointed out in comment question, expression evaluates //server[@name=example] when should //server[@name="example"] or //server[@name='example']. quotes in expression
$t = 'example' are defining example string for powershell. don't become part of string , not present when expanding variable in string xpath expression.
demonstration:
ps c:\> $t = 'example' ps c:\> $t example ps c:\> "//server[@name=$t]" # wrong //server[@name=example] ps c:\> "//server[@name='$t']" # correct //server[@name='example'] ps c:\> "//server[@name=`"$t`"]" # correct //server[@name="example"] add missing quotes xpath expression , problem disappear:
$node = $xml.selectsinglenode("//server[@name='$t']")
Comments
Post a Comment