Solved

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

Posted on 2013-10-22
2
279 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&desc=vacation_hawaii,0.9
http://www.example.com/catalog?item=73&desc=vacation_new_zealand
http://www.example.com/catalog?item=74&desc=vacation_newfoundland
http://www.example.com/catalog?item=83&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 109

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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