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 July 9th, 2007, 09:41 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Default Namespace - XSLT 1.0

Hi!

I have a problem with the default namespace in XSLT 1.0.

I have a XSLT stylesheet which generates an XML-File. The namespace of the result XML-File depends on an element of the input XML-File.

So for example:
Input XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<version>2005</version>
XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
<xsl:element name="manifest">
Then if the version equals to 2005, the default namespace should be set to "http://a" otherwise to "http://b".
Further an attribute of the element should be generated, so:

Code:
    <xsl:choose>
        <xsl:when test="$version='2005'">

            <xsl:attribute name="xmlns:adlcp" namespace="{$xmlns}">http://www.adlnet.org/xsd/adlcp_v1p3</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>

            <xsl:attribute name="xmlns:adlcp" namespace="{$xmlns}">http://www.adlnet.org/xsd/adlcp_v1p2</xsl:attribute>
        </xsl:otherwise>
    </xsl:choose>

So how can i set the default namespace? Can i set it dynamically in XSLT 1.0? If not, does anybody know another solution?

Thanks!
Geierwally

 
Old July 9th, 2007, 09:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can't set the default namespace programmatically, but you can set the namespace in which new elements are placed at the point where you create the element:

<xsl:element name="newElement" namespace="{$targetNS}">

You can't use xsl:attribute to create namespace declarations - namespaces are not attributes in the XPath data model. It's very unusual to need to create namespace nodes "by hand" - usually you just need to put all your elements and attributes in the right namespace when you create them, and the namespace declarations will look after themselves.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 9th, 2007, 11:08 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! :)

So if i set a namespace for the first element, this will be set to the default namespace (xmlns) of the output file.

But why can't i use xsl:attribute to create namespace declarations?

This code would create the namspace prefix "haha"... : Or did you mean something else?
<xsl:attribute name="haha:test" namespace="{$haha}"></xsl:attribute>

Geierwally










Similar Threads
Thread Thread Starter Forum Replies Last Post
default-xpath-namespace stolte XSLT 1 March 19th, 2008 12:49 PM
Dynamic Default Namespace EcceBozo XSLT 2 June 6th, 2007 04:05 PM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
How can I change the default namespace allanhu BOOK: ASP.NET Website Programming Problem-Design-Solution 1 November 1st, 2004 01:15 PM
Default Namespace (where is it?) bekim BOOK: ASP.NET Website Programming Problem-Design-Solution 4 June 12th, 2004 03:50 AM





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