0

The following XSLT perfectly selects nodes from the given XML when the namespace xmlns="http://tempuri.org/BaseSchema" is not present.

Issue : When the namespace xmlns="http://tempuri.org/BaseSchema" is present in the XML, as is the case for the real problem, the XSLT selects everything from the XML and not the nodes as mentioned in XSLT.

The input file cannot be modified to remove the namespace. Does anyone know how to ignore or suppress the effect of the namespace.

XML

<event xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/BaseSchema" >
    <name>Pancake</name>
    <categories>
    <category>community</category>
     </categories>
     <venue>
    <name>fatzcafe</name>
        <city>NYC</city>
    <venue_type>
             <name>Lounge</name>
             <cuisine>Thai</cuisine>
        </venue_type>
      </venue>
</event>

XSLT

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/BaseSchema">
 <xsl:output method="text" encoding="UTF-8" />

  <xsl:template match="/event">
     <xsl:apply-templates select="venue/venue_type"/>
  </xsl:template>

  <xsl:template match="/venue_type">
     <xsl:value-of select="name" />
     <xsl:value-of select="cuisine" />
  </xsl:template>
</xsl:stylesheet>
Hodor
  • 9
  • 4
  • Possible duplicate of [How to 'select' from XML with namespaces?](http://stackoverflow.com/questions/284094/how-to-select-from-xml-with-namespaces) – Mathias Müller Jan 17 '16 at 19:18

1 Answers1

0

Yes it is a bitreally boring in XSLT 1.0... You have to bound your namespace explicitely to a prefix and use it in the XPath for your templates match= and apply-templates select=...

Here's what your refactored XSLT should look like:

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:t="http://tempuri.org/BaseSchema">
 <xsl:output method="text" encoding="UTF-8" />

  <xsl:template match="/t:event">
     <xsl:apply-templates select="t:venue/t:venue_type" />
  </xsl:template>

  <xsl:template match="/t:venue_type">
     <xsl:value-of select="t:name" />
     <xsl:value-of select="t:cuisine" />
  </xsl:template>
</xsl:stylesheet>
potame
  • 7,597
  • 4
  • 26
  • 33
  • Thanks a lot! It helped. wondering if there is other shortcut like having something added in the header tag itself. – Hodor Jan 17 '16 at 19:37
  • @Hodor Not in XSLT 1.0. In XSL-T 2.0 you can use `xpath-default-namespace`, see [this question](http://stackoverflow.com/questions/1344158/) – potame Jan 17 '16 at 20:01
  • thanks. But selection of one node selects value of even it's siblings and children. In the above XSLT if `` is removed, in the resulting answer the cuisine is still seen. or anything which is children of is seen. Can you point out, what I might be missing here? – Hodor Jan 18 '16 at 05:08
  • Resolving my doubt in the above comment. Changing `` to `` corrected the issue. Thanks. – Hodor Jan 18 '16 at 05:34
  • @Hodor if your issue has been solved, please accept the answer. Thanks. – potame Feb 24 '16 at 16:59