XML to XML Using XSLT without namespace

I’m able to convert One Xml into another when the input xml doesn’t have any namespace but I’m not able to get the desired output when the input xml has namespace.

Input.xml(W/O Namespace)

<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<address>
<addressee>John Smith</addressee>
<streetaddress>250 18th Ave SE</streetaddress>
<city>Rochester</city>
<state>MN</state>
<postalCode>55902</postalCode>
</address>
<address>
<addressee>Yogesh</addressee>
<streetaddress>Saligramam</streetaddress>
<city>Chennai</city>
<state>TAmil nadu</state>
<postalCode>600026</postalCode>
</address>
</addressbook>

Xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="EmployeeDetail">
<xsl:apply-templates select="addressbook/address"/>
</xsl:element>
</xsl:template>
<xsl:template match="addressbook/address">
<xsl:element name="Employee" >
<xsl:value-of select="concat(city,'-',addressee,'-',postalCode)"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Output.xml(W/O Namespace)

<?xml version="1.0" encoding="UTF-8"?><EmployeeDetail>
<Employee>Rochester-John Smith-55902</Employee>
<Employee>Chennai-Yogesh-600026</Employee>
</EmployeeDetail>

Input xml (With Namespace)

<?xml version="1.0" encoding="UTF-8"?>
<addressbook xlmns="http:abc.comBuspart">
<address>
<addressee>John Smith</addressee>
<streetaddress>250 18th Ave SE</streetaddress>
<city>Rochester</city>
<state>MN</state>
<postalCode>55902</postalCode>
</address>
<address>
<addressee>Yogesh</addressee>
<streetaddress>Saligramam</streetaddress>
<city>Chennai</city>
<state>TAmil nadu</state>
<postalCode>600026</postalCode>
</address>
</addressbook>

Output.xml for above input(with Namespace)

<?xml version="1.0" encoding="UTF-8"?></EmployeeDetail>

How can I retrieve the result with the above input(which has namespace)?


Source: xml

1 Comment

  1. Digin Dominic

    The following XSL works:


    This is because by adding a namespace to the input xml you can no longer refer to an XML element by local name alone, but you must also specify the namespace uri portion of the path (see http://en.wikipedia.org/wiki/XPath#Syntax_and_semantics_.28XPath_1.0.29)

    BTW, you also have a typo in your input xml: xlmns should be xmlns.

    Output:

    Rochester-John Smith-55902
    Chennai-Yogesh-600026

    Reply

Leave a Reply