Templa...:XPath(3pm)

πŸ“¦
version 2.17 Β· 2 versions
  • 2.17 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 2.17 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Template::Plugin::XML::XPath β€” it's a plugin interface to XML::XPath, bestie πŸ’…

Like, this is the <pink>Template::Plugin::XML::XPath</pink> page from the User Contributed Perl Documentation, and she's here to let your Template Toolkit templates talk to XML like it's nothing. Periodt.


πŸ¦‹ SYNOPSIS πŸ¦‹

    # load plugin and specify XML file to parse
    [% USE xpath = XML.XPath(xmlfile) %]
    [% USE xpath = XML.XPath(file => xmlfile) %]
    [% USE xpath = XML.XPath(filename => xmlfile) %]

    # load plugin and specify XML text to parse
    [% USE xpath = XML.XPath(xmltext) %]
    [% USE xpath = XML.XPath(xml => xmltext) %]
    [% USE xpath = XML.XPath(text => xmltext) %]

    # then call any XPath methods (see XML::XPath docs)
    [% FOREACH page = xpath.findnodes('/html/body/page') %]
       [% page.getAttribute('title') %]
    [% END %]

    # define VIEW to present node(s)
    [% VIEW repview notfound='xmlstring' %]
       # handler block for a <report>...</report> element
       [% BLOCK report %]
          [% item.content(view) %]
       [% END %]

       # handler block for a <section title="...">...</section> element
       [% BLOCK section %]
       <h1>[% item.getAttribute('title') | html %]</h1>
       [% item.content(view) %]
       [% END %]

       # default template block passes tags through and renders
       # out the children recursivly
       [% BLOCK xmlstring;
          item.starttag; item.content(view); item.endtag;
       END %]

       # block to generate simple text
       [% BLOCK text; item | html; END %]
    [% END %]

    # now present node (and children) via view
    [% repview.print(page) %]

    # or print node content via view
    [% page.content(repview) %]

πŸ’– DESCRIPTION πŸ’–

Okay so. This is a Template Toolkit plugin that interfaces to the XML::XPath module. That's it, that's the vibe. πŸŽ€

All methods implemented by the XML::XPath modules are available. Literally all of them. No cap.

On top of that, the XML::XPath::Node::Element module implements present($view) and content($view) methods method for seamless integration with Template Toolkit VIEWs. (Yes, the original says "methods method" β€” iconic typo, we keep it, we don't touch it πŸ’€.)

Also? The XML::XPath::Node::Text module is adorned with a present($view) method which presents itself via the view using the 'text' template. Adorned. She's wearing jewelry. πŸ’Ž


🌸 Reconstructing XML, the cute way 🌸

To aid the reconstruction of XML, methods starttag and endtag are added to XML::XPath::Node::Element which return the start and end tag for that element. So you can literally just do:

  [% item.starttag %][% item.content(view) %][% item.endtag %]

Which renders out the start tag, followed by the content rendered in the view "view", followed by the end tag. Chef's kiss. πŸ‘‘


πŸ‘‘ AUTHORS πŸ‘‘

  • This plugin module was written by Andy Wardley.
  • The XML::XPath module is by Matt Sergeant.

Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


🫢 SEE ALSO 🫢

  • Template::Plugin
  • XML::XPath
  • XML::Parser

perl v5.34.0 🌈 2022-07-04 β€” that's the version and date from the footer, bestie. Go forth and parse some XML, you absolute main character. πŸ§šπŸ’–βœ¨

πŸ“¦ where this page comes from