Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old October 14th, 2003, 07:59 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath matching with a default namespace

I'm terribly confused, but I've got a simple toy example...

Input File:

<foo xmlns="http://foo/bar" name="A"/>

Stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://foo/bar"
                version="1.1">
    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <!--
    <xsl:template match="*[name() = 'foo']">
        <xsl:value-of select="@name"/>
    </xsl:template>
    -->

    <xsl:template match="foo">
        <xsl:value-of select="@name"/>
    </xsl:template>

</xsl:stylesheet>

My expected output is "A". I get that output when I use the template that's commented out here, but not when I match against "foo", as this example does.

Can someone explain what's going on here? What am I missing?

Thanks,
Rich
 
Old October 15th, 2003, 12:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Look what's going on:

1) When you want to match the element "foo" by using name() function, the XSLT processor tries to find an element in the source XML document whose element type literally equals to "foo" (by the way this approach is not safe and must be avoided when possible, since there are issues concerned with namespaces, but not directly concerned the issue you've arisen here). So, the processor sees an element whose element type is "foo" and outputs "A".
2) When you want to match the element by
Code:
match="foo"
, the XSLT processor tries to find an element having element type "foo" which is in so called "null" namespace(that is which isn't in any namespace). Since the "foo" element is in the namespace identified by the URI "http://foo/bar", no match is found.

-----
I think the default namespace declaration in your stylesheet confused you. It has no connection with the source XML doc at all and doesn't influence any pattern in template matchings.

There are 2 solutions:

1. you can remove the default namespace declaration in the source XML(also in the stylesheet since it has nothing to do there) or change it to xmlns="", which are equivalent, and you'll get the output you want.
2.
a) change the namespace declaration in the source XML doc as following:
Code:
<bar:foo xmlns:bar="http://foo/bar" name="A"/>
b) change the namespace declaration in the stylesheet:
[code]
 xmlns:bar="http://foo/bar"
[code]
c) change the "match" attribute value: "bar:foo"


Regards,
Armen
 
Old October 15th, 2003, 03:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Rich you are not alone in your confusion. The W3C has acknowledged this problem and, hopefully, will sort it out in XSLT v2, as this extract from the W3C 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.
rgds
Phil
 
Old October 15th, 2003, 04:52 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. I get what's going on now. It seems an odd choice on the w3c's part to say that unqualified element references should take on the null namespace, rather than the default namespace. It makes it impossible for me to generate an output file with a default namespace for all the elements.

I tried using the {http://foo/bar}myelement syntax, to no avail.

Also, I've been playing around with all kinds of namespace tricks, like:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:bar="http://foo/bar"
                xmlns="http://foo/bar"
                exclude-result-prefixes="bar"
                version="1.1">
    <xsl:namespace-alias stylesheet-prefix="bar" result-prefix="#default"/>

... and I'm able to generate elements that use the default namespace in the output file, but I still get little xmlns:bar="http://foo/bar" turds left in various places in the output file. Correct XML, strictly speaking, but not particularly pretty.

I guess I'll just have to deal with it until XSLT 2.0.

Thanks,
Rich
 
Old October 16th, 2003, 02:31 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can show the elements the way you want, you just need to be careful how you construct them. Can you show a small example of your source?

--

Joe
 
Old December 4th, 2003, 01:22 AM
Registered User
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rich,

Thanks for the tips on <xsl:namespace-alias/> Worked well for me and saved me a long night...

Hopefully the W3C will get this together on v2.

William Walseth


They say I'm lazy, but it takes all my time.&nbsp;&nbsp;
Joe Walsh





Similar Threads
Thread Thread Starter Forum Replies Last Post
default-xpath-namespace stolte XSLT 1 March 19th, 2008 12:49 PM
XPATH: Retrieve Value without namespace prefixes ramesh.kumarm XSLT 4 September 14th, 2006 03:50 AM
Access Namespace Node using XPath pvasudevan XSLT 1 January 2nd, 2006 06:20 AM
Default Namespace (where is it?) bekim BOOK: ASP.NET Website Programming Problem-Design-Solution 4 June 12th, 2004 03:50 AM
problems with xpath and matching templates Gero Meißner XSLT 3 August 16th, 2003 01:54 AM





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