我从xsd文件中获取了标记名称,并将其存储到数据库中,但是无法使用php根据父节点分配引用号。 我的XSD是
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>
我的PHP代码是
<?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);
}
?>
我期待的是
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
.....
有任何想法动态地以这种格式创建我的表吗?
帮我解决这个问题。
提前致谢。
答案 0 :(得分:0)
您可以尝试将元素的父节点的名称放在表的另一个字段中作为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
.....
然后您可以使用以下链接中给出的查询插入ref_num
答案 1 :(得分:0)
尝试在数组中追加root,子元素,子子元素。搜索子子元素,直到子元素的数量。在MYSQL中返回数组
答案 2 :(得分:0)
尝试使用以下代码获取每个元素的父节点并将其发布到数据库中
$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));