Data::Section; see also Inline::Files.
Code:
#!/usr/bin/env perl use Modern::Perl '2012'; use Data::Dump qw/pp/; use Data::Section -setup; use XML::XSLT; my $xslt_text = ${ main->section_data('XSLT') }; my $xslt = XML::XSLT->new($xslt_text); foreach my $xml_section ( qw/XML1 XML2/ ) { my $xml = ${ main->section_data($xml_section) }; my $res = $xslt->transform($xml)->toString(); say "XML $xml_section"; say $res; } __DATA__ __[XSLT]__ <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE stylesheet [ <!ENTITY lf " "> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" indent="no" /> <xsl:strip-space elements="*" /> <xsl:template match="array"> <xsl:text>Starting an array: </xsl:text> <xsl:value-of select="@name" /> <xsl:text>&lf;</xsl:text> <xsl:apply-templates match="item" /> <xsl:text>Ending an array&lf;</xsl:text> </xsl:template> <xsl:template match="item"> <xsl:text>Item </xsl:text> <xsl:value-of select="@name" /> <xsl:text>&lf;</xsl:text> </xsl:template> </xsl:stylesheet> __[XML1]__ <?xml version="1.0" encoding="utf-8" ?> <toplevel> <item name="1" /> <item name="2" /> <array name="3"> <item name="3.1" /> <item name="3.2" /> </array> </toplevel> __[XML2]__ <?xml version="1.0" encoding="utf-8" ?> <toplevel> <item name="1" /> <item name="2" /> <item name="3" /> <array name="3"> <item name="3.1" /> <item name="3.2" /> </array> <array name="3best"> <item name="4.1" /> <item name="4.sump" /> </array> </toplevel>
Output:
XML XML1 Item 1 Item 2 Starting an array: 3 Item 3.1 Item 3.2 Ending an array XML XML2 Item 1 Item 2 Item 3 Starting an array: 3 Item 3.1 Item 3.2 Ending an array Starting an array: 3best Item 4.1 Item 4.sump Ending an array