How do you get XML::Pastor to set xsi:type for programmatically generated elements?

Posted by Derrick on Stack Overflow See other posts from Stack Overflow or by Derrick
Published on 2010-06-16T00:26:10Z Indexed on 2010/06/16 0:32 UTC
Read the original article Hit count: 324

Filed under:
|
|
|

I'm learning how to use Perl as an automation test framework tool for a Java web service and running into trouble generating xml requests from the Pastor generated modules. The problem is that when including a type that extends from the required type for an element, the xsi:type is not included in the generated xml string. Say, for example, I want to generate the following xml request from the modules that XML::Pastor generated from my xsd:

<?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>

What I'm getting currently is this:

<?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>

Here are some relavent snippets from the 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>

And here are relavent parts of the script:

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, $pulseTextPromptAnswer);
push(@promptAnswers, $textPromptAnswer);

I haven't seen this addressed anywhere in the documentation for the XML::Pastor modules, so if anyone can point me at a good reference for its use it would be greatly appreciated. Also, I'm only using XML::Pastor because I don't know of any other modules that can do this, so if any of you know of something either easier to use, or more well maintained, please let me know about that too!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about perl