XSLT: Added Prefixes and Namespace

I am transitioning from one XML to another XML using XSLT. Applying the answers provided on the forum, I can fulfill all the requirements for the desired result, but the only problem is that the output is prefixed with ns0 automatically in two places and the namespace xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" added xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" at the beginning of each node.

Input file

 <?xml version="1.0" encoding="UTF-8"?> <manifest identifier="eXescorm_quiz4823c6301f3d3afc1c1f" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"> <resources> <resource identifier="RES22" type="webcontent" href="index.html"> <file href="index.html"/> <file href="common.js"/> </resource> </resources> </manifest> 

Desired Result:

 <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" identifier="eXeorm_sample4823c6301f29a89a4c1f" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd"> <resources> <resource identifier="RES22" type="webcontent" href="index.html" adlcp:scormtype="sco"> <file href="index.html"/> <file href="common.js"/> <file href="new1.js"/> <file href="new2.js"/> </resource> </resources> </manifest> 

MY XSLT:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl ims adlcp xsi"> <xsl:output method="xml" indent="yes" /> <xsl:strip-space elements="*" /> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="/*"> <xsl:element name="{name()}" namespace="{namespace-uri()}"> <xsl:copy-of select="namespace::*[name()]"/> <xsl:apply-templates select="@*"/> <xsl:attribute name="xsi:schemaLocation"> <xsl:value-of select= "'http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'" /> </xsl:attribute> <xsl:apply-templates select="node()"/> </xsl:element> </xsl:template> <xsl:template match="ims:resource" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute> <xsl:apply-templates select="node()"/> <file href="new1.js"/> <file href="new2.js"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

The result that I get: Instead

 <manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> 

I get

 <ns0:manifest xmlns:ns0="http://www.imsglobal.org/xsd/imscp_v1p1" > 

but instead

 <resources> 

I get

 <resources xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">` 

(and this xmlns is also added to the top of some other nodes), while the rest of things are fine.

Thanks!

+4
source share
2 answers

Unable to reproduce the problem!

I run the provided conversion on the provided XML document with all 11 XSLT processors that I have on my computer, and they all produce the same, desired, correct result.

In any case, why don't you try this slightly modified transformation , which also gives the desired result ?:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" exclude-result-prefixes="xsl ims adlcp xsi"> <xsl:output method="xml" indent="yes" /> <xsl:strip-space elements="*" /> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="/*"> <xsl:element name="{name()}" namespace="{namespace-uri()}"> <xsl:copy-of select="namespace::*[name()]"/> <xsl:apply-templates select="@*"/> <xsl:attribute name="xsi:schemaLocation"> <xsl:value-of select= "'http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'" /> </xsl:attribute> <xsl:apply-templates select="node()"/> </xsl:element> </xsl:template> <xsl:template match="ims:resource"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute> <xsl:apply-templates select="node()"/> <file href="new1.js" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"/> <file href="new2.js" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 
+3
source

Just make this small change:

  • Set the default namespace to your style for ...

     xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
+2
source

All Articles