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 January 20th, 2009, 12:06 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default Namespace Inheritance

I have some questions about namespace inheritance. I'm trying to set the default namespace of an output document and have all the child elements of the document node belogn to that namespace. But I seem to be having some trouble. I cannot think of a better way to illustrate this than with a example. I apologize for the semi-large amount of code, but I think it best illustrates my point.

Input Source Document
Code:
<Top>
    <A>Adata</A>
    <B>Bdata</B>
    <C>Cdata</C>
    <D>
        <D1>1data</D1>
        <D2>2data</D2>
    </D>
</Top>
Transformation
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:output method="xml" indent="yes"/> 
 
    <xsl:template match="/">
        <xsl:element name="Transaction" inherit-namespaces="yes" namespace="http://myuri.com">
            <xsl:apply-templates select="Top" />
        </xsl:element>
    </xsl:template>
 
    <xsl:template match="Top">
        <xsl:element name="Category">
            <xsl:attribute name="Source" select="A" />
            <xsl:element name="Description">
                <xsl:value-of select="B" />
            </xsl:element>
            <xsl:element name="Shortname">
                <xsl:value-of select="C" />
            </xsl:element>
            <xsl:apply-templates select="D" />
        </xsl:element>
    </xsl:template>
 
    <xsl:template match="D">
        <!--More Elements & Attributes-->
    </xsl:template>
</xsl:stylesheet>
Resulting document
Code:
<Transaction xmlns="http://myuri.com">
  <Category Source="Adata" xmlns="">
    <Description>Bdata</Description>
    <Shortname>Cdata</Shortname>
  </Category>
</Transaction>
Now when I check to see which namespaces that the elements belong to, I don't get what
I expect. Only the 'Transaction' node belongs to the 'http://myuri.com' namepsace. I used this transformation to check.
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:template match="node()">
        <xsl:value-of select="local-name(.),namespace-uri(.)"/>
        <xsl:apply-templates/>
    </xsl:template>
 
    <xsl:template match="text()">
    </xsl:template>
</xsl:stylesheet>
The results say the following:
Transaction http://myuri.com
Category {null}
Description {null}
Shortname {null}

I'm looking for a method to define a default namespace and force that namespace onto all the generated child elements of that document node. Also keep in mind that the real transformation that this will be applied to spans multiple <xsl:include>s and mutiple files. So the scope of the transformation is across many templates, call templates and global variables.

I really want the results that every element belogns to the default namespace defined at the document node.

Transaction http://myuri.com
Category http://myuri.com
Description http://myuri.com
Shortname http://myuri.com

Can to provide some insight as to how this might be done?
I really appreciate the help!

Thanks.
 
Old January 20th, 2009, 12:15 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

inherit-namespaces controls whether an element has particular namespaces in scope or not. It never changes the actual name of the element. The name of the element is determined by the values of the name and namespace attributes of the xsl:element instruction (or by the name of the literal result element where appropriate.)

It's always true that the name of the element (both the local part and the URI part) is determined by the instruction that creates the element, and is not affected by what namespaces are in scope from parent elements.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 20th, 2009, 12:18 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The easiest way is simply to put the default namespace declaration on the xsl:stylesheet element:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"
  xmlns="http://myuri.com"">
    <xsl:output method="xml" indent="yes"/> 
 
    <xsl:template match="/">
        <Transaction>
            <xsl:apply-templates select="Top" />
        </Transaction>
    </xsl:template>
 
    <xsl:template match="Top">
        <Category Source="{A}">
            <Description>
                <xsl:value-of select="B" />
            </Description>
            <Shortname>
                <xsl:value-of select="C" />
            </Shortname>
            <xsl:apply-templates select="D" />
        </Category>
    </xsl:template>
 
    <xsl:template match="D">
        <!--More Elements & Attributes-->
    </xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 20th, 2009, 01:15 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

Quote:
Originally Posted by Martin Honnen View Post
The easiest way is simply to put the default namespace declaration on the xsl:stylesheet element:
Thanks for both your replies, I really appreciate it.

The "issue" with defining the namespace in the transformation is that we actually are going to use different namespace, depending on data coming from the source document.

We have a variable like this in the transformation:
Code:
<LOBs>
    <LOB LOBCode="FPC">
        <Namespace>http://myuri.comi/FPC</Namespace>
    </LOB>
    <LOB LOBCode="AFL">
        <Namespace>http://myuri.com/AFL</Namespace>
    </LOB>
</LOBs> 
We would like to reference a node's value in the input document to determine the namespace of the entire output document. We make use to the above lookup table to retrieve the desired namespace.

Some code like the following:

Code:
namespace="{/LOBs/LOB/Namespace[../@LOBCode = current()/INPUT_ROOT/CHILD/NAMESPACE_CODE]}
So for example:
If the input document has a NAMESPACE_CODE of ' FPC', the entire resulting document would belong to the default namespace of 'http://myuri.comi/ECCC'.

We would like to be able to determine the default namespace at the runtime of the transformation.

We understand that would could a "second pass" transformation. Take the resulting document and force it into a desired namespace after the fact, but we'd like to avoid a second XSLT solution.

Are there any other mechanisms available?
 
Old January 20th, 2009, 01:47 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to compute namespaces at run-time then you can do that but you have to do it for each element you create so you have to put your code
namespace="{/LOBs/LOB/Namespace[../@LOBCode = current()/INPUT_ROOT/CHILD/NAMESPACE_CODE]}on each xsl:element where you want to create the element in that namespace.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 20th, 2009, 02:40 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

Well with a 10,000+ line transformation, that's not much of an option at this point :)

heh.

We have a solution that takes the resulting document and assigns every element a prefix and a namespace. Looks like we'll ahve to settle for that.

Thanks for your help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritance magagulad Visual Basic 2005 Basics 1 May 12th, 2008 07:51 AM
Is it possible via Inheritance!!! mike_remember ASP.NET 2.0 Professional 0 November 26th, 2007 10:25 AM
Inheritance michaelcode ASP.NET 2.0 Basics 5 September 26th, 2006 01:40 PM
Inheritance abhi_bth ASP.NET 1.0 and 1.1 Basics 0 September 23rd, 2006 10:03 AM
Need help with inheritance filip BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 August 25th, 2006 09:38 PM





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