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 August 3rd, 2010, 03:02 PM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default XML, Namespaces and XSLT, Oh, My!

First, I've been through Dave Pawson's excellent collection of namespace related information. If the answer to my question is in there.. I missed it.

I'm using Saxon9.0.06J, although I have OxygenXML 11, so there is a newer Saxon in there if I can figure out how to use from the command line.

I am writing a stylesheet to transform one XML file into another with a different schema. I need namespaces, including a default namespace, on the result XML file, so I've added them to the stylesheet so they will be declared in the result file. But, this is causing me problems.

I've boiled down the problem to as simple form as I can get it.

The Stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        version="2.0"
        xmlns="http://MyDefaultNS"
    >
    <!--
        xmlns="http://MyDefaultNS">
        
        We need the default namespace decl in the stylesheet element
        so it will appear on the root element decl of the result file and put the default NS in scope for the result document. This is needed so it will pass validation against the schema.
        
    -->
    <xsl:template match="/">
        <xsl:text>&#x0a;Test of Subtree&#x0a;</xsl:text>
        
        <xsl:variable name="testST">
            <SubElem1>
                Value for Sub Element 1
            </SubElem1>
            <SubElem2>
                Value for Sub Element 2
            </SubElem2>
        </xsl:variable>
        
        <xsl:value-of select="$testST/SubElem1"></xsl:value-of>
    </xsl:template>
    
</xsl:stylesheet>
The problem is, value of $testST/SubElem1 is not returning anything.
I use these subtrees throughout my stylesheet to pre-process things and
none of it works since I added the default 'xmlns' statement to the stylesheet declaration.

If I remove the default NS decl, then I get "Value for Sub Element 1" as expected from the 'value-of' statement.

What did adding this default NS statement do to my stylesheet?

Thanks!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
  #2 (permalink)  
Old August 3rd, 2010, 04:07 PM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Well, #48 of Dave's top 63 reason why I don't understand namespaces provided me a clue.

I changed the definition of the temporary tree to
Code:
<xsl:variable name="testST">
            <SubElem1 xmlns="">
                Value for Sub Element 1
            </SubElem1>
            <SubElem2 xmlns="">
                Value for Sub Element 2
            </SubElem2>
        </xsl:variable>
and it works.

So, my question becomes.. why?

Why can't the temporary results be part of the globally defined default namespace just like anything else? I guess they are and that's the problem.. So, why is it a problem and what's the best solution? I 'could' go through my stylesheet and add the xmlns="" decl to all the temporary elements, but that seems like a brut-force solution. I'm still missing something in the XML/NS/XSLT mix.

It seems the alternative is to remove the global NS decl and place local default NS decls on all elements I write to the results, which also seems a bit brut-force.



Thanks
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare

Last edited by mphare; August 3rd, 2010 at 04:10 PM..
  #3 (permalink)  
Old August 3rd, 2010, 04:21 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

A default namespace declaration in the stylesheet DOES apply to unqualified element names in literal result elements, but does NOT apply to unqualified element names in a path expression.

As for the question why? - meaning why did the WG decide to design the language that way - I think that demands historical research to discover, and in my experience, explaining why a committee made a decision can be difficult even when you were present at the relevant meetings, which in this case I wasn't. (I could interpret the question as being "can you advance any arguments in favour of doing it this way", but that's a different question.)
  #4 (permalink)  
Old August 3rd, 2010, 04:54 PM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Thanks Michael.

The why question wasn't so much a why did they do it this way, that's history and it's done. More, I assume it was done this way for a purpose, and so there must be a solution to my problem. XSLT/XPath is a fascinating language to program in. I can spend hours cooking up solutions to a problem, then discover there is a much better way by either changing my logic, or by learning a new facet of the language. Usually, all I have to do is stop thinking like a C programmer for a minute or two.

For this problem, the solution I came up with is to either give all my temporary elements an empty NS (xmlns=""), or to add a NS to all my queries (which I am still researching).

If that(those) is(are) the solution(s), then I'll proceed. I really just want to be sure that was the best solution and that I was not going about this the wrong way missing some magic construct that makes this all better.

Thanks again!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
  #5 (permalink)  
Old August 4th, 2010, 03:24 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

An alternative in XSLT 2.0 is to add the xpath-default-namespace attribute to the stylesheet element - this changes the default namespace in your XPATH expressions from the null namespace to the one you define.

Usually this would be the main namespace in your input XML, not the one in your output XML as in your example above.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #6 (permalink)  
Old August 4th, 2010, 02:27 PM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

I tried xpath-default-namespace, it fixed the temporary results trees, but it had the adverse affect of causing all my xpath directives pointing to the source document to fail for the same reason (wrong NS)

The easy solution was to add a xmlns="" directive to all the variable definitions that are building temporary trees causing all the temp tree to be in the null NS, which is what the xpath statements are expecting.

Thanks!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare


Similar Threads
Thread Thread Starter Forum Replies Last Post
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
Confused about XML namespaces planoie XSLT 2 June 2nd, 2005 04:23 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.