Puppet Function: stdlib::xml_encode

Defined in:
vendor_modules/stdlib/lib/puppet/functions/stdlib/xml_encode.rb
Function type:
Ruby 4.x API

Summary

Encode strings for XML files

Overview

stdlib::xml_encode(String $str, Optional[Enum['text','attr']] $type)String

This function can encode strings such that they can be used directly in XML files. It supports encoding for both XML text (CharData) or attribute values (AttValue).

Examples:

Creating an XML file from a template

file { '/path/to/config.xml':
  ensure  => file,
  content => epp(
    'mymodule/config.xml.epp',
    {
      password => $password.stdlib::xml_encode,
    },
  ),
}

Parameters:

  • str (String)

    The string to encode

  • type (Optional[Enum['text','attr']])

    Whether to encode for text or an attribute

Returns:

  • (String)

    Returns the encoded CharData or AttValue string suitable for use in XML



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'vendor_modules/stdlib/lib/puppet/functions/stdlib/xml_encode.rb', line 7

Puppet::Functions.create_function(:'stdlib::xml_encode') do
  # @param str The string to encode
  # @param type Whether to encode for text or an attribute
  # @return Returns the encoded CharData or AttValue string suitable for use in XML
  # @example Creating an XML file from a template
  #   file { '/path/to/config.xml':
  #     ensure  => file,
  #     content => epp(
  #       'mymodule/config.xml.epp',
  #       {
  #         password => $password.stdlib::xml_encode,
  #       },
  #     ),
  #   }
  dispatch :xml_encode do
    param 'String', :str
    optional_param "Enum['text','attr']", :type
    return_type 'String'
  end

  def xml_encode(str, type = 'text')
    str.encode(xml: type.to_sym)
  end
end