我有2个xsd架构,我需要合并 F.e。
<?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>
我不需要从子文件解析到父节点,但我不知道如何从结果文件中删除它。
有人能帮帮我吗?!谢谢
答案 0 :(得分: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();