xmlxsltvalue-of

Entry XML tag is causing the XSL value-of to not work


I have a (simplified) XML file I'm trying to transform:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns="http://www.symplectic.co.uk/vivo/" xmlns:api="http://www.symplectic.co.uk/publications/api">
<tag>Hello, Hello.</tag>
</entry>

Wrapping my content in the "entry" tag seems to cause problems with this statement in my XSLT.

<xsl:value-of select="$xmlfile/entry/tag"/>

Where the $xmlFile is the fn:document output of the above XML. I have found that when removing the entry tags from the XML and simply modifying my XSLT to:

<xsl:value-of select="$xmlfile/tag"/>

I get the content (Hello,Hello) I'm expecting in my output. I have verified that doing a copy-of $xmlfile returns the full XML file. I'm sure there is something I'm not doing right syntactically as I'm new to XSL. Any advice would be appreciated.

Thanks!


Solution

  • The issue is indeed with the entry element...

    <entry xmlns="http://www.symplectic.co.uk/vivo/"
    

    The xmlns attribute here is actually a namespace declaration. Or rather, because it does declare a prefix (like xmlns:api does) it is the default namespace. This means the entry element and its child tag element belong to the namespace.

    Now, your xslt is doing this...

    <xsl:value-of select="$xmlfile/entry/tag"/>
    

    This is looking for entry and tag elements that belong to NO namespace. These are different to elements with the same name that do belong to a namespace.

    The solution is to declare the namespace, with a prefix, in the XSLT, like so

    <xsl:stylesheet version="1.0" 
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:vivo="http://www.symplectic.co.uk/vivo/">
    

    Then change your xslt expression to look like this

    <xsl:value-of select="$xmlfile/vivo:entry/vivo:tag"/>
    

    Note the actual prefix is no important, but the namespace url must match.