0

I am trying to add a new node after in designmap.xml

The new node includes an src attribute that is customized by the array below.

$newStories = Array ( [0] => u102 [1] => u103 [2] => u107 [3] => u156  );

$designMap = simplexml_load_file('designmap.xml');

foreach ($newStories as $story) {
  $newStoryNode = '<idPkg:Story src="Stories/Story_' . $story . '.xml" />';
  $insert = new SimpleXMLElement($newStoryNode);
  $target = current($designMap->xpath('//idPkg:Story[last()]'));

  simplexml_insert_after($insert, $target);
}

function simplexml_insert_after(SimpleXMLElement $insert, SimpleXMLElement $target)
{
    $target_dom = dom_import_simplexml($target);
    $insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
    if ($target_dom->nextSibling) {
        return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
    } else {
        return $target_dom->parentNode->appendChild($insert_dom);
    }
}

$designMap->asXML('designmap.xml');

I get the following warnings for each of the looks:

  1. SimpleXMLElement::__construct(): namespace error : Namespace prefix idPkg on Story is not defined
  2. SimpleXMLElement::__construct(): u102.xml
  3. SimpleXMLElement::__construct(): ^ in
Justin
  • 31
  • 5
  • A general advice: learn how to use DOMDocument, SimpleXML is just useless (and not so simple). – Casimir et Hippolyte Jan 26 '17 at 21:13
  • I am working on that option right now. Still looking to target the insert to happen after the last node as the function above does. – Justin Jan 26 '17 at 21:52
  • 1
    The InDesign Markup Language mostly relies on adobe specific namespaces. When you instantiate a xml object from the file, you loose most of the extra infos such as namespaces and possibly declared entities. Then when you later try to inject attributes with a prefix, you get error because the compiler doesn't even know where that prefix comes from. It's up to you to see how to declare and add namespaces to your xml object. Be aware that writing the file may lead to the same issues. – Loic Jan 27 '17 at 10:03
  • I have managed to run some successful tests using the DOMDocument method, using the proper namespace that Loic referenced. Currently I am stuck at the ability to target the insert to after the last element. My query seems valid $xpath->query("(//idPkg:Story)[last()]") – Justin Jan 27 '17 at 14:35

1 Answers1

0

$xpath->query("//idPkg:Story[last()]")

Not sure you need to embrace the first statement "//idPkg:Story"

Loic
  • 2,173
  • 10
  • 13