Feeds namespaces API
RSS is extensible using XML namespaces, but not so the default syndication parser of the Feeds module. Feeds namespaces provides an RSS parser and an API to extend it.
Declaring support for a namespace: hook_feeds_namespaces()
hook_feeds_namesaces() returns an associative array keyed by namespace URL, that contains information about the namespace.
function hook_feeds_namespaces() {
$ns['http://example.com/example-namespace-url'] = array(
'name' => t('The name of the namespace'),
'description' => t('A longer description of the namespace'),
'sources' => array(
// Available mapping sources
),
);
return $ns;
}
Mapping sources
The sources array contains information about available mapping sources. The user can select those sources on the mappings page.
This is an example feed:
<rss version="2.0">
<channel>
...
<item>
<title>The title of the feed item</title>
...
</item>
</channel>
</rss>
To map a single element you can simply use:
'sources' => array(
'title' => array( // the key is the name of the element <title>
'name' => t('Title'),
'description' => t('Title of the item'),
),
...
),
There are however more advanced settings:
| Property | Default | Description |
|---|---|---|
| name | required | The human readable name of the mapping source. Should usually be passed through t(). |
| description | no description | A description of the mapping source in a few sentences. Should usually be passed through t(). |
| element | the array key of the mapping source | The name of the element to use, that is a direct child of an <item>. |
| xpath | use element instead |
Use an XPath query to get an element, relative to the current item. If the If you are doing an XPath query in any namespace other than the core RSS namespace, use Note that |
| attribute | get the element content, not an attribute | The name of the element's attribute. Use this to get attribute values, instead of the selected element's content. |
| attribute namespace | FALSE
|
Only useful together with With With Other than |
| multiple | FALSE |
TRUE, to allow multiple values. If there are multiple elements selected using If there are multiple elements and you use |
| callback | feeds_namespaces_default_callback |
Use a custom callback function to get a value. The default callback implements the element, element namespace, xpath, attribute and multiple properties. You can use your own properties for your custom callback. See "Custom callbacks" section. |
| default value | NULL |
If no value is found (callback returning NULL) use this value. The default callback doesn't find a value, if no element or attribute matches. |
| default value callback | feeds_namespaces_return_default_value |
The default callback returns the default value, but you can use custom logic for default values. See "Custom callbacks" section. |
| rewriters | array()
| An array of rewriter callbacks or a single rewriter callback. The first callback gets the value as an argument. The return value is then used as the new value. If there are more callbacks, the value will be passed through all of them. Useful, for example, to quickly pass a value through strtotime. |
Custom callbacks
The callback and default value callback properties allow the use of custom callbacks.
Those callbacks look like that:
function example_callback_name($namespace, $sourcename, $feed, $entry, $source) {
...
}
$namespaceis the URL of the namespace specification. It will beFEEDS_NAMESPACES_RSSfor the core RSS namespace.$sourcenameis the array key from thesourcesarray.$feedis aSimpleXMLElement. It is the whole feed.$entryis the current feed entry as aSimpleXMLElement.$sourceis an array of options. It is the source definition. It contains at leastname,description,callback,default value callbackandrewriters, but there can be more options specific to the callback.
The returned value will be mapped.
Altering already supported namespaces: hook_feeds_namespaces_alter()
To alter namespaces that are already declared, implement hook_feeds_namespaces_alter().
function hook_feeds_namespaces_alter(&$ns) {
...
}
You will be given, by reference, an array of all the namespaces. It is structured just as in hook_feeds_namespaces().
Use the FEEDS_NAMESPACES_RSS constant as the array key, if you want to alter the core RSS namespace.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion