Catman...th_map(3pm)

πŸ’– Catmandu::Fix::xpath_map πŸ’–

okay bestie, gather round, because this one is literally the fix you need when your XML is being extra πŸŽ€

Catmandu::Fix::xpath_map maps values from a XML::LibXML::Element value into a field. yes, that's the whole vibe. no cap. ✨


🌸 SYNOPSIS 🌸

#   <mets:dmdSec ID="dmd1">
#    <mets:mdWrap MDTYPE="MODS">
#     <mets:xmlData>
#      <mods:mods xmlns:mods="http://www.loc.gov/mods/v3" ID="mods1" version="3.4">
#        <mods:titleInfo>
#         <mods:title>Alabama blues</mods:title>
#        </mods:titleInfo>
#      </mods:mods>
#     </mets:xmlData>
#    </mets:mdWrap>
#   </mets:dmdSec>

# The dmdSec.0.mdWrap.xmlData contains a XML::LibXML::Element
# Map the value of the 'mods:titleInfo/mods:title' XPath to
# a new field 'my_new_field'.
# Optionally provide one or more namespace mappings to use
xpath_map(
    dmdSec.0.mdWrap.xmlData,
    'mods:titleInfo/mods:title',
    my_new_field,
    -mods:'http://www.loc.gov/mods/v3'
)

# Result:
#
# 'my_new_field' => 'Alabama blues'

see how clean that is?? obsessed. the namespace mapping is the little cherry on top πŸ“


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea β˜•: not every XML field in an XML Schema can just be mapped to a Perl Hash using Catmandu::XSD. especially those <any> fields in a schema β€” the ones that can hold literally any type of XML? yeah, those are the problem children 😭

those fields get mapped into a blessed XML::LibXML::Element object. and that's where the "xpath_map" Fix comes in, bestie β€” it lets you reach into those blessed objects and extract data from them using XPaths. main character energy fr fr πŸ‘‘


πŸ‘‘ METHOD πŸ‘‘

xpath_map(xml_field, xpath, new_field [, namespace-prefix:namespace-url [,...]])

Map an XML field at xml_field to new_field using an XPath expression xpath. that's it, that's the method πŸ’…


🌈 SEE ALSO 🌈

  • Catmandu::Fix

πŸ’œ AUTHOR πŸ’œ

Patrick Hochstenbach , patrick.hochstenbach at ugent.be


This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See <http://dev.perl.org/licenses/> for more information.


and that's a wrap, angel 🫢 this lil page came to you via perl v5.36.0, dated 2022-11-29 β€” slay, iconic, no notes πŸ’–βœ¨

πŸ“¦ where this page comes from