What is the namespace method in Mojo::DOM?

Overview

The namespace method will provide the namespace for the given HTML element if it is present. This method is provided by the Mojo::DOM module, which is an HTML/XML DOM parser with CSS selectors.

Syntax

$dom->at('html element')->namespace

Return value

This method will return the namespace for the given element if it is present. Otherwise, it will return undef.

Example

use 5.010;
use Mojo::DOM;
# Parse the html
my $dom = Mojo::DOM->new('<svg xmlns:svg="http://www.w3.org/2000/svg"><svg:circle>3.14</svg:circle></svg>');
# Get namespace
say $dom->at('svg\:circle')->namespace;

Explanation

  • Line 2: We import the Mojo::DOM module.
  • Line 5: We parse the HTML and store it in the $dom scalar.
  • Line 8: We get the namespace for the element svg:circle using the namespace method and print it.

Free Resources