0

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!

daniel9x
  • 755
  • 2
  • 11
  • 28
  • 1
    If you remove the "xmlns" attribute on "entry", does it work? – David Mar 12 '14 at 20:47
  • Hi, you should be able to use the root `entry` element in your xpath lookup (see here for an example - http://www.w3.org/TR/xslt20/#d5e25672). Two questions. 1) How is the `$xmlfile` variable defined? 2) Why aren't you using the namespace in your xpath? (Can you please provide the full xsl) – Nick Grealy Mar 12 '14 at 23:19

2 Answers2

0

XPath and XSLT are namespace-aware. To specify a namespaced node in a path, you should give the node name a prefix that you have bound to the correct namespace.

Using wildcards, and then adding a predicate to test only the localname, is possible but generally the Wrong Answer. Namespaces are a meaningful part of the name. Use them properly.

keshlam
  • 7,931
  • 2
  • 19
  • 33
0

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.

Tim C
  • 70,053
  • 14
  • 74
  • 93
  • Thank you so much for this! This worked perfectly, and I understand a little bit more about XSLT now. :) – daniel9x Mar 13 '14 at 13:52