0

I've been trying to convert my xml using xslt and it's driving me nuts.

Here is the XML,

<env:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<items xmlns="http://ws.apache.org/ns/synapse">
    <profiles>
        <candidate_details>
            <application>
                <app_id>ABC</app_id>
                <position_id>RR</position_id>
            </application>
        </candidate_details>
        <candidate_details>
            <application>
                <app_id>ABC2</app_id>
                <position_id>RR2</position_id>
            </application>
        </candidate_details>
    </profiles>
</items>
</env:Body>

And here is XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:env="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="no"/>  
<xsl:template match="env:Body/items/profiles">

    <xsl:for-each select="candidate_details">

            <application>  
                <app_id> <xsl:value-of select='application/app_id' /> </app_id>
            </application>

    </xsl:for-each> 

</xsl:template>

</xsl:stylesheet>

My expected output is

<application>
    <app_id>
        ABC
    </app_id>
</application>
<application>
    <app_id>
        ABC2
    </app_id>
</application>

It doesn't print out this format at all, but only the values, what am I doing wrong? Please help. Thanks!

PS. I'm aware that

<items xmlns="http://ws.apache.org/ns/synapse">

is invalid namespace, but that's how the xml is coming, so I have to use this xml as input.

R. A
  • 3
  • 2

1 Answers1

0

A few things need tweaking

  • Add all namespaces explicitly
  • Exclude them from the output
  • Capture the template at the root element

as follows:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:s="http://ws.apache.org/ns/synapse"
    exclude-result-prefixes="s env" 
>
<xsl:output  method="xml" omit-xml-declaration="yes"  indent="yes" />
<xsl:template match="/env:Body">
<xsl:for-each select="s:items/s:profiles/s:candidate_details">
<application>
    <app_id>
       <xsl:value-of select='s:application/s:app_id' /> 
    </app_id>
</application>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Stavr00
  • 3,219
  • 1
  • 16
  • 28