XSLT and Java: Default Namespace XMLNS

RahulD Source

I'm new to XSLT and I'm trying to transform one XML file into another one. My problem is that there is one namespace 'xmlns' without any prefix in the original xml file and when I transform it to another one through xslt then nothing happens but if I remove that xmlns namespace then it works out but actually I cannot modify the original .xml file since I'm bound to use that file only, so I have to keep xmlns in the original file as it is. So can anyone please suggest some modifications in my .xsl or java code to overcome this problem.

My Original XML namespace looks like-

<?xml version="1.0" encoding="UTF-8"?>
        <manifest identifier="eXeorm_sample4823c6301f29a89a4c1f"
        xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
        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"      
</manifest>

My desired xml is:

 <?xml version="1.0" encoding="UTF-8"?>
        <manifest identifier="eXescorm_quiz4823c6301f29a8419515" 
            xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" 
            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" 
        </manifest>

My Modified XSLT-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:ims="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" 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="ims:manifest">   
</xsl:stylesheet> 
javaxmlxsltxml-namespacesprefix

Answers

answered 6 years ago Michael Kay #1

There's a search box at the top of this page: type "XSLT default namespace" and you will find hundreds of answers to this question.

Incidentally, your code is incredibly verbose. Instead of this:

<xsl:element name="item">
    <xsl:attribute name="identifier">ITEM-eXeorm_sample4823c6301f29a89a4d27</xsl:attribute>
    <xsl:attribute name="isvisible">true</xsl:attribute>
    <xsl:attribute name="identifierref">RES-eXeorm_sample4823c6301f29a89a4d28</xsl:attribute>
</xsl:element>

use this:

<item identifier="ITEM-eXeorm_sample4823c6301f29a89a4d27" invisible="true"
      identifierref="RES-eXeorm_sample4823c6301f29a89a4d28"/>

comments powered by Disqus