Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 1st, 2005, 08:10 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default Confused about XML namespaces

I've been googling for answers for a while now but can not seem to find an explanation of how XML namespaces play together between a source XML document and an XSLT.

Here's the situation:
- I have a blog. (http://geekdork.blogspot.com)
- The blog has a feed. (http://geekdork.blogspot.com/atom.xml)
- I can not affect the feed, it's generated.
- The feed has some nodes with an xmlns attribute defined in them (value is "http://purl.org/atom/ns#"). Example:
   <entry xmlns="http://purl.org/atom/ns#">

I'd like to read in the feed, transform it with an XSLT and display the result on an ASP.NET page. However, I can't seem to get past the namespace issue.

I have saved a copy of the feed locally and have added a reference to an XSLT style sheet to test the layout. I have a single "xsl:template":

    <xsl:template match="//entry">

That XPath does not work. In my testing on a XPath tester I found I needed to specify the namespace prefix in the query (//ns:entry) in order to get the query to return nodes as expected.

When I bring up the XML document in IE to test the XSLT I get the following error:
   "Reference to undeclared namespace prefix".
All variations of the xpath result in this same error. It seems that something is seeing the xmlns attributes in the source XML document and requiring their declaration somewhere.

I've been searching on that exact error string, but still can't seem to find an answer. I just don't understand where in the XSLT I need to declare the namespace for nodes in the source document. I have tried putting a namespace attribute in the style-sheet root node as several pages suggested:

<xsl:transform version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://purl.org/atom/ns#">

This still didn't work.

I have worked with XML enough to understand XPath queries and have done some XSLT transformations, but it has always been on simpler XML document structures without namespaces.

Can someone explain where I need to define the namespace(s)?

-Peter
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie
  #2 (permalink)  
Old June 1st, 2005, 09:16 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Declare

xmlns:ns="http://purl.org/atom/ns#"

in the xsl:stylesheet element, and then use

select="//ns:entry"

It must be an explicit namespace prefix, not the default namespace.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old June 2nd, 2005, 04:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Peter you are not alone in your confusion, as this extract from the W3C XSLT 2 requirement shows:

Quote:
quote:
Extract from http://www.w3.org/TR/xslt20req
2.1 Must Allow Matching on Default Namespace Without Explicit Prefix

Many users stumble trying to match an element with a default namespace. They expect to be able to do something like:

<xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns="urn:myuri">

  <xsl:template match="foo">

thinking that leaving off the prefix from the foo element name, that it will match <foo> elements in the default namespace with the URI of urn:myuri. Instead, they are required to assign a non-null prefix name to their namespace URI and then match on "someprefix:foo" instead, which has proven to be far from obvious. XSLT 2.0 SHOULD provide an explicit way to handle this scenario to avoid further user confusion.
hth
Phil


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very confused on XML to XML conversion mal141 XSLT 2 August 11th, 2006 02:44 AM
cant reach data in xml with namespaces TPP XSLT 3 March 25th, 2006 04:40 PM
cant reach data in xml with namespaces TPP XSLT 1 March 24th, 2006 11:47 AM
XML Namespaces billy_bob_the_3rd XML 1 January 31st, 2005 03:41 PM
.NET and XML Namespaces - NamespaceManager billy_bob_the_3rd XML 1 December 15th, 2004 08:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.