xslt to dynamically re-namespace elements

W

wooks

I want to apply 2 changes to the following xml

<ROOTSTUB app="appname"/>

1. change the app attribute to an element.
2. put the ROOTSTUB element into a namespace that is passed as a
global parameter at run time.


so I want

<ROOTSTUB xmlns="urn:aNamespace">
<app>appname</app>
</ROOTSTUB>

and I'd like to do it in one pass. I have tried

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <
<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:param name="namespace"/>
<xsl:template match="node()|@*">
<xsl:element name="{local-name(.)}" namespace="$namespace">
<xsl:value-of select= "." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>

and variations thereof. I cannot get the output I desire.
Can anyone else?
 
P

Patrick TJ McPhee

% <xsl:stylesheet
% xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
% version="1.0"> <
% <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8"
% indent="yes"/>
% <xsl:param name="namespace"/>
% <xsl:template match="node()|@*">

You probably want this to match just "*|@*" (elements and attributes).
You'll want another template matching and copying other nodes
<xsl:template match="node()">
<xsl:copy-of select ='.'/>
</xsl:template>

% <xsl:element name="{local-name(.)}" namespace="$namespace">
% <xsl:value-of select= "." />
% </xsl:element>

how about

<xsl:element name="{local-name(.)}" namespace="{$namespace}">
<xsl:apply-templates select= "node()|@*" />
</xsl:element>
 
W

wooks

% <xsl:stylesheet
% xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
% version="1.0"> <
% <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8"
% indent="yes"/>
% <xsl:param name="namespace"/>
% <xsl:template match="node()|@*">

You probably want this to match just "*|@*" (elements and attributes).
You'll want another template matching and copying other nodes
<xsl:template match="node()">
<xsl:copy-of select ='.'/>
</xsl:template>

% <xsl:element name="{local-name(.)}" namespace="$namespace">
% <xsl:value-of select= "." />
% </xsl:element>

how about

<xsl:element name="{local-name(.)}" namespace="{$namespace}">
<xsl:apply-templates select= "node()|@*" />
</xsl:element>

I finally got this to work using a variation of your suggestion as below. Thanks

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="namespace"/>
<xsl:template match="*">
<xsl:element name="{local-name(.)}" namespace="{$namespace}">
<xsl:apply-templates select= "node()|@*" />
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:element name="{local-name(.)}" namespace="{$namespace}">
<xsl:value-of select= "." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top