Hi,

I'm trying to extract elements from nodes in XML using XML::LibXML, and I need to extract the elements that could be embedded within that node.

For example, I have this XML string: <doc><text>From mobile, <ph1 i="1" type="33" x="1"/>dial<ph2 i="1"/> this number:</text></doc>

What I need as a result is this: From mobile, <ph1 i="1" type="33" x="1"/>dial<ph2 i="1"/> this number:, but I cannot extract the elements embedded (<ph1 i="1" type="33" x="1"/> and <ph2 i="1"/>), they disappear no matter what I try.

This is the sample code to reproduce the issue:

use strict; use warnings; use XML::LibXML; my $xmlString = "<doc><text>From mobile, <ph1 i=\"1\" type=\"33\" x=\" +1\"/>dial<ph2 i=\"1\"/> this number:</text></doc>"; my $dom = XML::LibXML->load_xml(string => $xmlString); my $to_literal = $dom->to_literal('/doc/text'); print "to_literal:\t$to_literal\n"; my $findvalue = $dom->findvalue('/doc/text'); print "findvalue:\t$findvalue\n"; my $textContent = $dom->textContent('/doc/text'); print "textContent:\t$textContent\n";

This is what I get when I run this code:

to_literal: From mobile, dial this number: findvalue: From mobile, dial this number: textContent: From mobile, dial this number:

It looks like "to_literal", "findvalue" and "textContent" take out the embedded placeholders, but I want to keep them in.

Is there any way to get what I need with a simple method?

Thanks very much in advance!

TA


In reply to XML::LibXML - How to extract an element including the elements within? by TravelAddict

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.