in reply to XML::LibXML problem

Instead of 3, use the exported constant:
if ($node->nodeType == XML_TEXT_NODE) {
"Text node" is not an element containing text. It is an abstract node in the XML DOM structure that corresponds to the text itself. Moreover, //* only matches elements, not text nodes. To match text nodes, use //text().

Replies are listed 'Best First'.
Re^2: XML::LibXML problem
by norricorp (Initiate) on Dec 29, 2011 at 14:11 UTC
    Hi, thanks for the reply. The problem is I want both elements and text nodes. But having said that, the bigger problem I am having is just geting a single child level of elements. This particular schema is literally dozens of levels deep but I only want the children under a particular element. So far, this query produces output (but all of the elements beneath the starting element) (I just want a single layer)
    $query = $root->nodePath() . $extendPath . "[\@name]";
    but this query
    $query = "child::" . $root->nodePath() . $extendPath . "[\@name]";
    produces an error of (please excuse the path - it is the schema, not me!) XPath error : Invalid expression child::/xs:schema/xs:element/xs:complexType/xs:sequence/xs:element/xs:complexType/xs:sequence/xs:element@name ^ at dump2.pl line 85 Any ideas? Regards, John
      I do not get your exact problem. To get children elements of a particular parent element, just do
      $parent->findnodes("*")
      To get both elements and text nodes, do
      $parent->findnodes("* | text()")
      To get text nodes and elements containing text, do
      $parent->findnodes("*[text()] | text()")
      And so on.
        Yes that will work but I am looking for just the next level only not the whole tree. So for instance
        <library> <book attr1="zzz"> <title>xxxx</title> <author>yyyyy</author> </book> <book attr1="sssss"> ---- </book> <book attr1="dddd"> ---- </book> </library>
        So, if i was at library then I just want all of the books, no title or author elements. And if I do * then I get back all of the elements from the whole tree. Regards, John