homepage Welcome to WebmasterWorld Guest from 23.20.220.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How create xmlns: attribute for a XML document?
NomikOS




msg:4219489
 9:56 pm on Oct 20, 2010 (gmt 0)

I am trying to create a google sitemap. The expected results (http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=80472#2) is something like this:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>http://www.example.com/videos/some_video_landing_page.html</loc>
<video:video>
<video:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
<video:title>Grilling steaks for summer</video:title>
<video:description>Alkis shows you how to get perfectly done steaks every
time</video:description>
<video:content_loc>http://www.example.com/video123.flv</video:content_loc>
<video:player_loc allow_embed="yes" autoplay="ap=1">
http://www.example.com/videoplayer.swf?video=123</video:player_loc>
<video:duration>600</video:duration>
<video:expiration_date>2009-11-05T19:20:30+08:00</video:expiration_date>
<video:rating>4.2</video:rating>
<video:view_count>12345</video:view_count>
<video:publication_date>2007-11-05T19:20:30+08:00</video:publication_date>
<video:tag>steak</video:tag>
<video:tag>meat</video:tag>
<video:tag>summer</video:tag>
<video:category>Grilling</video:category>
<video:family_friendly>yes</video:family_friendly>
<video:restriction relationship="allow">IE GB US CA</video:restriction>
<video:gallery_loc title="Cooking Videos">http://cooking.example.com</video:gallery_loc>
<video:price currency="EUR">1.99</video:price>
<video:requires_subscription>yes</video:requires_subscription>
<video:uploader info="http://www.example.com/users/grillymcgrillerson">GrillyMcGrillerson
</video:uploader>
</video:video>
</url>
</urlset>


To begin we code:


$xmlDoc = new DOMDocument();

$root = $xmlDoc->appendChild(
$xmlDoc->createElement("urlset"));

$root->appendChild(
$xmlDoc->createAttribute("xmlns"))->appendChild(
$xmlDoc->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9"));
... etc ...


To produce:


<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
... etc ...
</urlset>


Question: How continue to produce xmlns:video attribute:


<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
... etc ...
</urlset>


Seems that we need createAttributeNS, but not sure.


$root->appendChild(
$xmlDoc->createAttributeNS( "http://www.google.com/schemas/sitemap-video/1.1", "video:attr" ));
... etc ...


Produce:

<?xml version="1.0"?>
<urlset xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" video:attr="">
... etc ...


video:attr="" is breaking the document!

 

NomikOS




msg:4219608
 4:27 am on Oct 21, 2010 (gmt 0)

SOLVED!

is there no need to use createAttributeNS. Just use createAttribute twice. Once for xmlns and once for xmlns:video

--- code ---
$xmlDoc = new DOMDocument('1.0', 'UTF-8');

$root = $xmlDoc->appendChild(
$xmlDoc->createElement('urlset'));

$root->appendChild(
$xmlDoc->createAttribute('xmlns'))->appendChild(
$xmlDoc->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9'));

$root->appendChild(
$xmlDoc->createAttribute('xmlns:video'))->appendChild(
$xmlDoc->createTextNode('http://www.google.com/schemas/sitemap-video/1.1'));
--- code ---

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved