Search code examples
xmlxsltxslt-1.0prefix

XSLT: Prefix and namespace get added


I'm converting from one XML to another XML by using XSLT. By applying the answers given in the forum, I'm able to achieve my all the requirements for desired output but the only problem is that in the output one extra prefix ns0 gets added automatically at two places and namespace xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" is added in the start of every 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 Output:

<?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>

Output which I get : Instead of

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

I get

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

while instead of

<resources>

I get

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

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

Thanks!


Solution

  • Can't reproduce the problem!

    I run the provided transformation on the provided XML document with all 11 XSLT processors I have on my computer and all produced the same, wanted, correct result.

    Anyway, why don't you try this slightly modified transformation, which also produces the wanted 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>