0

The below xml is not transforming because of xmlns, I tried without xmlns, it working as expected. but i am receiving input with xmlns. Please suggest how can i overcome it.

Requirement: To retrieve productBenefitHeaders from the xml.

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<productData xmlns="http://www.example.org/consolidated">
<productResponse>
    <product>
        <status>
            <isError>false</isError>
        </status>
        <productBenefit>
            <productBenefitCategory>CARDs</productBenefitCategory>
            <productBenefitId>12AA</productBenefitId>
            <productBenefitHeader>Philips</productBenefitHeader>
        </productBenefit>
       <productBenefit>
            <productBenefitCategory>CARDs</productBenefitCategory>
            <productBenefitId>12AB</productBenefitId>
            <productBenefitHeader>Samsung</productBenefitHeader>
        </productBenefit> 
    </product>  
</productResponse>  
<productData>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:template match="/">
<xsl:for-each select="productData/productResponse/product/productBenefit">        
 <xsl:value-of select="productBenefitHeader"/>
  </xsl:for-each>   
</xsl:template>
</xsl:stylesheet>
Raj550
  • 19
  • 9

1 Answers1

1

The elements in the source xml are in the namespace http://www.example.org/consolidated. While you are searching for elements without specifying the namespace.

To search with the namespace you need to add the namespace in the stylesheet tag and set a prefix for it, in this case I used 'pref'.

xmlns:pref="http://www.example.org/consolidated"

Now you can use the prefix in your xsl while specifying the elements you are looking for. This is your xsl but with the prefix added.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:pref="http://www.example.org/consolidated">
 <xsl:output method="html" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
  <xsl:template match="/">
     <xsl:for-each select="pref:productData/pref:productResponse/pref:product/pref:productBenefit">        
     <xsl:value-of select="pref:productBenefitHeader"/>
   </xsl:for-each>   
 </xsl:template>
</xsl:stylesheet>

Also, make sure your end tag is correct. Currently the last tag in your example xml is not a closing tag.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
ophychius
  • 2,623
  • 2
  • 27
  • 49