njack has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I have a problem I just can't find a solution for. I really tried everything so i'm asking you. I have this login script which reads the user xml file looking for a match:

my $digestuser ="dado"; my $digestpass ="cane"; my $file="user.xml"; my $parser=XML::LibXML->new(); my $doc=$parser->parse_file($file); my $root = $doc->getDocumentElement; for my $u ($doc->findnodes('userlist/user')) { print "inside-cycle"; my $property=$u->findnodes('./username')->get_node(1); if(($property->textContent()) eq $digestuser){ print $userok; $property=$u->findnodes('./password')->get_node(1); if(($property->textContent()) eq $digestpass){ $passok=1; print "i did it"; } last; } }

Now the problem is: it works until i link the xml file to it's schema. I need to link it but for some reason it stops working. the xml schema is this

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tecnologie-web.studenti.math.unipd.it/tecweb/~gpinat +o" targetNamespace="http://tecnologie-web.studenti.math.unipd.it/tecw +eb/~gpinato" elementFormDefault="qualified"> <xs:element name="userlist"> <xs:complexType> <xs:sequence> <xs:element name="user" minOccurs="0" maxOccurs="unbounded +"> <xs:complexType> <xs:sequence> <xs:element name="username" type="xs:string" / +> <xs:element name="password" type="xs:string" / +> <xs:element name="email" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

and my file is this

<?xml version="1.0" encoding="UTF-8"?> <userlist xmlns="http://www.progetto.com" xmlns:xs="http://www.w3.org/ +2001/XMLSchema-instance" xs:schemaLocation="http://www.progetto.com u +ser.xsd"> <user> <username>dado</username> <password>cane</password> <email>cane</email> </user></userlist>

with <userlist> it works, as soon as i add this thing here "xmlns="http://tecnologie-web.studenti.math.unipd.it/tecweb/~gpinato"" it stops. I really have lost days on this one but i cant find a solution. Can you PLEASE help me? i really don't know what to do anymore

Replies are listed 'Best First'.
Re: Can't read xml after linking schema
by choroba (Cardinal) on Jan 28, 2014 at 15:59 UTC
    The main problem is that you add not only the link to the schema, but also a namespace to your document. To handle documents containing namespaced elements, you have to use XML::LibXML::XPathContext:
    #!/usr/bin/perl use warnings; use strict; use XML::LibXML; my ($userok, $passok) = (1, 0); my $digestuser = 'dado'; my $digestpass = 'cane'; my $file = 'user.xml'; my $parser = 'XML::LibXML'->new; my $doc = $parser->parse_file($file); my $root = $doc->getDocumentElement; my $xpc = 'XML::LibXML::XPathContext'->new; $xpc->registerNs('p', 'http://www.progetto.com'); for my $u ($xpc->findnodes('p:userlist/p:user', $doc)) { print "inside-cycle"; my ($property) = $xpc->findnodes('p:username', $u); if ($property->textContent eq $digestuser) { print $userok; ($property) = $xpc->findnodes('p:password', $u); if ($property->textContent eq $digestpass) { $passok = 1; print "i did it"; } last } }

    The wrapper XML::XSH2 simplifies the code to

    open user.xml ; my $digestuser = 'dado' ; my $password = 'cane' ; my $passok ; if p:userlist/p:user[p:username=$digestuser and p:password=$digestpass +] $passok = 1 ;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      and what if I remove the line with the namespace? could it cause trouble?

        It can cause serious trouble to any other code reading the file - if the code expected the namespace and it wasn't present, the code wouldn't find any nodes.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      anyway it works and i f*****g love you, i'd buy you a lifetime supply of beer

        No jokes. I am from the Czech Republic, we drink several litres a day!
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ