The "kitchen sink" module for manipulating XML is XML::Twig.

A starting point using XML::Twig to solve you problem may be:

use strict; use warnings; use XML::Twig; my $xml = <<XML; <xs:complexType name="semt.002.001.01"> <xs:sequence> <xs:element name="PrvsRef" type="AdditionalReference2" minOccurs="0" m +axOccurs="unbounded"/> <xs:element name="RltdRef" type="AdditionalReference2" minOccurs="0" m +axOccurs="unbounded"/> <xs:element name="MsgPgntn" type="Pagination"/> <xs:element name="StmtGnlDtls" type="Statement3"/> <xs:element name="AcctDtls" type="SafekeepingAccount1"/> <xs:element name="BalForAcct" type="AggregateBalanceInformation1" minO +ccurs="0" maxOccurs="unbounded"/> <xs:element name="SubAcctDtls" type="SubAccountIdentification1" minOcc +urs="0" maxOccurs="unbounded"/> <xs:element name="TtlVals" type="TotalValueInPageAndStatement" minOccu +rs="0" maxOccurs="1"/> <xs:element name="Xtnsn" type="Extension1" minOccurs="0" maxOccurs="un +bounded"/> </xs:sequence> </xs:complexType> XML my $twig = XML::Twig->new (twig_handlers => {'[@name]' => \&showNestin +g}); $twig->parse ($xml); sub showNesting { my @path; for my $elt (reverse $_->ancestors_or_self ()) { next unless defined $elt->att ('name'); push @path, $elt->att ('name'); } print join ('.', @path), "\n" if @path; }

Prints:

semt.002.001.01.PrvsRef semt.002.001.01.RltdRef semt.002.001.01.MsgPgntn semt.002.001.01.StmtGnlDtls semt.002.001.01.AcctDtls semt.002.001.01.BalForAcct semt.002.001.01.SubAcctDtls semt.002.001.01.TtlVals semt.002.001.01.Xtnsn semt.002.001.01

which doesn't do exactly what you want (I can't see exactly what you want in any case), but should get you going.


Perl is environmentally friendly - it saves trees

In reply to Re: perl parsing xml by GrandFather
in thread perl parsing xml by thickice97

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.