Perl modules for multi hierarchical XML processing

Discussion in 'XML' started by Ivan Dwyer, Jul 23, 2004.

  1. Ivan Dwyer

    Ivan Dwyer Guest

    I hope my Subject makes sense:)

    I'm developing a front end form that generates an XML file that is the
    nprocessed through a templating system. Unfortunately, the format the
    XML needs to be in has proven to be difficult for me to reference with
    any perl modules. Here's sample XML.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE record>
    <record name="test" type="content">
    <item name="Title">
    <value>Testing</value>
    </item>
    <item name="Author">
    <value>Ivan</value>
    </item>
    <item name="Author_Desc">
    <value>Stumped</value>
    </item>
    <item name="List">
    <value>
    <item name="List_ID">
    <value>1</value>
    </item>
    <item name="List_Title">
    <value>List Title 1</value>
    </item>
    </value>
    </item>
    <item name="List">
    <item name="List_ID">
    <value>2</value>
    </item>
    <item name="List_Title">
    <value>List Title 2</value>
    </item>
    </item>
    </record>

    Unfortunately, all elements have to be <item name="something">, and
    when there are multiple instances of the same type, they have to have
    the same name.

    I have no problem accessing the individual elements using XML::Simple.

    my $s = XML::Simple->new();
    my $data = $s->XMLin($file);

    print "$data->{item}->{Title}->{value}", etc.

    My problem occurs when trying to iterate through the list items. I've
    tried a few modules like XML::parser, XML::XPath, and XML::Twig, but I
    have yet to find a way to access a value based on one of the list
    element attributes. My closest attempt has been with XPath. I'm able
    to get the list blocks into nodes, but I get stuck from there.

    my $x = XML::XPath->new( filename => $file );
    my $nodeset = $x->findnodes( "/record/item[\@name='List']" );

    foreach my $node ( $nodeset->get_nodelist )
    {
    ##### Lost in Here ######
    # Need List->List_ID->value and List->List_Title->value somehow
    }

    Would anyone have any advice on a solution with XPath, or any
    assistance as to a better module choice? I would greatly appreciate
    any help. Thanks,

    Ivan Dwyer
    Ivan Dwyer, Jul 23, 2004
    #1
    1. Advertising

  2. * Ivan Dwyer wrote in comp.text.xml:
    >My problem occurs when trying to iterate through the list items. I've
    >tried a few modules like XML::parser, XML::XPath, and XML::Twig, but I
    >have yet to find a way to access a value based on one of the list
    >element attributes. My closest attempt has been with XPath. I'm able
    >to get the list blocks into nodes, but I get stuck from there.
    >
    >my $x = XML::XPath->new( filename => $file );
    >my $nodeset = $x->findnodes( "/record/item[\@name='List']" );
    >
    >foreach my $node ( $nodeset->get_nodelist )
    >{
    > ##### Lost in Here ######
    > # Need List->List_ID->value and List->List_Title->value somehow
    >}


    You should be able to

    my $foo = $node->findnodes('item[@name="List_ID"]/value')->[0];
    my $bar = $node->findnodes('item[@name="List_Title"]/value')->[0];

    or maybe $node->findvalue(...) depending on the content model of these
    elements.
    Bjoern Hoehrmann, Jul 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Björn Bengtsson
    Replies:
    0
    Views:
    1,982
    Björn Bengtsson
    Aug 20, 2003
  2. Bomb Diggy
    Replies:
    0
    Views:
    431
    Bomb Diggy
    Jul 28, 2004
  3. John
    Replies:
    2
    Views:
    595
    Wole Ogunremi
    Nov 18, 2004
  4. Replies:
    0
    Views:
    439
  5. Jane Austine
    Replies:
    14
    Views:
    2,240
    Matej Cepl
    Jul 18, 2012
Loading...

Share This Page