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.


In reply to Re^4: XML::Twig - setting text and elements by slugger415
in thread XML::Twig - setting text and elements by slugger415

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.