Search

I’m currently rebuilding one of my sites, and has now come to the point where it’s time to move data from the old to the new one. Is the best method Nick Dunn’s Archiving XML with Symphony? Or are there other options?

Alright, almost got this working with Nick’s PHP script. But I’m running into problem where each entry has a node with multiple childs, like

<topic>
<item handle="media">media</item>
<item handle="mobilt">mobilt</item>
</topic>

How can I in PHP iterate over the items and have them added, as a comma separated list, to a variable?

If its an array: string implode(string $glue, array $pieces);

Not an array. I’m trying with

    foreach ($bookmark->getElementsByTagName("topic")->item(0)->childNodes as $category) {
        $bookmark_topic .= $category->nodeValue . ", ";
    }

But that gives me

, feature, , recension, ,

Three comma to much…

You might want to take a look at the EventEx extension. The way I understand it, each of the child nodes can be imported into a separate Topics section with section links to the current entry. Maybe Nick can advise whether this would be a suitable use case.

The topics are going into a tag field, so I don’t think the EventEx is needed. It’s rather about me not being able to get my head around PHP DOM-functions, I’m afraid…

What is it I’m missing here. I tried another approach, by first creating an array, just to see what data is really in there. With

foreach ($bookmark->getElementsByTagName("topic")->item(0)->childNodes as $topics) {
            $topic_array[] = $topics->nodeValue;
        }

I get this array:

Array ( [0] => [1] => feature [2] => [3] => recension [4] => ) 

Where does key 0, 1 and 4 come from? The topic-node just have two children, right?

My XML has two other nodes with item as childs. In total, there are five ‘item’. I guess thats were the extra commas and keys originate. But why are these other three items get into my string/array?

Although I wouldn’t be able to write one… We need an extension to import XML into Sections.

My XML has two other nodes with item as childs. In total, there are five ‘item’.

I remember having a similar issue and it was because your code finds all nodes (i.e. whitespace), not just text.

There are various workarounds but I can’t remember how I solved. Will look on other computer later. One solution (I didn’t use this) and good explanation:

http://blog.netweblogic.com/php/tutorials/domdocument-whitespace-php/

Thanks Lewis, that was something I would never had figured out myself.

I was thinking about two different solutions instead.

Right now, I am just using a copy-of to have the XML the DS produces made available for my new site. But I could instead use some XSLT to transform topic nodes into a comma separated one.

Or i could use SimpleXMLElement on the recieving end to select nodes with Xpath.

I used SimpleXMLElement() like so:

$xml = new SimpleXMLElement($return_xml, null, false);

foreach($xml->children() as $foo)
{
    $bar = $foo->bar->get_content();
}

I also had trouble with SimpleXMLElement() methods sometimes returning an object when I expected a string, so I would just convert object to string by prefixing (string).

I’ve tried with the method Lewis linked to, and also with SimpleXMLElement. Both without any success. So I’m now trying the last approach, making my current site output xml that’s easier for me to handle on the recieving end.

But there are still some trouble. Why doesn’t this XSLT generate valid XML? It’s the article-nodes I want to create for each article that throws an error on me

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8" indent="yes" />

<xsl:template match="data">
   <xsl:apply-templates select="export-articles/entry" />
</xsl:template>

<xsl:template match="entry">
<article>
   <xsl:copy-of select="header" />
   <xsl:copy-of select="intro" />
   <xsl:copy-of select="body" />
   <topic><xsl:apply-templates select="topic/item" /></topic>
</article>

</xsl:template>

<xsl:template match="topic/item">
   <xsl:value-of select="." />
   <xsl:if test="position() != last()">
     <xsl:text>, </xsl:text>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>

Try adding a parent articles parent node inside your first match. If I remember correctly there always needs to be a single root node in which the rest of an XML document is nested.

Great, thanks!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details