2015-09-24 2 views
1

У меня есть 2 схемы xsd, которые мне нужно объединить F.e.Как слить 2 схемы xsd

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

<?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 type="xsd:string" name="Field1"/> 
       <xsd:element type="xsd:string" name="Field2"/> 
       <xsd:element type="xsd:string" name="Field3"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:schema> 

Я пытаюсь объединить их и написать следующий код, который объединит 2 xsd-файла в один и напечатает результат.

$DOMChild = new \DOMDocument; 
$DOMChild->loadXML($child); 
$node = $DOMChild->documentElement; 

$DOMParent = new \DOMDocument; 
$DOMParent->formatOutput = true; 
$DOMParent->loadXML($parent); 

$node = $DOMParent->importNode($node, true); 

if ($tag !== null) { 
    $tag = $DOMParent->getElementsByTagName($tag)->item(0); 
    $tag->appendChild($node); 
} else { 
    $DOMParent->documentElement->appendChild($node); 
} 

echo $DOMParent->saveXML(); 

Результат этого следующий:

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

Я не нужно разобрать из детского файла для родительского узла, но я dont'n знаю, как он может быть удален из результирующего файла.

Может ли кто-нибудь помочь мне ?! Спасибо

ответ

1

Кажется, я нашел решение

$first = new \DOMDocument("1.0", 'UTF-8'); 
$first->formatOutput = true; 
$first->loadXML($parent); 

$second = new \DOMDocument("1.0", 'UTF-8'); 
$second->formatOutput = true; 
$second->loadXML($child); 
$second = $second->documentElement; 

foreach($second->childNodes as $node) 
{ 
    $importNode = $first->importNode($node,TRUE); 
    $first->documentElement->appendChild($importNode); 
} 
echo $first->saveXML(); 
+0

Что такое обоснование третьего документа XML ('$ xml') в конце? Не должно ли, например, '$ first' именно это? – hakre

+0

Да. Ты прав. Это не нужно. Я удалил это – Dmitry