in reply to Re: XML::Twig - setting text and elements
in thread XML::Twig - setting text and elements

tried that, but getting this:

cannot paste an element that belongs to a tree at C:\scripts\keyref2href.pl line 259.

line 259:

$topicref->paste(last_child=>$_);

seems to be crashing on the $_ that contains the reference to the element. Any thoughts?

Thank you.

Replies are listed 'Best First'.
Re^3: XML::Twig - setting text and elements
by Anonymous Monk on Sep 09, 2015 at 22:51 UTC

    Any thoughts?

    You can't paste what has not been cut?

Re^3: XML::Twig - setting text and elements
by Anonymous Monk on Sep 10, 2015 at 11:42 UTC
    Either $_->copy or $_->cut the element you are trying to paste.

      Ok, sorry all to beleaguer this; I'm getting close but not seeing the cigar.

      Here's my code and what I'm getting. (I'll post as clear an explanation of what I'm trying to do afterwards - some folks have said I wasn't clear enough to begin with.)

      if($xref->att('keyref')){ my($h) = $linktext{$xref->att('keyref')}; # the keyref att is the key + previously defined for the %linktext hash my($count); foreach my $i (@$h){ $count++; print "child $count:\n"; $i->print; print $/; $xref->copy->paste(last_child=>$i); # this is where I must be doing +something wrong! } $xref->print; }

      The results:

      child 1: <keyword conref="ds"/> child 2: Database- child 3: <b>level</b> child 4: privileges <xref keyref="ids_sqs_0147"></xref>

      As you can see nothing is changing - the copy-paste doesn't seem to be working, the children are not getting inserted, though they're clearing displaying correctly with each $i->print statement. What am I doing wrong?


      Only read the following if you want to get a clearer explanation of what I'm trying to do.

      I have a number of "reference" files that each contain hundreds of citeReference elements. I am interested in two attributes - keys and href - and anything contained inside the child linktext element. For example:

      <citeReference keys="ids_sqs_0147" href="../com.ibm.sqls.doc/ids_sqs_0147.dita#ids_sqs_0147"> <linktext> <keyword conref="ds"/> Database-<b>level</b> privileges </linktext> </citeReference>

      My script uses XML::Twig to read each citeReference element and store its children in a referenced hash called %linktext.

      my @children = $citeReference->children; $linktext{citeReference->att('keys')} = \@children;

      Elsewhere I have hundreds of files with xref elements, with a keyref attribute that matches the keys attribute of the citeReference. For example:

      <xref keyref="ids_sqs_0147">some existing text that should get replaced</xref>

      What I want to do is replace keyref with the href from the citeReference element, and insert the linktext inside of the xref, including its text and child elements.

      <xref href="../com.ibm.sqls.doc/ids_sqs_0147.dita#ids_sqs_0147"> <keyword conref="ds"/> Database-<b>level</b> privileges </xref>

      My script matches the keyref attribute (e.g. "ids_sqs_0147") which corresponds to the keys attribute, and dereferences it to an array. This is where I'm not seeing what I want.

      if($xref->att('keyref')){ my($h) = $linktext{$xref->att('keyref')}; my($count); foreach my $i (@$h){ $count++; print "child $count:\n"; $i->print; print $/; $xref->copy->paste(last_child=>$i); } $xref->print; }

      The results:

      child 1: <keyword conref="ds"/> child 2: Database- child 3: <b>level</b> child 4: privileges <xref keyref="ids_sqs_0147">Database-level privileges</xref>

      As you can see nothing is changing - the cut-paste doesn't seem to be working. What am I doing wrong?

      Thanks - and sorry to be dense here.

        Aha! I figured it out:

        $topicref->set_content(@$h);

        Result:

        <xref href="../com.ibm.sqls.doc/ids_sqs_0147.dita#ids_sqs_0147"> <keyword conref="ds"/> Database-<b>level</b> privileges </xref>

        Thanks for the suggestions.

        Ok, sorry all to beleaguer this; I'm getting close but not seeing the cigar.

        Probably because fragments don'r run