views:

602

answers:

1

I'm running into a problem with axis2 and ajax. I'm getting xml from one of my web services with jQuery's ajax functions, and using this jquery plugin to transform the result xml to html.

Here's an example of the relevant xml that the service returns.

<ns:getPatientsByDoctorResponse>
    <ns:return type="com.emolst.jdbc.PatientBean">
        <ax23:firstName>Bryce</ax23:firstName>
        <ax23:lastName>Thompson</ax23:lastName>
    </ns:return>
</ns:getPatientsByDoctorResponse>

I looked through the xml Document object that I get from the jQuery ajax call, and it seems to have stripped the namespaces from the tags and made the tags all lowercase. However, I can't seem to get my xsl templates to recognize any of the tags.

Here's what I have now in the xsl.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <option>success1</option>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="//return">
        <option>success2</option>
        <option>
            <xsl:value-of select="firstname"/> <xsl:value-of select="lastname"/>
        </option>
    </xsl:template>
</xsl:transform>

The best I can get is the success1 option. I found some info here about making axis2 play nicer with ajax, but that looks like it might screw up the java service clients I have.

Here's the javascript in question.

$("select[name=patientlist]").transform({
    xml:{
        url:"/axis2/services/PatientService/getPatientsByDoctor",
        data {
            docKey: "6"
        },
        type:"GET",
        dataType:"xml"
    },
    xsl:"xsl/patients-option.xsl"
});

So am I doing something stupid or is there a better way to do this? Thanks for any help.

+1  A: 

You say that you think namespaces are gone, but I think they are not. Why should they?

Try a transformation that ignores namespaces, like this:

<xsl:transform 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <option>success1</option>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="//*[local-name()='return']">
        <option>success2</option>
        <option>
            <xsl:value-of select="*[local-name()='firstname']"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="*[local-name()='lastname']"/>
        </option>
    </xsl:template>

</xsl:transform>

or a template that uses them correctly, like this:

<xsl:transform 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ax23="........ax23 namespace here........"
  xmlns:ns="........ns namespace here........"
>

    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <option>success1</option>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="ns:return">
        <option>success2</option>
        <option>
            <xsl:value-of select="ax23:firstname"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="ax23:lastname"/>
        </option>
    </xsl:template>

</xsl:transform>
Tomalak
Awesome! The first solution works beautifully. I might try the second one later.
intoc
The second one with correct namespace usage might perform better on the long run. And it sure as hell is easier to manage. Of course you *can* write XPath expressions that are all local-name(), but it is neither fun nor very readable.
Tomalak