Thursday, November 20, 2008

XSLT Tip: Omit input xml namespace


In XSLT whenever the Input XML has some 'xmlns' in its node, the normal XSLT path identifier will not work. So the XSLT should declare the input xml's 'xmlns' in itself with a prefix and then should be accessed. (e.g)
Input xml:
<ArrayOfCatalogItem>
  <CatalogItem>
    <ID xmlns="http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices">6ca7b76b-ed36-4128-9f84-e275fbf9</ID>
    <Name xmlns="http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices">Ad Serving</Name>
    <Path xmlns="http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices">/MIS/Report/Ad Serving</Path>    
  </CatalogItem>
</ArrayOfCatalogItem>

In XSLT, declare the xmlns in XSLT like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rs="http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices" exclude-result-prefixes="rs" >

Note the word: xmlns:rs="http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices"

And to access the value of any node(here I use 'Name') use this:
<xsl:value-of select="rs:Name" disable-output-escaping="yes"/>

Note the word: "rs:Name"

No comments:

Post a Comment