Reputation: 41
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.com\Buspart">
<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)?
Upvotes: 2
Views: 90
Reputation: 119
Try this in your xpath
select="*[local-name(.) ='addressbook']/*[local-name(.)='address']"
the full solution :
<?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="*[local-name(.) ='addressbook']/*[local-name(.)='address']" />
</xsl:element>
</xsl:template>
<xsl:template match="*[local-name(.) ='addressbook']/*[local-name(.)='address']">
<xsl:element name="Employee" >
<xsl:value-of select="concat(*[local-name(.) ='city'],'-',*[local-name(.)='addressee'],'-',*[local-name(.)='postalCode'])"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 31770
The following XSL works:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="http:\\abc.com\Buspart">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="EmployeeDetail">
<xsl:apply-templates select="/f:addressbook/f:address"/>
</xsl:element>
</xsl:template>
<xsl:template match="/f:addressbook/f:address">
<xsl:element name="Employee" >
<xsl:value-of select="concat(f:city, '-', f:addressee, '-', f:postalCode)"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
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:
<EmployeeDetail>
<Employee>Rochester-John Smith-55902</Employee>
<Employee>Chennai-Yogesh-600026</Employee>
</EmployeeDetail>
Upvotes: 0