如何使用PHP从xsd文件中动态检索基于父节点的节点号 [英] How to retrieve node number based on parent node dynamically from xsd file using PHP

查看:135
本文介绍了如何使用PHP从xsd文件中动态检索基于父节点的节点号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从xsd文件中获取标签名称,并将其存储到数据库中,但无法使用php基于父节点分配引用号。
my XSD is

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>

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

有任何想法动态创建我的表格吗?

Is there any idea to create my table in this format dynamically?

帮助我解决这个问题。

提前感谢。

推荐答案

您可以尝试将元素的父节点的名称放在表的另一个字段中为parent_name

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

然后,您可以使用以下链接中提供的查询插入ref_num

And then you can insert the ref_num by using the query as given in the following link

MySQL Update Column from same column in the same table

这篇关于如何使用PHP从xsd文件中动态检索基于父节点的节点号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆