如何使用XML :: Compile :: SOAP创建元素数组

时间:2011-09-14 19:31:04

标签: xml perl soap

所以我试图创建的基本XML就是这个(为了简洁而删除了标题)。

<SOAP-ENV:Body><data:requestMessage xmlns:data="urn:schemas-cybersource-com:transaction-data-1.62">
<data:merchantID>obfuscated</data:merchantID>
<data:merchantReferenceCode>42</data:merchantReferenceCode>
<data:billTo><data:firstName>caleb</data:firstName>
   <data:lastName>cushing</data:lastName><data:street1>somewhere</data:street1>
   <data:city>houston</data:city>
   <data:state>TX</data:state>
   <data:postalCode>77064</data:postalCode>
   <data:country>US</data:country>
   <data:email>xenoterracide@gmail.com</data:email>
</data:billTo>
 <data:item id="0">
   <data:unitPrice>2.01</data:unitPrice>
   <data:quantity>1</data:quantity>
</data:item>
<data:item id="1">
   <data:unitPrice>3</data:unitPrice>
   <data:quantity>1</data:quantity>
</data:item>
<data:purchaseTotals>
   <data:currency>USD</data:currency>
   <data:grandTotalAmount>5.01</data:grandTotalAmount>
</data:purchaseTotals><data:card>
    <data:accountNumber>4111111111111111</data:accountNumber>
    <data:expirationMonth>12</data:expirationMonth>
    <data:expirationYear>2015</data:expirationYear>
</data:card><data:ccAuthService run="true"/>
</data:requestMessage></SOAP-ENV:Body>

这是Perl

#!/usr/bin/perl
use v5.14;
use strict;
use warnings;
use Env qw( CYBS_ID CYBS_KEY );
use XML::Compile::Util qw( pack_type type_of_node );
use XML::Compile::Cache;
use XML::Compile::SOAP::WSS;

use XML::Compile::WSDL11;
use XML::Compile::SOAP11;
use XML::Compile::Transport::SOAPHTTP;

my $wss = XML::Compile::SOAP::WSS->new( version => '1.1' );


my $wsdl = XML::Compile::WSDL11->new( 'CyberSourceTransaction_1.62.wsdl' );

$wsdl->importDefinitions( 'CyberSourceTransaction_1.62.xsd' );

my $call = $wsdl->compileClient('runTransaction');

my $security = $wss->wsseBasicAuth( $CYBS_ID, $CYBS_KEY );

my ( $answer, $trace ) = $call->(
    wsse_Security         => $security,
    merchantID            => $CYBS_ID,
    merchantReferenceCode => '42',
    billTo => {
        firstName  => 'caleb',
        lastName   => 'cushing',
        street1    => 'somewhere',
        city       => 'houston',
        state      => 'TX',
        postalCode => '77064',
        country    => 'US',
        email      => 'xenoterracide@gmail.com',
    },
    item => {
        id => 0,
        quantity => 1,
        unitPrice => 2.01,
    },
    item => {
        id => 1,
        quantity => 1,
        unitPrice => 3.00,
    },
    purchaseTotals => {
        currency         => 'USD',
        grandTotalAmount => 5.01,
    },
    card => {
        accountNumber => '4111111111111111',
        expirationMonth => '12',
        expirationYear  => '2015',
    },
    ccAuthService => {
        run => 'true',
    },
);

$trace->printRequest;
say '---';
$trace->printResponse;

Here are the WSDL and XSD

以下是正在生成的XML。显然我只得到第二项。如何创建包含多个项目的请求?

<SOAP-ENV:Body><data:requestMessage xmlns:data="urn:schemas-cybersource-com:transaction-data-1.62">
<data:merchantID>obfuscated</data:merchantID>
<data:merchantReferenceCode>42</data:merchantReferenceCode>
<data:billTo><data:firstName>caleb</data:firstName>
   <data:lastName>cushing</data:lastName><data:street1>somewhere</data:street1>
   <data:city>houston</data:city>
   <data:state>TX</data:state>
   <data:postalCode>77064</data:postalCode>
   <data:country>US</data:country>
   <data:email>xenoterracide@gmail.com</data:email>
</data:billTo>
 <data:item id="1">
   <data:unitPrice>3</data:unitPrice>
   <data:quantity>1</data:quantity>
</data:item>
<data:purchaseTotals>
   <data:currency>USD</data:currency>
   <data:grandTotalAmount>5.01</data:grandTotalAmount>
</data:purchaseTotals><data:card>
    <data:accountNumber>4111111111111111</data:accountNumber>
    <data:expirationMonth>12</data:expirationMonth>
    <data:expirationYear>2015</data:expirationYear>
</data:card><data:ccAuthService run="true"/>
</data:requestMessage></SOAP-ENV:Body>

1 个答案:

答案 0 :(得分:3)

您尝试在HASH中使用多个具有相同名称的键,这当然是不可能的。使用$ wsdl-&gt; explain($ op,recurse =&gt; 1),您可以轻松找出数据结构的外观......

对于简单的重复元素,使用和数组:

  item => [ { id => 0, quantity => 1, unitPrice => 2.01, },
            { id => 1, quantity => 1, unitPrice => 3.00, }, ]

一些好的:

 $items[0] = { id => 0, quantity => 1, unitPrice => 2.01, };
 $items[1] = { id => 1, quantity => 1, unitPrice => 3.00, };
 $data = { ..., item => @items, ... };

当项目是对象时更容易...请参阅typemap。