问题
I took the tag names from xsd file and also stored into database but am not able to assign the reference number based on the parent node using php. my XSD is
sample.xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
My PHP code is
<?php
$xsdstring ="sample.xsd";
$doc = new DOMDocument();
$doc->preserveWhitespace = false;
$xsdstring = $doc->load($xsdstring);
$doc->loadXML(mb_convert_encoding($xsdstring, 'utf-8', mb_detect_encoding($xsdstring)));
$xpath = new DOMXPath($doc);
$mysql_hostname = "localhost"; // Example : localhost
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "sample_db";
$dbh = new PDO("mysql:dbname={$mysql_database};host={$mysql_hostname};port=3306", $mysql_user, $mysql_password);
$num=1;
function echoElements($indent, $elementDef) {
global $doc, $xpath,$elements,$dbh,$num,$sql;
$elements = $indent . $elementDef->getAttribute('name') ."\n";
$elementDefs = $xpath->evaluate("xs:complexType/xs:sequence/xs:element", $elementDef);
$sql = $dbh->prepare("INSERT INTO `test` (`name`,ref_num) VALUES (?,?)");
$sql->execute(array($elements ,$num));
foreach($elementDefs as $elementDef) {
$test=echoElements("" , $elementDef);
}
}
$elementDefs = $xpath->evaluate("/xs:schema/xs:element");
foreach($elementDefs as $elementDef) {
echoElements("", $elementDef);
}
?>
what I expect is
id name ref_num
1 shiporder 0 //refers that it is root node
2 orderperson 1 //refers the id of its parent node(shiporder)
3 shipto 1 //refers the id of its parent node(shiporder)
4 name 3 //refers the id of its parent node(shipto)
5 address 3
.....
Is there any idea to create my table in this format dynamically?
Help me to Solve this.
Thanks in advance.
回答1:
You can try to put the names of the parent nodes of an element in another field of the table as parent_name
id name parent_name ref_num
1 shiporder root 0
2 orderperson shiporder 1
3 shipto shiporder 1
4 name shipto 3
5 address shipto 3
.....
And then you can insert the ref_num by using the query as given in the following link
MySQL Update Column from other column in same table
回答2:
Try to append root, child elements, sub child elements in an array. Search sub child elements until the number of child elements. return the array in MYSQL
回答3:
Try using the following code to get the parent node of each element and post it into the database
$elements = $indent . $elementDef->getAttribute('name') ."\n";
$parent = $elements->xpath("parent::*"); // or $elements->xpath( '..' );
$elementDefs = $xpath->evaluate("xs:complexType/xs:sequence/xs:element", $elementDef);
$sql = $dbh->prepare("INSERT INTO `test` (`name`,ref_num,`parent_node`) VALUES (?,?,?)");
$sql->execute(array($elements,$num,$parent));
来源:https://stackoverflow.com/questions/35865892/how-to-retrieve-node-number-based-on-parent-node-dynamically-from-xsd-file-using