sarma
sarma

Reputation: 63

xml to xml transformation - trying to merge two XSLs

i am trying to transform one xml into another using an xsl.

  1. change input XML's 'name' attribute's value from 'Code' to 'id'
  2. change input XML's 'name' attribute's value from 'Name' to 'name'

  3. copy all nodes under json:array in outpur xml under json:object as shown in the output xml

I am able to achieve what i have needed using two separate XSLs and trying to figure how I can merge those two XSLs, can you please review and assist me

xsl1:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dp="http://www.datapower.com/extensions" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="dp date">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@name[.='Name']">
        <xsl:attribute name="name"><xsl:value-of select="'name'"/></xsl:attribute>
    </xsl:template>
    <xsl:template match="@name[.='Code']">
        <xsl:attribute name="name"><xsl:value-of select="'id'"/></xsl:attribute>
    </xsl:template>     
    <xsl:template match="@name[.='SimpleCarrier']">
        <xsl:variable name="carrierType">
            <xsl:value-of select="'airlines'"/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="$carrierType='airlines'">
                <xsl:attribute name="name"><xsl:value-of select="'airlines'"/></xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="name"><xsl:value-of select="'SimpleCarrierNotUpdated'"/></xsl:attribute>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template> 
</xsl:stylesheet>

xsl2:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dp="http://www.datapower.com/extensions" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="dp date">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>      
    <xsl:template match="/">
        <xsl:copy-of select="//json:array"/>
    </xsl:template>     
</xsl:stylesheet>

input XML:

<json:object xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <json:object name="Header">
        <json:string name="Action">http://webs.abcd.com/CService</json:string>
        <json:string name="RelatesTo">urn:uuid:9455ee68-bc4d-4e6a-9174-fb2000c18e24</json:string>
    </json:object>
    <json:object name="Body">
        <json:object name="GetSimpleCLResponse">
            <json:object name="GetSimpleCLResult">
                <json:array name="SimpleCarrier">
                    <json:object>
                        <json:string name="Code">m9</json:string>
                        <json:string name="Name">1B9FHQK</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="Code">25</json:string>
                        <json:string name="Name">1TIME</json:string>
                    </json:object>                  
                </json:array>
            </json:object>
        </json:object>
    </json:object>
</json:object>

Output XML:

<json:object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd">
    <json:array name="airlines">
        <json:object>
            <json:string name="id">m9</json:string>
            <json:string name="name">1B9FHQK</json:string>
        </json:object>      
        <json:object>
            <json:number name="id">25</json:number>
            <json:string name="name">1TIME</json:string>
        </json:object>      
    </json:array>
</json:object>

Upvotes: 0

Views: 53

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

AFAICT, all you need to do is add:

<xsl:template match="/">
    <xsl:apply-templates select="//json:array"/>
</xsl:template>

to the first stylesheet.

Upvotes: 1

Related Questions