2
votes

I am trying to dynamically create xml schemas with php but I'm having trouble with the namespace. What I want to do is have a function return the xsd:elements and add them to the xsd:sequence nodes.

I create the xsd:element nodes in a temporary DOMDocument in the function, I need to specify the xsd namespace "xmlns:xsd="http://www.w3.org/2001/XMLSchema" otherwise the 'xsd:' bit is removed. I then extract the required node form the temporaty document and use importNode() to copy to the existign DOMDocument. the problem is that the full xmlns string is attached to EVERY node that is returned form the function that creates the elements.

Initial DOMDocument

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="UserType">
    <xsd:sequence>
    // add elements here
    </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

Temp DOMDocument I use to collect fields

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element type="xsd:string" name="Field1"/>
    <xsd:element type="xsd:string" name="Field2"/>
    <xsd:element type="xsd:string" name="Field3"/>
</xsd:schema>

What I get

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
  <xsd:complexType name="UserType"/>
    <xsd:sequence/>
      <xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="xsd:string" name="Field1"/>
      <xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="xsd:string" name="Field2"/>
      <xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="xsd:string" name="Field3"/>
    </xsd:sequence/>
  </xsd:complexType/>
</xsd:schema/>

How do I import into an existing namespace?

2
all your xml declaration has a ?/> at the end... Is that a typo?Andreas Wong
yes that was a typo. I was screwing round with the formatting because I couldnt' get the copy/paste code to display properly then I worked out you need a blank line befor a code block.RoboDave

2 Answers

2
votes

What I needed to do was make sure I created _ALL_ the elements in the first DOMDocument using:

createElementNS('http://www.w3.org/2001/XMLSchema','xsd:sequence')

rather than:

createElement('xsd:sequence')

I was just using createElementNS on the first element that needed the xmlns declaration.

1
votes

seems to work? http://codepad.viper-7.com/SueilL

<?php header('content-type: text/plain;charset=utf-8'); 



$s1 = '<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="UserType">
    <xsd:sequence>
    </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

';
$s2 = '<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element type="xsd:string" name="Field1"/>
    <xsd:element type="xsd:string" name="Field2"/>
    <xsd:element type="xsd:string" name="Field3"/>
</xsd:schema>
';


$ns = 'http://www.w3.org/2001/XMLSchema';

$doc = new DOMDocument();
$doc->loadXML($s1);
$seqElem = $doc->getElementsByTagNameNS($ns, "sequence")->item(0);

$d = new DOMDocument();
$d->loadXML($s2);
foreach ($d->getElementsByTagNameNS($ns, "*") as $e) {
    $seqElem->appendChild($doc->importNode($e));
}


echo $doc->saveXML();