Web service example 6: Transforming attributes to dhild elements

Previous Next

When exporting USoft application data, for an RPC-oriented method it is recommended to use child elements instead of attributes for complex type parameters. The reason why is that some client applications may not support or properly recognize attributes for those RPC-calls.

So, the output must have the following basic structure:

<?xml version="1.0" encoding="UTF-8"?>

<Scheduled_Tours>

<SCHEDTOUR>

<SCHEDTOUR_ID>50</SCHEDTOUR_ID>

<DESTINATION>AUSTRALIA</DESTINATION>

<TOUR_TYPE>ISLAND SUNTANNER</TOUR_TYPE>

<START_DATE>2005-11-01000000</START_DATE>

<RETURN_DATE>2005-11-13000000</RETURN_DATE>

<PRICE>120</PRICE>

<MAX_PART>10</MAX_PART>

<GUIDE>137</GUIDE>

</SCHEDTOUR>

<SCHEDTOUR>

<SCHEDTOUR_ID>51</SCHEDTOUR_ID>

<DESTINATION>AUSTRALIA</DESTINATION>

<TOUR_TYPE>ISLAND SUNTANNER</TOUR_TYPE>

<START_DATE>2005-11-15000000</START_DATE>

<RETURN_DATE>2005-11-27000000</RETURN_DATE>

<PRICE>122</PRICE>

<MAX_PART>5</MAX_PART>

<GUIDE>6</GUIDE>

</SCHEDTOUR>

</Scheduled_Tours>

By default, the XML export method results in an XML document with attributes, like:

<Scheduled_Tours documentName="Scheduled Tours">

<SCHEDTOUR SCHEDTOUR_ID="50" DESTINATION="AUSTRALIA" TOUR_TYPE="ISLAND SUNTANNER" START_DATE="2005-11-01000000" RETURN_DATE="2005-11-13000000" PRICE="120" MAX_PART="10" GUIDE="137" />

<SCHEDTOUR SCHEDTOUR_ID="51" DESTINATION="AUSTRALIA" TOUR_TYPE="ISLAND SUNTANNER" START_DATE="2005-11-15000000" RETURN_DATE="2005-11-27000000" PRICE="122" MAX_PART="5" GUIDE="6" />

</Scheduled_Tours>

To achieve a document structure containing child elements, an XSL transformation is needed to transform the default output of the xml export method. This XSL transformation is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="Scheduled_Tours">

     <xsl:copy>

        <xsl:attribute name="arrayType" namespace="http://schemas.xmlsoap.org/soap/encoding/">

        <xsl:text>Array:ScheduledTour[</xsl:text>

        <xsl:value-of select="count(child::*)"/>

        <xsl:text>]</xsl:text>

        </xsl:attribute>

        <xsl:apply-templates select="node() | @*"/>

     </xsl:copy>

    </xsl:template>

    <xsl:template match="/ | node()">

        <xsl:copy>

            <xsl:apply-templates select="node() | @*"/>

        </xsl:copy>

    </xsl:template>

    <xsl:template match="@documentName"/>

    <xsl:template match="@*">

        <xsl:element name="{name()}">

            <xsl:value-of select="."/>

        </xsl:element>

    </xsl:template>

</xsl:stylesheet>

You can store this XSL transformation in a file, for example: att2childelts.xsl, and then execute the following statement to get an output containing child elements:

INVOKE USXSL.apply

WITH

SELECT

(    

    INVOKE    xml.export WITH

    SELECT    * 

    FROM      schedtour

    WHERE     destination = 'AUSTRALIA') XmlDocument

    ,        'att2childelts.xsl'

)