Reputation: 2207
Im working to php SimpleXMLElement to build an xml sitemap and I have 2 questions about it.
As I'm working with a multilanguage domain I need to include the hreflang element see https://support.google.com/webmasters/answer/2620865?hl=en for reference.
This element has 3 attributes 'rel', 'href' & 'hreflang'.
How do we set this element and add custom values to it?
// example
foreach($array as $value ){
$item->addChild('xhtml:link' , '//takes no value');
// needed output
<xhtml:link href="http://www.example.com/path-to-file" hreflang="de" rel="alternate"/>
}
Also when using
->addChild('xhtml:link')
it will output
<link/>
and NOT
<xhtml:link/>
Yes im using the correct urlset attributes(xmlns:xhtml="http://www.w3.org/1999/xhtml").
Upvotes: 0
Views: 360
Reputation: 57121
When adding a new element using addChild()
there is a third parameter for the namespace. Also you add the attributes using - addAttribute()
. So create the element, then add each attribute one at a time...
foreach($array as $value ){
$newElement = $item->addChild('link' , '//takes no value', 'xhtml');
$newElement->addAttribute( "href", "http://www.example.com/path-to-file");
$newElement->addAttribute( "hreflang", "de");
$newElement->addAttribute( "rel", "alternate");
// needed output
//<xhtml:link href="http://www.example.com/path-to-file" hreflang="de" rel="alternate"/>
}
Upvotes: 1