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

"/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.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^6: language selection via libxml
by tangent (Parson) on Jan 20, 2014 at 13:21 UTC
    I am trying to get this as I believe Xpath expressions to be very powerful and I've always had a hard time understanding them.

    The two examples marked OK above seem to behave the same way:

    use XML::LibXML; my $tree = XML::LibXML->load_xml(string => <<'EOT'); <?xml version="1.0"?> <language> <english> <widget ID="1">input</widget> <widget ID="2">output</widget> </english> <deutsch> <widget ID="1">eingabe</widget> <widget ID="2">ausgabe</widget> </deutsch> </language> EOT test('english',1); test('deutsch',1); sub test { my ($language,$id) = @_; my @result=$tree->findnodes("/language/$language/widget[\@ID=$id]" +); print $_->textContent . "\n" for @result; my @result2=$tree->findnodes("/language/$language/widget[\@ID='$id +']"); print $_->textContent . "\n" for @result2; }
    Output:
    input input eingabe eingabe
      OK, numbers do not have to be enclosed in quotes, and a valid identifier cannot start with a digit. Try using "a" and "b" as the identifiers, though.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        I see. So when I changed the ID values to 'a' and 'b' the only one that worked was the one with single quotes:
        "/language/$language/widget[\@ID='$id']"
        I'm not quite sure what you mean by "a valid identifier cannot start with a digit" - what does 'identifier' refer to?