tags:

views:

156

answers:

3

Hi,

I am new to XSLT, so this question may have been answered other times. I've searched but I did not found anything :(

I need to parse an XML like this

<ns1:tagName1>
    <ns2:tagName2>
          This is the content
    </ns2:tagName2>       
</ns1:tagName1>

And I using this XSL for that

<xsl:template match="ns1:tagName1">
    <resultns1>
     <xsl:if test="ns2:tagName2">
      <resultns2>
       <xsl:value-of select=".">
      </resultns2>
     </xsl:if>
    </resultns1>
</xsl:template>

The result that I expect is

<resultns1>
    <resultns2>
     This is the content
    </resultns2>       
</resultns1>

but instead of it, all that I get is

<resultns1/>

If both tags use the same namespace, all works as expected, but if the outer tag is in ns1 and the inner one is in ns2, then the inner one is not detected. Any clues on why this is happening ?

Thanks !

A: 

The XSLT needs to declare the same namespaces as the XML file does. Perhaps your ns2 declaration is slightly different between the two files? Be especially careful about things like the case of letters (it's case-sensitive) and trailing slashes and the like. The namespace strings must match exactly.

If that doesn't help, perhaps you could post a complete XML and XSLT file that demonstrates the problem you're having?

Peter Cooper Jr.
+2  A: 

It works fine for me; xml:

<?xml version="1.0" encoding="utf-8" ?>
<xml xmlns:ns1="foo" xmlns:ns2="bar">
  <ns1:tagName1>
    <ns2:tagName2>
      This is the content
    </ns2:tagName2>
  </ns1:tagName1>
</xml>

xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns1="foo" xmlns:ns2="bar"
    exclude-result-prefixes="ns1 ns2"
>
  <xsl:template match="/xml">
    <xsl:apply-templates select="*"/>
  </xsl:template>

  <xsl:template match="ns1:tagName1">
    <resultns1>
      <xsl:if test="ns2:tagName2">
        <resultns2>
          <xsl:value-of select="."/>
        </resultns2>
      </xsl:if>
    </resultns1>
  </xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="utf-8"?>
<resultns1>
  <resultns2>
    This is the content
  </resultns2>
</resultns1>
Marc Gravell
A: 

Ouch !

while preparing a complete XML and XSLT I realized that namespaced in both files were referring to different schemas :(

So altought they use the same name, by using different schemas makes them different

Thank you very much for pointing me in the right direction :)

You should be able to use another alias to do the same, though.
Marc Gravell