如何使用 PHP 从 xsd 文件动态检索基于父节点的节点号

How to retrieve node number based on parent node dynamically from xsd file using PHP

我从 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
.....

是否有想法以这种格式动态创建我的 table?

帮我解决这个问题。

提前致谢。

您可以尝试将元素的父节点名称放在 table 的另一个字段中,如 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
.....

然后您可以使用以下 link

中给出的查询插入 ref_num

MySQL Update Column from other column in same table

尝试在数组中追加根元素、子元素、子元素。搜索子子元素直到子元素的数量。 return MYSQL

中的数组

尝试使用以下代码获取每个元素的父节点并post将其存入数据库

$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));