Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: hashref with(out) arrays

by bfdi533 (Friar)
on Nov 22, 2017 at 18:11 UTC ( [id://1204054]=note: print w/replies, xml ) Need Help??


in reply to hashref with(out) arrays

Ok, so taking a stab at XML::Rules and the first thing I find in my XML is a namespace tag prefixed on all of my data elements.

<ns2:Height Units="inches">1.00</ns2:Height> <ns2:Length Units="inches">1.00</ns2:Length> <ns2:Width Units="inches">1.00</ns2:Width> <ns2:Weight Units="pounds">0.01</ns2:Weight>

In my rules, do I refer to this tag as "ns2:Height" or as "Height" somehow accounting for the namespace?

Update:

And do I need to deal with every tag in the XML? For example, if my XML is:

<ns2:Item> <ns2:Attributes> <ns2:Height Units="inches">1.00</ns2:Height> <ns2:Length Units="inches">1.00</ns2:Length> <ns2:Width Units="inches">1.00</ns2:Width> <ns2:Weight Units="pounds">0.01</ns2:Weight> </ns2:Attributes> </ns2:Item>

Can I have rules that just deal with Height, Length, Width, Weight, etc? Or do I have to have rules for Item and Attributes to get to the other attributes? I am unclear from reading the XML::Rules documentation. The example has a _default tag which looks like it is supposed to deal with all items not otherwise specified but so far, I am only getting Item in my output as:

0 HASH(0x36a9310) 'Item' => undef

The rules that I am using are:

my @rules_prod = ( _default => sub { $_[0] => $_[1]->{_content}}, 'ns2:Weight' => sub { weight => $_[1]->{_content} }, 'ns2:Height' => sub { height => $_[1]->{_content} }, );

Replies are listed 'Best First'.
Re^2: hashref with(out) arrays
by haukex (Archbishop) on Nov 23, 2017 at 09:16 UTC
    In my rules, do I refer to this tag as "ns2:Height" or as "Height" somehow accounting for the namespace?

    You can assign namespace prefixes using the namespaces option, for example namespaces => { 'http://www.example.com/foo' => 'ns2' }, and then use the ns2: prefix in your rules. Or, you can use the same option to map different namespaces to the same prefix, if that is acceptable in your application (!), in the way I showed below (in that case the empty prefix).

    Can I have rules that just deal with Height, Length, Width, Weight, etc?

    Yes, you can specify multiple tag names for a rule with |. You can also use regexes to specify rules, as in '/foo/' => ... (although I don't think the two can be combined). Here's an example:

    use warnings; use strict; use XML::Rules; use Data::Dump; my $parser = XML::Rules->new( stripspaces => 3|4, warnoverwrite => 1, namespaces => { 'http://www.example.com/foo' => '', 'http://www.example.com/bar' => '', '*' => 'die' }, # don't allow other namespaces rules => [ 'Height|Length|Width|Weight' => sub { lc $_[0] => $_[1]->{_content} }, 'root' => 'pass', _default => sub { die "Unknown tag $_[0]" }, ] ); dd $parser->parse(<<'END_XML'); <root xmlns="http://www.example.com/bar" xmlns:foo="http://www.example.com/foo" > <foo:Height Units="inches">1.00</foo:Height> <foo:Length Units="inches">1.00</foo:Length> <foo:Width Units="inches">1.00</foo:Width> <foo:Weight Units="pounds">0.01</foo:Weight> </root> END_XML __END__ { height => "1.00", length => "1.00", weight => 0.01, width => "1.00" }

    In this code, I've made the _default rule be to die, that way I can make sure during development that I haven't overlooked a tag in the input XML. But you can of course also specify any _default rule you like.

    By the way, since these questions are going a bit off topic from the original post, it might be best to start a new thread on your questions about XML::Rules, showing your code, enough example input to reproduce, etc. (Short, Self-Contained, Correct Example).

      Thank you very much; that is indeed what I needed (and was missing). That example is right on point!

      Agreed on the separate issue that would have been better for the XML::Rules. I was really trying to solve the same issue (in my head) so this is, for me, the same ultimate issue on how to get my data out of the XML. The XML::Simple representation of the data structure made the initial question about the array the starting point.

      I will keep your recommendations in mind for the future.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1204054]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found