Umaimath Thonikkadavan
Umaimath Thonikkadavan

Reputation: 133

XSLT How to add namespace prefix in elements inside template

I am converting an XML to another using xslt, I have the namespace prefix for all tags, for example "ns9". In some places I am calling a template to generate the element within, where I am missing the namespace prefix. I tried to add namespace in the template, but it does not reflect.

 <xsl:call-template name="split">
     <xsl:with-param name="list" select="FlightReferences"/>
     <xsl:with-param name="delimiter" select="' '"/>
   <xsl:with-param name="tag" select="'PaxJourneyRefID'"/>
</xsl:call-template>

The response is as below, for the tag PaxJourneyRefID namespace prefix is missing.

 <ns9:OriginDest>
   <ns9:DestCode>DXB</ns9:DestCode>
   <ns9:OriginCode>LHR</ns9:OriginCode>
   <ns9:OriginDestID>LHRDXB</ns9:OriginDestID>
  <PaxJourneyRefID>Flight1</PaxJourneyRefID>
</ns9:OriginDest>

Tried adding name space in the template declaration,

<xsl:template name="split">
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:param name="tag"/>
<xsl:choose>
    <xsl:when test="contains($list, $delimiter)">
        <xsl:element name="{$tag}" xmlns:ns9="http://www.example.com/IATA/2015/00/2018.2/Sample" >
            <xsl:value-of select="substring-before($list,$delimiter)"/>
        </xsl:element>
        <xsl:call-template name="split">
            <xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
            <xsl:with-param name="delimiter" select="$delimiter"/>
            <xsl:with-param name="tag" select="$tag"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:choose>
            <xsl:when test="$list = ''">
                <xsl:text/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{$tag}">
                    <xsl:value-of select="$list"/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:otherwise>
</xsl:choose>

Upvotes: 0

Views: 478

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

You can use a prefix in xsl:element name e.g. <xsl:element name="ns9:{$tag}">.

Upvotes: 1

Related Questions