4

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!

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. I don't what is the problem with my processor. It is still producing the same result. I'm using a JAXP program to generate the output by taking .xml and .xsl file as input. So can this problem happen due to that program also? Thanks!
@rahuldwivedi, I don't know. Try specifying the top element as a literal-result-element. What XSLT processor are you using? Maybe it's time to get rid of it...
I just tested the generated output and it didn't create any problem in my project even with those extra prefixes, so I can afford this output. I use SAXON, Can you please suggest me what could be a better processor? Thanks.
@rahuldwivedi, That's weird, Saxon is one of the processors with which I can't repro your result. Most probably you are using a very old version, or you haven't given us the exact code that you have.
2

Just make this small change:

  • Set the default name-space of your style-sheet to...

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

1 Comment

Thanks for the answer, I had added this default namespace but still its producing the same result. I'm unable to figure out the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.