[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Changing namespaces


Subject: [xsl] Changing namespaces
From: Pradnya Gawade <pradnya.gawade7@xxxxxxxxx>
Date: Mon, 15 Aug 2011 17:09:34 -0400

Hi folks,

I am trying to achieve some XSLT transformation and having hard time
in getting the exact output. Any help will nbe highly appreciated.

My input xml has elements from 2 namespaces. One is my default
namespace and another with namespace prefix as 'OC:'. Target is to
strip out all the elements and attributes with this prefix 'OC:'
except the selected once. I try to achieve this by using identity
template modified for my requirements in following steps:
1. Use copy template and specify not to copy any elements/attribute
from the namespace corresponding to prefix 'OC:'
2. In a separate templates copy selectively the elements and
attributes I want to retain in the output.

After struggling a lot for the issue of namespances uri getting
displayed in the individual elements I copy selectively in last step,
I finally got it correct. Now the only difficulty I am having is
getting the root element with right namespace URIs in it. I wish to
have default namespace changed to some thing else, say
"new-default-namespace" and namepsace corresponding to prefix 'OC:'
changed to some thing else, say "new-oc-namespace-uri". I went through
lot on similar postings online but could not get the perfect solution.

My xsl stylesheet is as follows:

<xsl:stylesheet version="2.0"
	xmlns:odm="old-default-namespace"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xmlns:def="http://www.cdisc.org/ns/def/v1.0"
	xmlns:xlink="http://www.w3c.org/1999/xlink" xmlns:OC="old-oc-namespace-uri"
	xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
	exclude-result-prefixes="xlink" xmlns:exsl="http://exslt.org/common">

	<!-- standard copy template -->
	<xsl:template name="copyTemplate" match="node()|@*">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" />
		</xsl:copy>
	</xsl:template>


<xsl:template name="removeOCExtnElmnt" priority="3" match="//*[
namespace-uri()='old-oc-namespace-uri' ]" ></xsl:template>

<xsl:template name="removeOCExtnAttrib" priority="2" match="//@*[
namespace-uri()='old-oc-namespace-uri' ]" ></xsl:template>

<xsl:template priority="4" match="@ODMVersion">
	<xsl:attribute name="ODMVersion">1.2</xsl:attribute>
</xsl:template>
		
<xsl:template priority="5" match="OC:MultiSelectList |
OC:MultiSelectListRef | OC:MultiSelectListItem">
	<xsl:element name="{name()}" namespace="{namespace-uri()}" >		
		<xsl:copy-of select="@*"/>
			<xsl:apply-templates/>
		</xsl:element>
</xsl:template>
	
<xsl:template priority="6" match="//*[@OC:StudySubjectID |
@OC:UniqueIdentifier | @OC:Status
	| @OC:DateOfBirth | @OC:Sex | @OC:StudyEventLocation | @OC:StartDate
	| @OC:SubjectAgeAtEvent | @OC:Version | @OC:InterviewerName
	| @OC:InterviewDate | @OC:Status]">
	<xsl:element name="{name()}" namespace="{namespace-uri()}">
		<xsl:copy-of select="@*" copy-namespaces="no"/>
			<xsl:apply-templates/>
		</xsl:element>
</xsl:template>

</xsl:stylesheet>

Root element in my present output and input xml is:
<ODM xmlns="old-default-namespace" xmlns:OC="old-oc-namespace-uri"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
FileOID="oc_extn_testD20110809115446-0400" Description="oc_extn_test"
CreationDateTime="2011-08-09T11:54:46-04:00" FileType="Snapshot"
ODMVersion="1.2"
xsi:schemaLocation="http://www.cdisc.org/ns/odm/v1.3OC-ODM1-3-0-OC2-0.xsd">

I expect it to be changed to:

<ODM xmlns="new-default-namespace" xmlns:OC="new-oc-namespace-uri"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
FileOID="oc_extn_testD20110809115446-0400" Description="oc_extn_test"
CreationDateTime="2011-08-09T11:54:46-04:00" FileType="Snapshot"
ODMVersion="1.2"
xsi:schemaLocation="http://www.cdisc.org/ns/odm/v1.3OC-ODM1-3-0-OC2-0.xsd">


Thanks in advance.

- Pradnya


Current Thread
Keywords