Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: XML need suggestions

by Wonko the sane (Deacon)
on Jun 13, 2007 at 13:55 UTC ( #620964=note: print w/ replies, xml ) Need Help??


in reply to XML need suggestions

I find the best way to manipulate XML is with XSLT.
I took a stab at what I thought you were asking for in output, and added some extra records to print out.

!/usr/local/bin/perl -w use strict; use warnings; use XML::LibXML; use XML::LibXSLT; my $xml = q{<eSummaryResult> <DocSum> <Id>25</Id> <Item Name="Description" Type="String">Ableson Murine</Item> </DocSum> <DocSum> <Id>26</Id> <Item Name="Description" Type="String">yada yada</Item> </DocSum> <DocSum> <Id>27</Id> <Item Name="Description" Type="String">yada yada something else</It +em> </DocSum> </eSummaryResult> }; my $xslt_stylesheet = q{<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:template match="/eSummaryResult/DocSum"><xsl:apply-templates se +lect="Id|Item"/></xsl:template> <xsl:template match="Id"><xsl:value-of select="."/>, </xsl:template> <xsl:template match="Item[@Name='Description']"><xsl:value-of select +="."/></xsl:template> </xsl:stylesheet> }; my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); my $style_doc = $parser->parse_string( $xslt_stylesheet ); my $source = $parser->parse_string( $xml ); my $stylesheet = $xslt->parse_stylesheet( $style_doc ); my $results = $stylesheet->transform( $source ); my $output = $stylesheet->output_string( $results ); print $output;
Output:
:!./t2.pl 25, Ableson Murine 26, yada yada 27, yada yada something else
Hope that helps.
Best Regards,
WOnko


Comment on Re: XML need suggestions
Select or Download Code
Re^2: XML need suggestions
by Jenda (Abbot) on Jun 14, 2007 at 09:23 UTC

    Let me see. what would the code be for the very same thing using XML::Rules:

    use XML::Rules; my $xml = q{<eSummaryResult> <DocSum> <Id>25</Id> <Item Name="Description" Type="String">Ableson Murine</Item> </DocSum> <DocSum> <Id>26</Id> <Item Name="Description" Type="String">yada yada</Item> </DocSum> <DocSum> <Id>27</Id> <Item Name="Description" Type="String">yada yada something else</It +em> </DocSum> </eSummaryResult> }; my $parser = XML::Rules->new( rules => [ Item => sub { $_[1]->{Name} => $_[1]->{_content}}, Id => 'content', DocSum => sub { print "$_[1]->{Id}, $_[1]->{Description}\n"; return; }, ] ); $parser->parse($xml);
    There's less code to get the job done without XSLT then there is to apply it. And I'm sure you'd get the same result using most other modules.

    I find XSLT incredibly clumsy, blithering and unreadable.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-07-26 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls