3

我正在学习如何使用 Perl 作为 Java Web 服务的自动化测试框架工具,并且在从 Pastor 生成的模块生成 xml 请求时遇到了麻烦。问题是,当包含从元素所需类型扩展的类型时,xsi:type 不包含在生成的 xml 字符串中。例如,我想从 XML::Pastor 从我的 xsd 生成的模块生成以下 xml 请求:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PromptAnswersRequest xmlns="http://mycompany.com/api">
    <Uri>/some/url</Uri>
    <User ref="1"/>
    <PromptAnswers>
        <PromptAnswer xsi:type="textPromptAnswer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <Prompt ref="2"/>
            <Children>
                <PromptAnswer xsi:type="choicePromptAnswer">
                    <Prompt ref="1"/>
                    <Choice ref="2"/>
                </PromptAnswer>
            </Children>
            <Value>totally</Value>
        </PromptAnswer>
    </PromptAnswers>
</PromptAnswersRequest>

我目前得到的是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PromptAnswersRequest xmlns="http://mycompany.com/api">
    <Uri>/some/url</Uri>
    <User ref="1"/>
    <PromptAnswers>
        <PromptAnswer>
            <Prompt ref="2"/>
            <Children>
                <PromptAnswer>
                    <Prompt ref="1"/>
                    <Choice ref="2"/>
                </PromptAnswer>
            </Children>
            <Value>totally</Value>
        </PromptAnswer>
    </PromptAnswers>
</PromptAnswersRequest>

以下是来自 xsd 的一些相关片段:

<xs:complexType name="request">
    <xs:sequence>
        <xs:element name="Uri" type="xs:anyURI"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="promptAnswersRequest">
    <xs:complexContent>
        <xs:extension base="api:request">
            <xs:sequence>
                <xs:element name="User" type="api:ref"/>
                <xs:element name="PromptAnswers" type="api:promptAnswerList"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="promptAnswerList">
    <xs:sequence>
        <xs:element name="PromptAnswer" type="api:promptAnswer" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="promptAnswer" abstract="true">
    <xs:sequence>
        <xs:element name="Prompt" type="api:ref"/>
        <xs:element name="Children" type="api:promptAnswerList" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="textPromptAnswer">
    <xs:complexContent>
        <xs:extension base="promptAnswer">
            <xs:sequence>
                <xs:element name="Value" type="api:nonEmptyString" minOccurs="0"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

以下是脚本的相关部分:

my $promptAnswerList = new My::API::Type::promptAnswerList;
my @promptAnswers;

my $promptAnswerList2 = new My::API::Type::promptAnswerList;
my @textPromptAnswerChildren;

my $textPromptAnswer = new My::API::Type::textPromptAnswer;
my $textPromptAnswerRef = new My::API::Type::ref;
$textPromptAnswerRef->ref('2');
$textPromptAnswer->Prompt($textPromptAnswerRef);
my $choicePromptAnswer = new My::API::Type::choicePromptAnswer;
my $choicePromptAnswerPromptRef = new My::API::Type::ref;
my $choicePromptAnswerChoiceRef = new My::API::Type::ref;
$choicePromptAnswerPromptRef->ref('1');
$choicePromptAnswerChoiceRef->ref('2');
$choicePromptAnswer->Prompt($choicePromptAnswerPromptRef);
$choicePromptAnswer->Choice($choicePromptAnswerChoiceRef);

push(@textPromptAnswerChildren, $choicePromptAnswer);
$promptAnswerList2->PromptAnswer(@textPromptAnswerChildren);
$textPromptAnswer->Children($promptAnswerList2);
$textPromptAnswer->Value('totally');

push(@promptAnswers, $textPromptAnswer);

我没有在 XML::Pastor 模块的文档中的任何地方看到这个问题,所以如果有人能指出我使用它的一个很好的参考,我将不胜感激。另外,我只使用 XML::Pastor 因为我不知道任何其他模块可以做到这一点,所以如果你们中的任何人知道更容易使用或维护得更好的东西,请告诉我也!

4

0 回答 0