php - 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 语句来处理,但到目前为止我的尝试都惨遭失败:
可以提出一些提示来完成任务吗?
解决方案
推荐阅读
- java - 如何使用 Google Cloud Dataflow 增加 Apache Beam 管道工作程序的线程堆栈大小?
- google-cloud-storage - 尽管已登录,但无法从 Python 连接到 GCS 存储桶
- php - symfony 中的教义协会
- gradle - 如何为 Gradle 5.6.2+ 的全局 init.gradle pluginManagement 提供凭据?
- python - python中的平滑直方图
- python - 如何在python中检查路径/文件是否存在?
- r - 如何在 R 中使用长数据进行箱线图
- json - Swift IOS 无法使用 Codable 访问 json 数组数据
- android - windows10无法运行mksdcard.exe SDK工具android v3.5.2
- mysql - 使用另一个表的主键创建一个表,该表由多个列组成