Solved

add priority(optional) to each line of input to alter output

Posted on 2013-10-22
2
271 Views
Last Modified: 2013-10-29
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2013-10-20</lastmod>
<priority>0.9</priority>
</url>
<url>
<loc>http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii</loc>
<lastmod>2013-10-20</lastmod>
<priority>0.9</priority>
</url>
<url>
<loc>http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand</loc>
<lastmod>2013-10-20</lastmod>
</url>
<url>
<loc>http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland</loc>
<lastmod>2013-10-20</lastmod>
</url>
<url>
<loc>http://www.example.com/catalog?item=83&amp;desc=vacation_usa</loc>
<lastmod>2013-10-20</lastmod>
</url>
</urlset>

Open in new window




I want the xml file in the code block to be the output

created by php
with the php input an array of urls comma priority (optional)
http://www.example.com/,0.9
http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii,0.9
http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand
http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland
http://www.example.com/catalog?item=83&amp;desc=vacation_usa


this code works (but want to add priority (optional))
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

/** http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28271756.html
with the php input an array of urls
http://www.example.com/
http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii
http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand
http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland
http://www.example.com/catalog?item=83&amp;desc=vacation_usa
 */

$arr = array
( 'http://www.example.com/'
, 'http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii'
, 'http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand'
, 'http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland'
, 'http://www.example.com/catalog?item=83&amp;desc=vacation_usa'
)
;

$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
EOD;

foreach ($arr as $url)
{
    $xml
    .= PHP_EOL
    . '<url>' . PHP_EOL
    . '<loc>' . $url . '</loc>' . PHP_EOL
    . '<lastmod>' . date('Y-m-d') . '</lastmod>' . PHP_EOL
    . '</url>'
    ;
}
$xml .= PHP_EOL . '</urlset>';

// TEST: IS IT A VALID XML DOCUMENT?
$obj = SimpleXML_Load_String($xml);

// SHOW THE WORK PRODUCT
echo htmlentities($xml);

Open in new window

0
Comment
Question by:rgb192
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39591302
I'm going to recommend that you do this a little differently.  Suggest you provide parallel construction of each of the <url> elements.  If priority is omitted, put an empty tag into the XML document.
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28273696.html

$arr = array
( 'http://www.example.com/,0.9'
, 'http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii,0.9'
, 'http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand'
, 'http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland'
, 'http://www.example.com/catalog?item=83&amp;desc=vacation_usa'

)
;

$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
EOD;

foreach ($arr as $txt)
{
    $txt = explode(',', $txt);
    $url = $txt[0];
    $pri = isset($txt[1]) ? $txt[1] : NULL;
    $xml
    .= PHP_EOL
    . '<url>' . PHP_EOL
    . '<loc>' . $url . '</loc>' . PHP_EOL
    . '<lastmod>' . date('Y-m-d') . '</lastmod>' . PHP_EOL
    ;
    if ($pri)
    {
        $xml .= '<priority>' . $pri . '</priority>' . PHP_EOL;
    }
    else
    {
        $xml .= '<priority />' . PHP_EOL;
    }
    $xml .= '</url>'
    ;
}
$xml .= PHP_EOL . '</urlset>';

// TEST: IS IT A VALID XML DOCUMENT?
$obj = SimpleXML_Load_String($xml);

// SHOW THE WORK PRODUCT
echo htmlentities($xml);

Open in new window

Cheers, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 39610823
thanks

>>If priority is omitted, put an empty tag into the XML document.

related question:
hide priority when not used

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28280748.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now