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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top