Perl modules for multi hierarchical XML processing

I

Ivan Dwyer

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
 
B

Bjoern Hoehrmann

* 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top