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

Hi! I'm trying to create a mixed content node like:
<head><term>Einführung</term> in die <term>Soziologie</term></head>
Given is the Text String "Einführung in die Soziologie". But I don't know how. I couldn't fin anything in the forum or on the net, I checked the documentation o XML::LibXML, but again no hits. Tips anybody?

Replies are listed 'Best First'.
Re: Create Mixed Content with XML::LibXML
by afoken (Chancellor) on Apr 13, 2011 at 14:38 UTC

    I see no "mixed content" node.

    What I see is a head element that has three child elements, the first child and the third child are term elements, the second child is a #text element. Both term elements have one #text child element each.

    And this is exactly how you would create it with XML::LibXML. Create the root element, create child elements, append the elements, create text nodes, append them to the parent nodes (Hint: shortcut methods appendText() and appendTextChild()).

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      As code:

      my $term1 = XML::LibXML::Element->new('term'); $term1->appendTextNode('Einführung'); my $term2 = XML::LibXML::Element->new('term'); $term2->appendTextNode('Soziologie'); my $head = XML::LibXML::Element->new('head'); $head->addChild($term1); $head->appendTextNode(' in die '); $head->addChild($term2);

      (Pay attention to the notes for addChild in the docs.)

        thank you both, i didn't know i could just add a text node inbetween those two nodes. thanks a lot!