首页 > 解决方案 > php 7 从 xsd 构建 xml

问题描述

我正在尝试使用eBaysvc.xsd不仅验证 xml,而且构建 xml 本身:
传递 API_Name

添加争议

我想检索以下 xml:

<?xml version="1.0" encoding="utf-8"?>
<AddDisputeRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <DisputeExplanation> Yes:DisputeExplanationCodeType:BuyerHasNotResponded|BuyerRefusedToPay|BuyerNotClearedToPay|... </DisputeExplanation>
  <DisputeReason> Yes:DisputeReasonCodeType:BuyerHasNotPaid|TransactionMutuallyCanceled|... </DisputeReason>
  <ItemID> Conditionally:ItemIDType:string </ItemID>
  <TransactionID> Conditionally:string: </TransactionID>
  <OrderLineItemID> Conditionally:string: </OrderLineItemID>
</AddDisputeRequest>

我构建了一个适用于简单调用的脚本,但对于具有嵌套 ItemTypes 的 API,它需要递归,并且我构建的方式不会返回可靠的响应:

$file = 'ebaysvc.xsd';
$xmlDoc = new DOMDocument('1.0','UTF-8');
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->load($file);
$xpath = new DomXpath($xmlDoc);
$xpath->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
$xpath->registerNamespace('ebl', 'urn:ebay:apis:eBLBaseComponents');

$xml='<?xml version="1.0" encoding="utf-8"?>';
$xml.='<'.$API.'Request xmlns="urn:ebay:apis:eBLBaseComponents">';

$Parameters = $xpath->query('//xs:complexType[@name="'.$API.'RequestType"]/xs:complexContent/xs:extension/xs:sequence')->item(0);

foreach($Parameters->childNodes as $node)
{
  $Param_name=$node->getAttribute('name');
  $Param_type=str_replace(['ns:','xs:'],'',$node->getAttribute('type'));
  $RequiredInput = $xpath->query('xs:annotation/xs:appinfo/ebl:CallInfo/ebl:RequiredInput',$node)->item(0)->nodeValue;

  $Param_Type=$RequiredInput.':'.$Param_type.':';

  switch($Param_type)
  {
    case 'string':
    case 'dateTime':
    case 'int':
    case 'long':
    case 'double':
    case 'boolean':
    case 'anyURI':
    break;
    default:
      $Tokens=$xpath->query('//xs:simpleType[@name="'.$Param_type.'"]/xs:restriction')->item(0);
      if (!is_null($Tokens))
      {
        switch($Tokens->getAttribute('base'))
        {
          case 'xs:string':
              $Param_Type.= ' ('.str_replace(['ns:','xs:'],'',$Tokens->getAttribute('base')).')';
          break;
          case 'xs:token':
            foreach($Tokens->childNodes as $token)
            {
              $Param_Type.= $token->getAttribute('value').'|';
            }
          break;
        }
      }
      else
      {
        $Tokens=$xpath->query('//xs:complexType[@name="'.$Param_type.'"]/xs:sequence')->item(0);
        foreach($Tokens->childNodes as $subnode)
        {
          $SubParam_name=$subnode->getAttribute('name');
          $SubParam_type=str_replace(['ns:','xs:'],'',$subnode->getAttribute('type'));

          switch($SubParam_type)
          {
            case 'string':
            case 'dateTime':
            case 'int':
            case 'long':
            case 'double':
            case 'boolean':
            case 'anyURI':
            break;
            default:
                ....

此外,递归不应通过嵌套的 if/for 语句来处理,但到目前为止我的尝试都惨遭失败:

可以提出一些提示来完成任务吗?

标签: phpxmlxsddomdocument

解决方案


推荐阅读