Java, XSLT: How to handle the dynamic namespaces generated by Axis in XSLT

javaxslt

I have an application that calls a web service (axis based) to get a response in the following format:

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <axis2ns394:ServiceLevelDetailsResponse xmlns:axis2ns394="urn:myco:com:mapping:service:3.0">
            <ns1:errorFlag xmlns:ns1="urn:myco:com:mapping:service:3.0">false</ns1:errorFlag>
            <axis2ns394:customerProgram>
                <axis2ns394:name>ABC</axis2ns394:name>
                <axis2ns394:description>ABC SERVICES</axis2ns394:description>
                <axis2ns394:programRank>1</axis2ns394:programRank>
                <axis2ns394:bindOptions>
                    <axis2ns394:name>PRO1A</axis2ns394:name>
                    <axis2ns394:description>Complete Home Solution</axis2ns394:description>
                    <axis2ns394:programName>PROMO</axis2ns394:programName>
                    <axis2ns394:programDescription>Promotional Bundle 1A</axis2ns394:programDescription>
                </axis2ns394:bindOptions>
                <axis2ns394:bindOptions>
                    <axis2ns394:name>PRO2A</axis2ns394:name>
                    <axis2ns394:description>Buy 1 Get 1 Free</axis2ns394:description>
                    <axis2ns394:programName>PROMO</axis2ns394:programName>
                    <axis2ns394:programDescription>Promotional Bundle 2A</axis2ns394:programDescription>
                </axis2ns394:bindOptions>
            </axis2ns394:customerProgram>
        </axis2ns394:ServiceLevelDetailsResponse>
    </soapenv:Body>
</soapenv:Envelope>


Here – the namespace 'axis2ns394' is generated dynamically at the runtime. I need to write an XSLT to flatten this XML to a simpler XML (Say one html table with one row for each 'customerProgram'). But I am not sure how to handle these dynamic namespaces.

Any help will be greatly appreciated.

Regards,
– Ashish

Best Answer

Is the namespace dynamic, or the namespace prefix dynamic?

The actual namespace for the XML is urn:myco:com:mapping:service:3.0

So, an example stylesheet to transform the results doesn't need to know what the namespace prefix is, but what the Namespace is. You can use a different namespace prefix in your XSLT and it will match on the elements, as long as you declare the namespace correctly.

For example this stylesheet declaring the namespace prefix myco for the namespace urn:myco:com:mapping:service:3.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myco="urn:myco:com:mapping:service:3.0"
>

<xsl:template match="/">
<table border="1">
<thead>
<tr>
 <th>Name</th>
 <th>Description</th>
 <th>Program Rank</th>
</tr>
</thead>
  <xsl:apply-templates select="//myco:customerProgram" />
</table>
</xsl:template>

<xsl:template match="myco:customerProgram">
<tr>
  <td>
   <xsl:value-of select="myco:name" />
  </td>
 <td>
   <xsl:value-of select="myco:description" />
  </td>
 <td>
   <xsl:value-of select="myco:programRank" />
  </td>

</tr>
</xsl:template>
</xsl:stylesheet>

Would produce:

<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Program Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td>ABC</td>
<td>ABC SERVICES</td>
<td>1</td>
</tr>
</tbody>
</table>