in reply to Re: language selection via libxml
in thread language selection via libxml

Also need to lose the quotes around $id
"/language/$language/widget[\@ID=$id]"
Update:
Thanks to choroba's explanations and examples below, this only works by accident, i.e. only if $id is a number.

Here are some different ways of doing it:

"/language/$language/widget[\@ID='$id']" "/language/$language/widget[attribute::ID='$id']" qq|/language/$language/widget[\@ID="$id"]| qq|/language/$language/widget[\@ID='$id']| qq|/language/$language/widget[attribute::ID="$id"]| qq|/language/$language/widget[attribute::ID='$id']|

Replies are listed 'Best First'.
Re^3: language selection via libxml
by choroba (Cardinal) on Jan 20, 2014 at 12:29 UTC
    Not really. String literals must be enclosed in quotes in XPath expressions, and XPath does not know Perl variables (unless you use XML::XSH2).
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      With the quotes I get
      Scalar found where operator expected near ""/language/$language/widget +[\@ID="$id" String found where operator expected near "$id"]"" syntax error near ""/language/$language/widget[\@ID="$id"
      Update: Perhaps I should have said "lose double quotes around $id", though no quotes works for me too:
      "/language/$language/widget[\@ID="$id"]" - Error "/language/$language/widget[\@ID='$id']" - OK "/language/$language/widget[\@ID=$id]" - OK
        "/language/$language/widget[\@ID=$id]" - OK
        Well, it is a valid syntax, but the meaning is different: it tries to find a widget whose ID attribute has the same value as the text of the child of widget whose name is $id. Barewords are not promoted to strings in XPath.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        You need to escape the quotes as well or use single quotes around your string:

        $result=$tree->findnodes('/language/$language/widget[@ID="$id"]');