在PHP中解析XSD文件并在XSD中显示元素

Kev*_*vin 4 php xml xsd xml-parsing

我有一个XSD模式文件,我想解析并回显它包含的元素,如果可能的话,还可以显示每个元素所具有的子对象。我看过一些示例,其中最好的示例是: 在PHP中将XSD文件转换为数组

我试了一下,即使更改了xpaths和文件位置中的xs:部分,它也给了我一个空数组。有没有正确的方法来解析XSD文件并在PHP中显示其元素?

为了消除任何困惑,我想说的是:

可以说这是我正在尝试加载和显示其元素的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>
Run Code Online (Sandbox Code Playgroud)

如我们所见,有一个名称为“ shiporder”的元素,它有一些子元素,如果我没记错的话,它是具有字符串类型的“ orderperson”,还有一个“ shipto”,其子元素是:字符串类型的“”,“地址”,“城市”,“国家”。

我想做的就是简单地打印出“ shiporder”有孩子“ orderperson”和“ shipto”“ shipto”有孩子“ name”,“ address”,“ city”和“ country”。

我怎样才能做到这一点?我应该使用下面描述的方法吗:

<?php 
$attributes = array(); 
$xsdstring = "test.xsd"; 
$XSDDOC = new DOMDocument(); 
$XSDDOC->preserveWhiteSpace = false; 
if ($XSDDOC->load($xsdstring)) 
{ 
    $xsdpath = new DOMXPath($XSDDOC); 
    $attributeNodes = 
              $xsdpath-> 
              query('//xs:element[@name="shiporder"]')
              ->item(0); 
    foreach ($attributeNodes->childNodes as $attr) 
    { 
        $attributes[ $attr->getAttribute('value') ] = $attr->getAttribute('name'); 
    } 
    unset($xsdpath); 
} 
print_r($attributes); 
?>
Run Code Online (Sandbox Code Playgroud)

希望这使我的问题更加清楚,因为我是php和xml解析的新手。

kjh*_*hes 6

这个PHP:

<?php 

$xsdstring = <<<XML
<?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>
XML;

$doc = new DOMDocument();
$doc->loadXML(mb_convert_encoding($xsdstring, 'utf-8', mb_detect_encoding($xsdstring)));
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');

function echoElements($indent, $elementDef) {
  global $doc, $xpath;
  echo "<div>" . $indent . $elementDef->getAttribute('name') . "</div>\n";
  $elementDefs = $xpath->evaluate("xs:complexType/xs:sequence/xs:element", $elementDef);
  foreach($elementDefs as $elementDef) {
    echoElements($indent . "&nbsp;&nbsp;&nbsp;&nbsp;", $elementDef);
  }
}

$elementDefs = $xpath->evaluate("/xs:schema/xs:element");
foreach($elementDefs as $elementDef) {
  echoElements("", $elementDef);
}                       
?>
Run Code Online (Sandbox Code Playgroud)

产生以下输出:

shiporder
    orderperson
    shipto
        name
        address
        city
        country
    item
        title
        note
        quantity
        price
Run Code Online (Sandbox Code Playgroud)

按照要求。

请注意,这假设一个简单的Russian Doll XSD设计,该设计使用xs:complexType/xs:sequence/提供的shiporder定义及其子元素中提供的基本嵌入式结构。