0

I have to transfrom from one XML to another by using XSLT. I have to add two new nodes <file href="new1.js"> and <file href="new2.js"> with attributes inside the <resource> tag where-ever it appears-

My 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"> 

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

    <resources>
         <resource identifier="RES22" type="webcontent" href="index.html">
                 <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:xhtml="http://www.imsglobal.org/xsd/imscp_v1p1"
                xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
                exclude-result-prefixes="xhtml">
                <xsl:output method="html" indent="yes" encoding="UTF-8"/>
                <xsl:strip-space elements="*" />

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

    <xsl:template match="xhtml:resource">
     <file href="new1.js"/>
    <file href="new2.js"/>
     <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

But it doesn't add the new file nodes. Thanks!

1 Answer 1

1
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
  exclude-result-prefixes="xsl ims">
<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="ims:resource" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
  <file href="new1.js"/>
  <file href="new2.js"/>   
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

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.